EpcTools
An event based multi-threaded C++ development framework.
eqbase.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 __eqbase_h_included
19 #define __eqbase_h_included
20 
21 #include "ebase.h"
22 #include "eerror.h"
23 #include "esynch.h"
24 #include "etime.h"
25 #include "etimer.h"
26 //#include "etq.h"
27 #include "emsg.h"
28 
38 
41 
43 DECLARE_ERROR(EQueueBaseError_UnInitialized);
44 DECLARE_ERROR(EQueueBaseError_NotOpenForWriting);
45 DECLARE_ERROR(EQueueBaseError_NotOpenForReading);
46 DECLARE_ERROR(EQueueBaseError_MultipleReadersNotAllowed);
48 
51 
52 class EQueueBase;
53 
55 class EQueueMessage : public EMessage
56 {
57  friend class EQueueBase;
58 
59 public:
62  {
63  }
64 
67  {
68  }
69 
74  {
75  m_timer = a.m_timer;
76  m_msgType = a.m_msgType;
77  return *this;
78  }
79 
82  virtual Void getLength(ULong &length);
87  virtual Void serialize(pVoid pBuffer, ULong &nOffset);
92  virtual Void unserialize(pVoid pBuffer, ULong &nOffset);
93 
97  {
98  return m_timer;
99  }
100 
103  Void setMsgType(Long msgType) { m_msgType = msgType; }
106  Long getMsgType() { return m_msgType; }
107 
108 private:
109  ETime m_timer;
110  Long m_msgType;
111 };
112 
115 
116 class EQueuePublic;
117 class EQueuePrivate;
118 
121 {
122  friend class EQueuePublic;
123  friend class EQueuePrivate;
124 
125 public:
127  enum Mode
128  {
135  };
136 
143  Bool push(EQueueMessage &msg, Bool wait = True);
147  EQueueMessage *pop(Bool wait = True);
148 
150  Void destroy();
151 
152 protected:
154  virtual Bool isPublic() = 0;
155  virtual ULong &msgSize() = 0;
156  virtual Int &msgCnt() = 0;
157  virtual Long &msgHead() = 0;
158  virtual Long &msgTail() = 0;
159  virtual Bool &multipleReaders() = 0;
160  virtual Bool &multipleWriters() = 0;
161  virtual Int &numReaders() = 0;
162  virtual Int &numWriters() = 0;
163  virtual Int &refCnt() = 0;
164  virtual pChar data() = 0;
165  virtual Void allocDataSpace(cpStr sFile, Char cId, Int nSize) = 0;
166  virtual Void initReadMutex() = 0;
167  virtual Void initWriteMutex() = 0;
168  virtual Void initSemFree(UInt initialCount) = 0;
169  virtual Void initSemMsgs(UInt initialCount) = 0;
170 
171  virtual EMutexData &readMutex() = 0;
172  virtual EMutexData &writeMutex() = 0;
173  virtual ESemaphoreData &semMsgs() = 0;
174  virtual ESemaphoreData &semFree() = 0;
175 
176  virtual EQueueMessage *allocMessage(Long msgType) = 0;
177 
178  Mode mode() { return m_mode; }
179 
180  EQueueBase();
181  ~EQueueBase();
182 
183  Void init(ULong nMsgSize, Int nMsgCnt, Int queueId, Bool bMultipleReaders,
184  Bool bMultipleWriters, EQueueBase::Mode eMode);
186 
187 private:
188  Bool m_initialized;
189  Mode m_mode;
190  Char m_rBuffer[USHRT_MAX];
191  Char m_wBuffer[USHRT_MAX];
192 };
193 
196 
197 #if 0
198 class PointerMessage
199 {
200  friend class PointerQueue;
201 public:
202  virtual PointerMessage &operator=(const PointerMessage &msg) { tmr_=msg.tmr_; ptr_=msg.ptr_; return *this; }
203  epctime_t queueMicroSeconds() { return tmr_.MicroSeconds(); }
204  epctime_t queueMilliSeconds() { return tmr_.MilliSeconds(); }
205  pVoid pointer() { return ptr_; }
206  PointerMessage &pointer(pVoid p) { ptr_=p; return *this; }
207  Void start() { tmr_.Start(); }
208  Void stop() { tmr_.Stop(); }
209 private:
210  ETimer tmr_;
211  pVoid ptr_;
212 };
213 
214 class PointerQueue
215 {
216 public:
217  PointerQueue();
218  ~PointerQueue();
219 
220  size_t maxPages() const { return mp_; }
221  size_t messageSize() const { return ms_; }
222  UShort pageSize() const { return ps_; }
223 
224  PointerQueue &init(size_t ms, UShort ps, size_t mp_);
225 
226  Bool push(pVoid ptr, Bool wait=True);
227  pVoid pop(Bool wait=True);
228 
229 private:
230  struct Page
231  {
232  Page *prevPage;
233  Page *nextPage;
234  pUChar data;
235  };
236 
237  class PageAddress
238  {
239  public:
240  PageAddress() : pg_(nullptr), ofs_(0) {}
241  PageAddress(Page &page, UShort offset) : pg_(&page), ofs_(offset) {}
242  PageAddress(const PageAddress &addr) : pg_(addr.pg_), ofs_(addr.ofs_) {}
243 
244  PageAddress &operator=(const PageAddress &addr) { pg_=addr.pg_; ofs_=addr.ofs_; return *this; }
245  Bool operator==(const PageAddress &addr) { return pg_==addr.pg_ && ofs_==addr.ofs_; }
246 
247  Page &page() { return *pg_; }
248  PageAddress &page(Page &p) { pg_=&p; return *this; }
249  PageAddress &page(Page *p) { pg_=p; return *this; }
250  UShort offset() const { return ofs_; }
251  PageAddress &offset(UShort ofs) { ofs_=ofs; return *this; }
252 
253  Page &nextPage() { return *pg_->nextPage; }
254  Page &prevPage() { return *pg_->prevPage; }
255 
256  private:
257  Page *pg_;
258  UShort ofs_;
259  };
260 
261  std::atomic_int rc_; // reference count
262  std::atomic_int nr_; // number of readers
263  std::atomic_int nw_; // number of writers
264 
265  size_t mp_; // max pages
266  size_t ms_; // message size
267  UShort ps_; // page size (entries per page)
268  Page *head_; // write to the head
269  Page *tail_; // read from the tail
270 
271  EMutexPrivate rmtx_;
272  EMutexPrivate wmtx_;
273  ESemaphorePrivate free_;
274  ESemaphorePrivate used_;
275 
276  // write to the head, read from the tail
277  // if no more space in the current page and the next page contains the tail, create a new page
278 };
279 #endif
280 
281 #endif // #define __eqbase_h_included
Void setMsgType(Long msgType)
Assigns the message type value.
Definition: eqbase.h:103
Void destroy()
Destroys the message queue.
Definition: eqbase.cpp:121
provivdes read only access
Definition: eqbase.h:130
#define True
True.
Definition: ebase.h:25
Macros for various standard C library functions and standard includes.
Classes used to encode and decode messages sent to and recieved from a message queue.
~EQueueMessage()
Class destructor.
Definition: eqbase.h:66
virtual Void unserialize(pVoid pBuffer, ULong &nOffset)
Deserializes the message.
Definition: eqbase.cpp:36
Allows read or write access.
Void init(Int nMsgSize, Int nMsgCnt, Int queueId, Bool bMultipleReaders, Bool bMultipleWriters, EQueueBase::Mode eMode)
Initializes the private message queue.
Definition: eqpriv.h:42
Implements a stopwatch style timer.
Definition: etimer.h:26
Contains the data associated with a public or private mutex.
Definition: esynch.h:72
Provides class for manipulating time of day values.
Contains the data associated with a public or private semaphore.
Definition: esynch.h:268
virtual Void getLength(ULong &length)
Calculates the length of the message.
Definition: eqbase.cpp:24
EQueueMessage & operator=(const EQueueMessage &a)
Assignment operator.
Definition: eqbase.h:73
Bool push(EQueueMessage &msg, Bool wait=True)
Writes a message to the queue.
Definition: eqbase.cpp:154
EQueueMessage * pop(Bool wait=True)
Retrieves the next message from the queue.
Definition: eqbase.cpp:224
friend class EQueueBase
Definition: eqbase.h:57
Class for manipulating date and time of day values.
Definition: etime.h:199
Defines base class for exceptions and declaration helper macros.
The private queue class.
Definition: eqpriv.h:27
virtual Void serialize(pVoid pBuffer, ULong &nOffset)
Serializes the message.
Definition: eqbase.cpp:30
ETime & getTimer()
Retrieves the message lifetime timer.
Definition: eqbase.h:96
provides write only access
Definition: eqbase.h:132
Definition: eqpub.h:36
The message queue base class.
Definition: eqbase.h:120
#define DECLARE_ERROR(__e__)
Declares exception class derived from EError with no constructor parameters.
Definition: eerror.h:53
Long getMsgType()
Retrieves the message type value.
Definition: eqbase.h:106
Represents a private semaphore, the semaphore data is allocated from either the stack or heap...
Definition: esynch.h:382
Mode
The queue access modes.
Definition: eqbase.h:127
EQueueMessage()
Default constructor.
Definition: eqbase.h:61
A private mutex (the mutex data is allocated from either the heap or stack).
Definition: esynch.h:175
The message queue base message class.
Definition: emsg.h:29
Contains definitions for synchronization objects.
long long int epctime_t
time typedef
Definition: ebase.h:34
Represents a message to be written to/read from a message queue.
Definition: eqbase.h:55