42 #ifndef _DWMPTHREADQUEUE_HH_ 43 #define _DWMPTHREADQUEUE_HH_ 68 template <
typename _ValueType>
76 : _queue(), _mutex(), _cv()
87 SysLogger::Log(LOG_ERR,
"Pthread::~Queue() failed to lock! {%s:%d}",
100 return(this->_maxLength);
109 this->_maxLength = maxLength;
110 return(this->_maxLength);
121 if ((! this->_maxLength) ||
122 (this->_queue.size() < this->_maxLength)) {
123 this->_queue.push_back(value);
135 template <
typename InputIterator>
136 uint32_t
PushBack(InputIterator firstIter, InputIterator lastIter)
140 if ((! this->_maxLength) ||
141 (this->_queue.size() < this->_maxLength)) {
142 while ((firstIter != lastIter) &&
143 ((! this->_maxLength) ||
144 (this->_queue.size() < this->_maxLength))) {
145 this->_queue.push_back(*firstIter);
165 if ((! this->_maxLength) ||
166 (this->_queue.size() < this->_maxLength)) {
167 this->_queue.push_front(value);
191 rc = _cv.
Wait(_mutex);
207 gettimeofday(&tv,&tz);
208 struct timespec tspc;
209 tspc.tv_sec = tv.tv_sec + timeToWait.tv_sec +
210 ((tv.tv_usec * 1000) + timeToWait.tv_nsec) / 1000000000;
211 tspc.tv_nsec = ((tv.tv_usec * 1000) + timeToWait.tv_nsec) % 1000000000;
228 if (! this->_queue.empty()) {
229 value = this->_queue.front();
230 this->_queue.pop_front();
246 if (! this->_queue.empty()) {
247 value = this->_queue.back();
248 this->_queue.pop_back();
264 while (this->_queue.empty()) {
265 if (_cv.
Wait(this->_mutex)) {
282 if (this->_queue.empty()) {
284 if (! this->_queue.empty()) {
300 return(this->_queue.empty());
309 random_shuffle(this->_queue.begin(), this->_queue.end());
318 uint32_t Copy(std::deque<_ValueType> & c)
325 typename std::deque<_ValueType>::iterator iter = this->_queue.begin();
326 for ( ; iter != this->_queue.end(); ++iter) {
337 std::deque<_ValueType> _queue;
346 return(_mutex.
Lock());
364 #endif // _DWMPTHREADQUEUE_HH_ This template provides inter-thread first-in first-out (FIFO) queueing.
Definition: DwmPthreadQueue.hh:69
bool TimedWaitForNotEmpty(const struct timespec &timeToWait)
Waits timeToWait for the queue to be non-empty.
Definition: DwmPthreadQueue.hh:279
Dwm::Pthread::Mutex class definition.
bool Empty()
Returns true if the queue is empty, else returns false.
Definition: DwmPthreadQueue.hh:298
bool Unlock()
Unlock the mutex. Returns true on success.
This class just encapsulates a condition variable (from the pthread library), with default attributes...
Definition: DwmConditionVariable.hh:59
bool PopBack(_ValueType &value)
Pops the entry from the back of the queue and stores it in value.
Definition: DwmPthreadQueue.hh:242
Queue()
Constructor.
Definition: DwmPthreadQueue.hh:75
bool PushBack(const _ValueType &value)
Inserts value on the back of the queue.
Definition: DwmPthreadQueue.hh:117
uint32_t MaxLength() const
Returns the max length of the queue.
Definition: DwmPthreadQueue.hh:98
bool Signal()
Unblocks one thread waiting for the condition variable.
bool ConditionTimedWait(const struct timespec &timeToWait)
Waits for the condition variable to be signalled or broadcasted for timeToWait to pass...
Definition: DwmPthreadQueue.hh:202
Dwm::SysLogger class definition and Syslog() macro.
bool Broadcast()
Unblocks all threads waiting for the condition variable.
static bool Log(int priority, const char *message,...)
Just like syslog(), takes a priority and a format string and variable list of arguments.
Definition: DwmBZ2IO.hh:67
bool Lock()
Lock the mutex.
~Queue()
Destructor.
Definition: DwmPthreadQueue.hh:84
bool TimedWait(Mutex &mutex, const struct timespec *abstime)
Atomically blocks the calling thread waiting on the condition variable, and unblocks the mutex specif...
Dwm::Pthread::ConditionVariable class definition.
bool ConditionSignal()
Unblocks at least one thread waiting on the condition variable.
Definition: DwmPthreadQueue.hh:179
bool ConditionWait()
Waits for the condition variable to be signalled or broadcasted.
Definition: DwmPthreadQueue.hh:187
This class just encapsulates a pthread_mutex_t (from the pthread library).
Definition: DwmMutex.hh:67
bool WaitForNotEmpty()
Blocks the calling thread until the queue contains at least one entry.
Definition: DwmPthreadQueue.hh:260
bool Wait(Mutex &mutex)
Atomically blocks the current thread waiting on the condition variable, and unblocks the mutex specif...
bool PopFront(_ValueType &value)
Pops the entry from the front of the queue and stores it in value.
Definition: DwmPthreadQueue.hh:224
bool PushFront(const _ValueType &value)
Inserts value on the front of the queue.
Definition: DwmPthreadQueue.hh:161
uint32_t MaxLength(uint32_t maxLength)
Sets and returns the max length of the queue.
Definition: DwmPthreadQueue.hh:107