EpcTools
An event based multi-threaded C++ development framework.
esynch2.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 __esynch2_h_included
19 #define __esynch2_h_included
20 
22 
23 #include "esynch.h"
24 #include "eshmem.h"
25 #include "eatomic.h"
26 #include "estatic.h"
27 
30 
31 DECLARE_ERROR(ESynchObjectsError_PublicObjectsNotEnabled);
32 DECLARE_ERROR_ADVANCED2(ESynchObjectsError_UnableToAllocateSynchObject);
33 DECLARE_ERROR(ESynchObjectsError_InvalidOffset);
34 
37 
38 class ESharedMemory;
39 
41 class ESynchObjects : public EStatic
42 {
44 public:
45  typedef struct
46  {
47  EMutexPrivate m_mutex;
48  Int m_max;
49  Int m_head;
50  Long m_currused;
51  Long m_maxused;
52  } _esynchcontrol_t;
53 
54  typedef struct
55  {
56  Bool m_initialized;
57  Long m_sequence;
58  _esynchcontrol_t m_semaphoreCtrl;
59  _esynchcontrol_t m_mutexCtrl;
60  } esynchcontrol_t;
61 
62  typedef struct
63  {
64  Char m_name[EPC_FILENAME_MAX];
65  Int m_queueid;
66  Int m_msgSize;
67  Int m_msgCnt;
68  Bool m_multipleReaders;
69  Bool m_multipleWriters;
70  } epublicqueuedef_t;
71 
72  ESynchObjects();
73  ~ESynchObjects();
74 
75  virtual Int getInitType() { return STATIC_INIT_TYPE_PRIORITY; }
76  Void init(EGetOpt &options);
77  Void uninit();
78 
79  Void logObjectUsage();
80 
81  static Int nextSemaphore();
82  static Int nextMutex();
83 
84  static Void freeSemaphore(Int nSemId);
85  static Void freeMutex(Int nMutexId);
86 
87  Long incSequence() { return atomic_inc(m_pCtrl->m_sequence); }
88 
89  epublicqueuedef_t *getPublicQueue(Int queueid)
90  {
91  for (int i = 0; m_pPubQueues[i].m_name[0] != 0; i++)
92  {
93  if (m_pPubQueues[i].m_queueid == queueid)
94  return &m_pPubQueues[i];
95  }
96 
97  return NULL;
98  }
99 
100  Void setPublicQueue(Int idx, cpChar pName, Int queueid, Int msgSize,
101  Int msgCnt, Bool multipleReaders, Bool multipleWriters)
102  {
103  epc_strcpy_s(m_pPubQueues[idx].m_name, sizeof(m_pPubQueues[idx].m_name), pName);
104  m_pPubQueues[idx].m_queueid = queueid;
105  m_pPubQueues[idx].m_msgSize = msgSize;
106  m_pPubQueues[idx].m_msgCnt = msgCnt;
107  m_pPubQueues[idx].m_multipleReaders = multipleReaders;
108  m_pPubQueues[idx].m_multipleWriters = multipleWriters;
109  }
110 
111  static ESynchObjects *getSynchObjCtrlPtr()
112  {
113  if (!m_pThis)
114  throw ESynchObjectsError_PublicObjectsNotEnabled();
115  return m_pThis;
116  }
117 
118  static ESemaphoreDataPublic &getSemaphore(Int ofs)
119  {
120  if (ofs < 0)
121  throw ESynchObjectsError_InvalidOffset();
122  return getSynchObjCtrlPtr()->m_pSemaphores[ofs - 1];
123  }
124 
125  static EMutexDataPublic &getMutex(Int ofs)
126  {
127  if (ofs < 0)
128  throw ESynchObjectsError_InvalidOffset();
129  return getSynchObjCtrlPtr()->m_pMutexes[ofs - 1];
130  }
132 
133 private:
134  class ESynchObjectsSharedMemory : public ESharedMemory
135  {
136  friend class ESynchObjects;
137 
138  protected:
139  Void onDestroy();
140  Void setSynchObjectsPtr(ESynchObjects *p);
141 
142  private:
143  ESynchObjects *m_pSynchObjects;
144  };
145 
146  ESynchObjectsSharedMemory m_sharedmem;
147  esynchcontrol_t *m_pCtrl;
148  ESemaphoreDataPublic *m_pSemaphores;
149  EMutexDataPublic *m_pMutexes;
150  epublicqueuedef_t *m_pPubQueues;
151 
152  static ESynchObjects *m_pThis;
153 };
154 
155 #endif // #define __esynch2_h_included
epc_strcpy_s
#define epc_strcpy_s(strDestination, sizeInBytes, strSource)
epc_strcpy_s - strncpy
Definition: ebase.h:50
atomic_inc
#define atomic_inc(a)
atomic increment - increments a by 1
Definition: eatomic.h:27
estatic.h
Performs static initialization associated with any EpcTools class that requires it....
EStatic::uninit
virtual Void uninit()
Performs uninitialization at system shutdown.
Definition: estatic.h:63
eshmem.h
Defines a class for access to shared memory.
EStatic
Any EpcTools that needs to have initialization performed as part of EpcTools::Initialize() should der...
Definition: estatic.h:51
EMutexPrivate
A private mutex (the mutex data is allocated from either the heap or stack).
Definition: esynch.h:175
esynch.h
Contains definitions for synchronization objects.
EGetOpt
Definition: egetopt.h:31
eatomic.h
Macros for performing CPU atomic/interlaced operations.
ESharedMemory
The shared memory access class.
Definition: eshmem.h:43
DECLARE_ERROR_ADVANCED2
DECLARE_ERROR_ADVANCED2(ESynchObjectsError_UnableToAllocateSynchObject)
EPC_FILENAME_MAX
#define EPC_FILENAME_MAX
maximum file name length
Definition: ebase.h:37
ESynchObjects
Used internally by EpcTools for managing access to public (shared memory) objects.
Definition: esynch2.h:41
EStatic::init
virtual Void init(EGetOpt &opt)
Performs class specific initialization.
Definition: estatic.h:61
EStatic::getInitType
virtual Int getInitType()
Definition: estatic.h:57
DECLARE_ERROR
DECLARE_ERROR(ESynchObjectsError_PublicObjectsNotEnabled)