Dynalib Utils
DynaWaitQueueImpl.h
Go to the documentation of this file.
1 #ifndef DYNAWAITQUEUEIMPL_H
2 #define DYNAWAITQUEUEIMPL_H
3 
4 #include <iostream>
5 #include <type_traits>
6 
7 #include "BitManip.h"
8 #include "DynaWaitQueue.h"
9 #include "CheckForError.h"
10 #include "Exception.h"
11 
12 #define MAKE_WAITQUEUETYPE_INSTANCE(C, T) \
13  template class DynaWaitQueue<C>; \
14  typedef DynaWaitQueue<C> T##WaitQueue
15 
16 
17 template <class T> DynaWaitQueue<T>::DynaWaitQueue(int maxSize) {
18  sem_init(&_empty, 0, maxSize); // MAX buffers are empty to begin with...
19  sem_init(&_full, 0, 0); // ... and 0 are full
20  sem_init(&_mutex, 0, 1); // mutex = 1 since it a lock
21  _queueList = new DynaList<T>();
22 }
23 
24 template <class T> bool DynaWaitQueue<T>::add(T* obj, long msecs) {
25  sem_wait(&_empty);
26  sem_wait(&_mutex); // scope of lock reduced to prevent deadlock
27  _queueList->push(obj);
28  sem_post(&_mutex);
29  sem_post(&_full);
30  return true;
31 }
32 
33 template <class T> T* DynaWaitQueue::next(long msecs) {
34  sem_wait(&_full);
35  sem_wait(&_mutex); // scope of lock reduced to prevent deadlock
36  T* obj = _queueList->popLast();
37  sem_post(&_mutex);
38  sem_post(&_empty);
39  return obj;
40 }
41 
42 
43 
44 
45 #endif
sem_t _full
Definition: DynaWaitQueue.h:15
sem_t _mutex
Definition: DynaWaitQueue.h:17
DynaWaitQueue(int maxSize)
Definition: DynaWaitQueueImpl.h:17
Definition: DynaList.h:38
sem_t _empty
Definition: DynaWaitQueue.h:16
DynaList< T > * _queueList
Definition: DynaWaitQueue.h:19
bool add(T *obj, long msecs)
Definition: DynaWaitQueueImpl.h:24
T * next(long msecs)
Definition: DynaWaitQueueImpl.h:33