use spaces instead of tab
This commit is contained in:
+104
-104
@@ -34,122 +34,122 @@ using std::list;
|
||||
*/
|
||||
template <typename T>
|
||||
class Queue {
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
Queue() {
|
||||
pthread_mutex_init(&m_mutex, NULL);
|
||||
pthread_cond_init(&m_cond, NULL);
|
||||
}
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
Queue() {
|
||||
pthread_mutex_init(&m_mutex, NULL);
|
||||
pthread_cond_init(&m_cond, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
~Queue() {
|
||||
pthread_mutex_destroy(&m_mutex);
|
||||
pthread_cond_destroy(&m_cond);
|
||||
}
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
~Queue() {
|
||||
pthread_mutex_destroy(&m_mutex);
|
||||
pthread_cond_destroy(&m_cond);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
/**
|
||||
* Hidden copy constructor.
|
||||
* @param src the object to copy from.
|
||||
*/
|
||||
Queue(const Queue& src);
|
||||
private:
|
||||
/**
|
||||
* Hidden copy constructor.
|
||||
* @param src the object to copy from.
|
||||
*/
|
||||
Queue(const Queue& src);
|
||||
|
||||
|
||||
public:
|
||||
/**
|
||||
* Add an item to the end of queue.
|
||||
* @param item the item to add.
|
||||
*/
|
||||
void push(T item) {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
m_queue.push_back(item);
|
||||
pthread_cond_broadcast(&m_cond);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
}
|
||||
public:
|
||||
/**
|
||||
* Add an item to the end of queue.
|
||||
* @param item the item to add.
|
||||
*/
|
||||
void push(T item) {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
m_queue.push_back(item);
|
||||
pthread_cond_broadcast(&m_cond);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the first item from the queue optionally waiting for the queue being non-empty.
|
||||
* @param timeout the maximum time in seconds to wait for the queue being filled, or 0 for no wait.
|
||||
* @return the item, or NULL if no item is available within the specified time.
|
||||
*/
|
||||
T pop(int timeout = 0) {
|
||||
T item;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
if (timeout > 0) {
|
||||
struct timespec t;
|
||||
clockGettime(&t);
|
||||
t.tv_sec += timeout;
|
||||
while (m_queue.empty()) {
|
||||
if (pthread_cond_timedwait(&m_cond, &m_mutex, &t) == ETIMEDOUT) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (m_queue.empty()) {
|
||||
item = NULL;
|
||||
} else {
|
||||
item = m_queue.front();
|
||||
m_queue.pop_front();
|
||||
}
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return item;
|
||||
}
|
||||
/**
|
||||
* Remove the first item from the queue optionally waiting for the queue being non-empty.
|
||||
* @param timeout the maximum time in seconds to wait for the queue being filled, or 0 for no wait.
|
||||
* @return the item, or NULL if no item is available within the specified time.
|
||||
*/
|
||||
T pop(int timeout = 0) {
|
||||
T item;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
if (timeout > 0) {
|
||||
struct timespec t;
|
||||
clockGettime(&t);
|
||||
t.tv_sec += timeout;
|
||||
while (m_queue.empty()) {
|
||||
if (pthread_cond_timedwait(&m_cond, &m_mutex, &t) == ETIMEDOUT) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (m_queue.empty()) {
|
||||
item = NULL;
|
||||
} else {
|
||||
item = m_queue.front();
|
||||
m_queue.pop_front();
|
||||
}
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified item from the queue optionally waiting for it to appear in the queue.
|
||||
* @param item the item to remove and optionally wait for.
|
||||
* @param wait true to wait for the item to appear in the queue.
|
||||
* @return whether the item was removed.
|
||||
*/
|
||||
bool remove(T item, bool wait = false) {
|
||||
bool ret = false;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
do {
|
||||
size_t oldSize = m_queue.size();
|
||||
if (oldSize > 0) {
|
||||
m_queue.remove(item);
|
||||
if (m_queue.size() != oldSize) {
|
||||
ret = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
pthread_cond_wait(&m_cond, &m_mutex);
|
||||
} while (wait);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return ret;
|
||||
}
|
||||
/**
|
||||
* Remove the specified item from the queue optionally waiting for it to appear in the queue.
|
||||
* @param item the item to remove and optionally wait for.
|
||||
* @param wait true to wait for the item to appear in the queue.
|
||||
* @return whether the item was removed.
|
||||
*/
|
||||
bool remove(T item, bool wait = false) {
|
||||
bool ret = false;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
do {
|
||||
size_t oldSize = m_queue.size();
|
||||
if (oldSize > 0) {
|
||||
m_queue.remove(item);
|
||||
if (m_queue.size() != oldSize) {
|
||||
ret = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
pthread_cond_wait(&m_cond, &m_mutex);
|
||||
} while (wait);
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first item in the queue without removing it.
|
||||
* @return the item, or NULL if no item is available.
|
||||
*/
|
||||
T peek() {
|
||||
T item;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
if (m_queue.empty()) {
|
||||
item = NULL;
|
||||
} else {
|
||||
item = m_queue.front();
|
||||
}
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return item;
|
||||
}
|
||||
/**
|
||||
* Return the first item in the queue without removing it.
|
||||
* @return the item, or NULL if no item is available.
|
||||
*/
|
||||
T peek() {
|
||||
T item;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
if (m_queue.empty()) {
|
||||
item = NULL;
|
||||
} else {
|
||||
item = m_queue.front();
|
||||
}
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
/** the queue itself */
|
||||
list<T> m_queue;
|
||||
private:
|
||||
/** the queue itself */
|
||||
list<T> m_queue;
|
||||
|
||||
/** mutex variable for exclusive lock */
|
||||
pthread_mutex_t m_mutex;
|
||||
/** mutex variable for exclusive lock */
|
||||
pthread_mutex_t m_mutex;
|
||||
|
||||
/** condition variable for exclusive lock */
|
||||
pthread_cond_t m_cond;
|
||||
/** condition variable for exclusive lock */
|
||||
pthread_cond_t m_cond;
|
||||
};
|
||||
|
||||
#endif // LIB_UTILS_QUEUE_H_
|
||||
|
||||
Reference in New Issue
Block a user