EpcTools
An event based multi-threaded C++ development framework.
etq.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2009-2019 Brian Waters
3 * Copyright (c) 2019 Sprint
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 
18 #ifndef __etq_h_included
19 #define __etq_h_included
20 
23 
24 #include "ebase.h"
25 #include "eerror.h"
26 #include "etimer.h"
27 #include "esynch.h"
28 #include "esynch2.h"
29 #include "eshmem.h"
30 
33 
35 DECLARE_ERROR(EThreadQueueBaseError_NotOpenForWriting);
36 DECLARE_ERROR(EThreadQueueBaseError_NotOpenForReading);
37 DECLARE_ERROR(EThreadQueueBaseError_MultipleReadersNotAllowed);
38 
39 DECLARE_ERROR(EThreadQueuePublicError_UnInitialized);
41 
44 
45 class EThreadQueueBase;
46 
48 class EThreadMessage
49 {
50  friend class EThreadQueueBase;
51 
52 protected:
54  typedef union _etmessage_data {
55  struct
56  {
57  Dword lowPart;
58  Long highPart;
59  } u;
60  pVoid voidPtr;
61  LongLong quadPart;
62  UChar bytes[sizeof(LongLong)];
63  } etmessage_data_t;
65 
66 public:
69  {
70  m_msgid = 0;
71  m_data.quadPart = 0;
72  }
75  EThreadMessage(UInt msgid)
76  {
77  m_msgid = msgid;
78  m_data.quadPart = 0;
79  }
84  EThreadMessage(UInt msgid, Dword lowPart, Long highPart)
85  {
86  m_msgid = msgid;
87  m_data.u.lowPart = lowPart;
88  m_data.u.highPart = highPart;
89  }
93  EThreadMessage(UInt msgid, pVoid voidPtr)
94  {
95  m_msgid = msgid;
96  m_data.voidPtr = voidPtr;
97  }
101  EThreadMessage(UInt msgid, LongLong quadPart)
102  {
103  m_msgid = msgid;
104  m_data.quadPart = quadPart;
105  }
109  {
110  m_msgid = msg.m_msgid;
111  m_data.quadPart = msg.m_data.quadPart;
112  }
115  {
116  }
117 
122  {
123  m_msgid = val.m_msgid;
124  m_timer = val.m_timer;
125  m_data.quadPart = val.m_data.quadPart;
126 
127  return *this;
128  }
129 
132  Void set(UInt msgid)
133  {
134  m_msgid = msgid;
135  m_data.quadPart = 0;
136  }
141  Void set(UInt msgid, Dword lowPart, Long highPart)
142  {
143  m_msgid = msgid;
144  m_data.u.lowPart = lowPart;
145  m_data.u.highPart = highPart;
146  }
150  Void set(UInt msgid, pVoid voidPtr)
151  {
152  m_msgid = msgid;
153  m_data.voidPtr = voidPtr;
154  }
158  Void set(UInt msgid, LongLong quadPart)
159  {
160  m_msgid = msgid;
161  m_data.quadPart = quadPart;
162  }
163 
167  {
168  return m_timer;
169  }
172  UInt &getMsgId()
173  {
174  return m_msgid;
175  }
178  Dword &getLowPart()
179  {
180  return m_data.u.lowPart;
181  }
184  Long &getHighPart()
185  {
186  return m_data.u.highPart;
187  }
190  LongLong &getQuadPart()
191  {
192  return m_data.quadPart;
193  }
196  pVoid &getVoidPtr()
197  {
198  return m_data.voidPtr;
199  }
200 
201 private:
202  ETimer m_timer;
203  UInt m_msgid;
204  etmessage_data_t m_data;
205 };
206 
209 
211 
212 class EThreadMessageQueuePublic;
213 class EThreadMessageQueuePrivate;
214 class EThreadBase;
215 
216 class EThreadQueueBase
217 {
218  friend class EThreadMessageQueuePublic;
219  friend class EThreadMessageQueuePrivate;
220  friend class EThreadBase;
221 
222 public:
223  enum Mode
224  {
225  ReadOnly,
226  WriteOnly,
227  ReadWrite
228  };
229 
230  Bool push(UInt msgid, Bool wait = True);
231  Bool push(UInt msgid, Dword lowPart, Long highPart, Bool wait = True);
232  Bool push(UInt msgid, pVoid voidPtr, Bool wait = True);
233  Bool push(UInt msgid, LongLong quadPart, Bool wait = True);
234 
235  Bool pop(EThreadMessage &msg, Bool wait = True);
236  Bool peek(EThreadMessage &msg, Bool wait = True);
237 
238  Bool isInitialized() { return m_initialized; }
239  Mode mode() { return m_mode; }
240 
241 protected:
242  Bool push(UInt msgid, EThreadMessage::etmessage_data_t &d, Bool wait = True);
243 
244  virtual Bool isPublic() = 0;
245  virtual Int &msgCnt() = 0;
246  virtual Int &msgHead() = 0;
247  virtual Int &msgTail() = 0;
248  virtual Bool &multipleWriters() = 0;
249  virtual Int &numReaders() = 0;
250  virtual Int &numWriters() = 0;
251  virtual Int &refCnt() = 0;
252  virtual EThreadMessage *data() = 0;
253  virtual Void allocDataSpace(cpStr sFile, Char cId, Int nSize) = 0;
254  virtual Void initMutex() = 0;
255  virtual Void initSemFree(UInt initialCount) = 0;
256  virtual Void initSemMsgs(UInt initialCount) = 0;
257 
258  virtual EMutexData &mutex() = 0;
259  virtual ESemaphoreData &semMsgs() = 0;
260  virtual ESemaphoreData &semFree() = 0;
261 
263  ~EThreadQueueBase();
264 
265  Void init(Int nMsgCnt, Int threadId, Bool bMultipleWriters,
266  EThreadQueueBase::Mode eMode);
267  Void destroy();
268 
269 private:
270  static Bool m_debug;
271  Bool m_initialized;
272  Mode m_mode;
273 };
274 
277 
279 {
280 public:
283 
284  Void init(Int nMsgCnt, Int threadId, Bool bMultipleWriters,
285  EThreadQueueBase::Mode eMode)
286  {
287  EThreadQueueBase::init(nMsgCnt, threadId, bMultipleWriters, eMode);
288  }
289 
290 protected:
291  Bool isPublic() { return True; }
292  Int &msgCnt() { return m_pCtrl->m_msgCnt; }
293  Int &msgHead() { return m_pCtrl->m_head; }
294  Int &msgTail() { return m_pCtrl->m_tail; }
295  Bool &multipleWriters() { return m_pCtrl->m_multipleWriters; }
296  Int &numReaders() { return m_pCtrl->m_numReaders; }
297  Int &numWriters() { return m_pCtrl->m_numWriters; }
298  Int &refCnt() { return m_pCtrl->m_refCnt; }
299  EThreadMessage *data() { return m_pData; }
300  Void allocDataSpace(cpStr sFile, Char cId, Int nSize);
301  Void initMutex();
302  Void initSemFree(UInt initialCount);
303  Void initSemMsgs(UInt initialCount);
304 
305  EMutexData &mutex() { return ESynchObjects::getMutex(m_pCtrl->m_mutexid); }
306  ESemaphoreData &semFree() { return ESynchObjects::getSemaphore(m_pCtrl->m_freeSemId); }
307  ESemaphoreData &semMsgs() { return ESynchObjects::getSemaphore(m_pCtrl->m_msgsSemId); }
308 
309 private:
310  typedef struct
311  {
312  Int m_refCnt;
313  Int m_numReaders;
314  Int m_numWriters;
315  Bool m_multipleWriters;
316 
317  Int m_msgCnt;
318  Int m_head; // next location to write
319  Int m_tail; // next location to read
320 
321  Int m_mutexid;
322  Int m_freeSemId;
323  Int m_msgsSemId;
324  } ethreadmessagequeue_ctrl_t;
325 
326  ESharedMemory m_sharedmem;
327  ethreadmessagequeue_ctrl_t *m_pCtrl;
328  EThreadMessage *m_pData;
329 };
330 
333 
335 {
336 public:
339 
340  Void init(Int nMsgCnt, Int threadId, Bool bMultipleWriters,
341  EThreadQueueBase::Mode eMode)
342  {
343  EThreadQueueBase::init(nMsgCnt, threadId, bMultipleWriters, eMode);
344  }
345 
346 protected:
347  Bool isPublic() { return False; }
348  Int &msgCnt() { return m_msgCnt; }
349  Int &msgHead() { return m_head; }
350  Int &msgTail() { return m_tail; }
351  Bool &multipleWriters() { return m_multipleWriters; }
352  Int &numReaders() { return m_numReaders; }
353  Int &numWriters() { return m_numWriters; }
354  Int &refCnt() { return m_refCnt; }
355  EThreadMessage *data() { return m_pData; }
356  Void allocDataSpace(cpStr sFile, Char cId, Int nSize);
357  Void initMutex();
358  Void initSemFree(UInt initialCount);
359  Void initSemMsgs(UInt initialCount);
360 
361  EMutexData &mutex() { return m_mutex; }
362  ESemaphoreData &semFree() { return m_semFree; }
363  ESemaphoreData &semMsgs() { return m_semMsgs; }
364 
365 private:
366  Int m_refCnt;
367  Int m_numReaders;
368  Int m_numWriters;
369  Bool m_multipleWriters;
370 
371  Int m_msgCnt;
372  Int m_head; // next location to write
373  Int m_tail; // next location to read
374 
375  EMutexPrivate m_mutex;
376  ESemaphorePrivate m_semFree;
377  ESemaphorePrivate m_semMsgs;
378 
379  EThreadMessage *m_pData;
380 };
381 
383 
386 
387 #endif // #define __etq_h_included
EThreadMessage::EThreadMessage
EThreadMessage()
Default constructor.
Definition: etq.h:68
DECLARE_ERROR
#define DECLARE_ERROR(__e__)
Declares exception class derived from EError with no constructor parameters.
Definition: eerror.h:53
EThreadQueuePublic
Definition of a public event thread message queue.
Definition: etevent.h:566
EThreadMessage::getLowPart
Dword & getLowPart()
Retrieves the unsigned 32-bit value associated with this message.
Definition: etq.h:178
eerror.h
Defines base class for exceptions and declaration helper macros.
ESemaphoreData
Contains the data associated with a public or private semaphore.
Definition: esynch.h:268
EThreadMessage::getHighPart
Long & getHighPart()
Retrieves the signed 32-bit value associated with this message.
Definition: etq.h:184
EThreadQueueBase
Defines the functionality for the thread queue.
Definition: etevent.h:359
EThreadMessage::getTimer
ETimer & getTimer()
Retrieves the ETimer object associated with this event.
Definition: etq.h:166
True
#define True
True.
Definition: ebase.h:25
EThreadMessage
An event message that is to be sent to a thread.
Definition: etevent.h:266
EThreadMessage::set
Void set(UInt msgid)
Assigns values to this message object.
Definition: etq.h:132
EThreadQueueBase::pop
Bool pop(T &msg, Bool wait=True)
Removes the next message from the thread event queue.
Definition: etevent.h:398
eshmem.h
Defines a class for access to shared memory.
ebase.h
Macros for various standard C library functions and standard includes.
EThreadMessage::getQuadPart
LongLong & getQuadPart()
Retrieves the signed 64-bit value associated with this message.
Definition: etq.h:190
EThreadQueuePrivate
Definition of a private event thread message queue.
Definition: etevent.h:670
EMutexPrivate
A private mutex (the mutex data is allocated from either the heap or stack).
Definition: esynch.h:175
EThreadQueuePrivate::~EThreadQueuePrivate
~EThreadQueuePrivate()
Class destructor.
Definition: etevent.h:689
EThreadQueueBase::push
Bool push(const T &msg, Bool wait=True)
Adds the specified message to the thread event queue.
Definition: etevent.h:369
esynch.h
Contains definitions for synchronization objects.
EThreadQueueMode::ReadWrite
Allows read or write access.
EThreadMessage::~EThreadMessage
~EThreadMessage()
Class destructor.
Definition: etq.h:114
etimer.h
EThreadMessage::getVoidPtr
pVoid & getVoidPtr()
Retrieves the void pointer value associated with this message.
Definition: etq.h:196
esynch2.h
ETimer
Implements a stopwatch style timer.
Definition: etimer.h:26
EThreadQueueBase::mode
EThreadQueueMode mode()
Retrieves the access mode associated with this queue object.
Definition: etevent.h:443
EThreadMessage::EThreadMessage
EThreadMessage(UInt msgid, Dword lowPart, Long highPart)
Class constructor.
Definition: etq.h:84
False
#define False
False.
Definition: ebase.h:27
EThreadQueuePublic::init
Void init(Int nMsgCnt, Int threadId, Bool bMultipleWriters, EThreadQueueMode eMode)
Initializes this public event thead message queue object.
Definition: etevent.h:588
EThreadQueueMode::ReadOnly
Allows read only access.
ESemaphorePrivate
Represents a private semaphore, the semaphore data is allocated from either the stack or heap.
Definition: esynch.h:382
EThreadQueueBase::isInitialized
Bool isInitialized()
Retrieves indication if this queue object has been initialized.
Definition: etevent.h:440
EThreadQueuePrivate::init
Void init(Int nMsgCnt, Int threadId, Bool bMultipleWriters, EThreadQueueMode eMode)
Initializes this public event thead message queue object.
Definition: etevent.h:705
EThreadQueueBase::peek
Bool peek(T &msg, Bool wait=True)
Retrievees the next message from the thread event queue without removing the message from the queue.
Definition: etevent.h:421
EThreadMessage::operator=
EThreadMessage & operator=(const EThreadMessage &val)
Assignment operator.
Definition: etq.h:121
EThreadMessage::EThreadMessage
EThreadMessage(UInt msgid, LongLong quadPart)
Class constructor.
Definition: etq.h:101
EThreadQueuePublic::~EThreadQueuePublic
~EThreadQueuePublic()
Class destructor.
Definition: etevent.h:577
ESharedMemory
The shared memory access class.
Definition: eshmem.h:43
EThreadMessage::EThreadMessage
EThreadMessage(UInt msgid, pVoid voidPtr)
Class constructor.
Definition: etq.h:93
EThreadMessage::set
Void set(UInt msgid, pVoid voidPtr)
Assigns values to this message object.
Definition: etq.h:150
EThreadQueuePrivate::EThreadQueuePrivate
EThreadQueuePrivate()
Default constructor.
Definition: etevent.h:675
EThreadMessage::getMsgId
UInt & getMsgId()
Retrieves the message ID associated with this message.
Definition: etq.h:172
EThreadMessage::EThreadMessage
EThreadMessage(const EThreadMessage &msg)
Copy constructor.
Definition: etq.h:108
EThreadMessage::set
Void set(UInt msgid, LongLong quadPart)
Assigns values to this message object.
Definition: etq.h:158
EThreadMessage::set
Void set(UInt msgid, Dword lowPart, Long highPart)
Assigns values to this message object.
Definition: etq.h:141
EThreadQueueMode::WriteOnly
Allows read only access.
EThreadMessage::EThreadMessage
EThreadMessage(UInt msgid)
Class constructor.
Definition: etq.h:75
EThreadQueuePublic::EThreadQueuePublic
EThreadQueuePublic()
Default constructor.
Definition: etevent.h:571
EMutexData
Contains the data associated with a public or private mutex.
Definition: esynch.h:72