sttcl  v0.9c
STTCL C++ template state machine framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EventQueue.h
Go to the documentation of this file.
1 
25 #ifndef EVENTQUEUE_H_
26 #define EVENTQUEUE_H_
27 
28 #include "SttclTime.h"
29 #include "SttclSemaphore.h"
30 #include "SttclMutex.h"
31 #if !defined(STTCL_USE_STL)
32 #else
33 #include <deque>
34 #endif
35 
36 #if defined(STTCL_USE_STL)
37 #if !defined(STTCL_DEFAULT_DEQUEIMPL)
38 #define STTCL_DEFAULT_DEQUEIMPL(__T__) std::deque<__T__>
39 #endif
40 #endif
41 
42 #if !defined(STTCL_DEFAULT_DEQUEIMPL)
43 #error "You need to define a default double ended queue implementation for sttcl"
44 #endif
45 
46 #define EAIGNORE_BEGIN
47 #define EAIGNORE_END
48 
49 namespace sttcl
50 {
51 
55 template
56 < class T
57 , class TimeDurationType = TimeDuration<STTCL_DEFAULT_TIMEDURATIONIMPL>
61 , class InnerQueueType = STTCL_DEFAULT_DEQUEIMPL(T)
63 >
65 {
66 public:
71  : eventQueue()
72  {
73  }
78  {
79  }
80 
85  void push_back(const T& event)
86  {
87  { sttcl::internal::AutoLocker<MutexType> lock(eventQueueMutex);
88  eventQueue.push_back(event);
89  }
90  eventsAvailable.post();
91  }
92 
97  T& front()
98  {
99  sttcl::internal::AutoLocker<MutexType> lock(eventQueueMutex);
100  return eventQueue.front();
101  }
102 
106  void pop_front()
107  {
108  sttcl::internal::AutoLocker<MutexType> lock(eventQueueMutex);
109  eventQueue.pop_front();
110  }
111 
117  {
118  eventsAvailable.wait();
119  return !empty();
120  }
121 
127  bool waitForEvents(TimeDurationType timeout)
128  {
129  return eventsAvailable.try_wait(timeout) && !empty();
130  }
131 
136  bool empty()
137  {
138  sttcl::internal::AutoLocker<MutexType> lock(eventQueueMutex);
139  return eventQueue.empty();
140  }
141 
145  void unblock()
146  {
147  eventsAvailable.post();
148  }
149 private:
152 
153  InnerQueueType eventQueue;
154  MutexType eventQueueMutex;
155  SemaphoreType eventsAvailable;
156 };
157 
158 
159 }
160 
161 
162 
163 #endif /* EVENTQUEUE_H_ */