Synchronizer.h
6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Synchronizer.h
*
* Created on: 15 gru 2018
* Author: mariuszo
*/
#ifndef SYNCHRONIZER_H_
#define SYNCHRONIZER_H_
#include <errno.h>
#include <assert.h>
#include <pthread.h>
#include <semaphore.h>
#include <map>
#include <mutex>
#include "wrflock.h"
#include "pthread_helpers.h"
#include "enums.h"
/*
#include <linux/limits.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
#include <string>
#include <atomic>
#include <stdint.h>
#include <memory>
#include <iostream>
*/
#ifndef NS_IN_S
#define NS_IN_S 1000000000
#endif
struct syncSlot {
wrflock_t lock;
off_t dataSlotID;
};
struct synchronizerData {
sem_t lock; //lock for data blocks access
pthread_rwlock_t consumersLock; //each consumer do rdlock to prevent producer finish prematurely
std::atomic<off_t> writeSlotID;
};
class SynchronizerBase {
static std::map<std::string, SynchronizerBase*> synchronizersMap;
static std::mutex synchronizersMapLock;
const std::string m_name;
size_t m_slotsRingBufferSize;
syncSlot* m_syncSlotsRingBuffer;
synchronizerData* m_synchronizerData;
protected:
SynchronizerBase(const std::string& name);
virtual ~SynchronizerBase();
void create();
void attach();
void destroy();
inline void setWriteSlotID(const off_t& slotID) {
m_synchronizerData->writeSlotID.store(slotID, std::memory_order_release);
}
inline const off_t getWriteSlotID() const {
assert(m_synchronizerData->writeSlotID>=0);
return m_synchronizerData->writeSlotID.load(std::memory_order_acquire);
}
bool consumersActiveLock(){
return rdLock(m_synchronizerData->consumersLock,0)==0;
}
bool consumersActiveUnlock(){
return rwUnlock(m_synchronizerData->consumersLock)==0;
}
bool consumersInactiveLock(const uint64_t& timeoutns=UINT64_MAX){
return wrLock(m_synchronizerData->consumersLock, timeoutns)==0;
}
bool consumersInactiveUnlock(){
return rwUnlock(m_synchronizerData->consumersLock)==0;
}
public:
inline const size_t& slotsRingBufferSize() const {
return m_slotsRingBufferSize;
}
inline void setQueueSlot(const off_t& slotID, const off_t& blockID) noexcept {
m_syncSlotsRingBuffer[slotID].dataSlotID=blockID;
}
inline syncSlot& getQueueSlot(const off_t& slotID) const noexcept {
return m_syncSlotsRingBuffer[slotID];
}
inline synchronizerData& getSynchronizerData() const noexcept {
return *m_synchronizerData;
}
inline void lockSynchronizerData() noexcept {
semWait(m_synchronizerData->lock);
}
inline void unlockSynchronizerData() noexcept {
semPost(m_synchronizerData->lock);
}
inline bool wacquire(const off_t& slotID) noexcept {
return wrflock_wacquire(&(m_syncSlotsRingBuffer[slotID].lock))>=0;
}
inline bool racquire(const off_t& slotID) noexcept {
return wrflock_racquire(&(m_syncSlotsRingBuffer[slotID].lock))>=0;
}
inline bool facquire(const off_t& slotID) noexcept {
return wrflock_facquire(&(m_syncSlotsRingBuffer[slotID].lock))>=0;
}
inline bool wrelease(const off_t& slotID) noexcept {
return wrflock_wrelease(&(m_syncSlotsRingBuffer[slotID].lock))>=0;
}
inline bool rrelease(const off_t& slotID) noexcept {
return wrflock_rrelease(&(m_syncSlotsRingBuffer[slotID].lock))>=0;
}
inline bool frelease(const off_t& slotID) noexcept {
return wrflock_frelease(&(m_syncSlotsRingBuffer[slotID].lock))>=0;
}
inline bool wwait(const off_t& slotID, const uint64_t& timeoutns=UINT64_MAX) noexcept {
if (timeoutns==UINT64_MAX){
while (1) {
if (wrflock_wwait(&(m_syncSlotsRingBuffer[slotID].lock))<0){
if (errno==EINTR)
continue;
return false;
}
return true;
};
} else if (timeoutns==0) {
while(1){
if (wrflock_wtrywait(&(m_syncSlotsRingBuffer[slotID].lock))<0){
return false;
}
return true;
}
} else {
timespec time;
clock_gettime(CLOCK_REALTIME, &time);
time.tv_nsec+=timeoutns;
time.tv_sec+=(time.tv_nsec/NS_IN_S);
time.tv_nsec%=NS_IN_S;
while (1) {
if (wrflock_wtimewait(&(m_syncSlotsRingBuffer[slotID].lock),&time)<0){
if (errno==EINTR)
continue;
return false;
}
return true;
}
}
}
inline bool rwait(const off_t& slotID, const uint64_t& timeoutns=UINT64_MAX) noexcept {
if (timeoutns==UINT64_MAX){
while (1) {
if (wrflock_rwait(&(m_syncSlotsRingBuffer[slotID].lock))<0){
if (errno==EINTR)
continue;
return false;
}
return true;
}
} else if (timeoutns==0) {
while(1){
if (wrflock_rtrywait(&(m_syncSlotsRingBuffer[slotID].lock))<0){
return false;
}
return true;
}
} else {
timespec time;
clock_gettime(CLOCK_REALTIME, &time);
time.tv_nsec+=timeoutns;
time.tv_sec+=(time.tv_nsec/NS_IN_S);
time.tv_nsec%=NS_IN_S;
while (1) {
if (wrflock_rtimewait(&(m_syncSlotsRingBuffer[slotID].lock),&time)<0){
if (errno==EINTR)
continue;
return false;
}
return true;
}
}
}
inline bool fwait(const off_t& slotID, const uint64_t& timeoutns=UINT64_MAX) noexcept {
if (timeoutns==UINT64_MAX){
while (1) {
if (wrflock_fwait(&(m_syncSlotsRingBuffer[slotID].lock))<0){
if (errno==EINTR)
continue;
return false;
}
return true;
}
} else if (timeoutns==0) {
while(1){
if (wrflock_ftrywait(&(m_syncSlotsRingBuffer[slotID].lock))<0){
return false;
}
return true;
}
} else {
timespec time;
clock_gettime(CLOCK_REALTIME, &time);
time.tv_nsec+=timeoutns;
time.tv_sec+=(time.tv_nsec/NS_IN_S);
time.tv_nsec%=NS_IN_S;
while (1) {
if (wrflock_ftimewait(&(m_syncSlotsRingBuffer[slotID].lock),&time)<0){
if (errno==EINTR)
continue;
return false;
}
return true;
}
}
}
};
template <access_type R> class Synchronizer {};
template <> class Synchronizer<access_type::access_master>: public SynchronizerBase {
public:
Synchronizer(const std::string& name): SynchronizerBase(name){}
virtual ~Synchronizer() {
destroy();
}
using SynchronizerBase::create;
using SynchronizerBase::destroy;
using SynchronizerBase::setWriteSlotID;
using SynchronizerBase::getWriteSlotID;
using SynchronizerBase::consumersInactiveLock;
using SynchronizerBase::consumersInactiveUnlock;
using SynchronizerBase::getSynchronizerData;
};
template <> class Synchronizer<access_type::access_slave>: public SynchronizerBase {
public:
Synchronizer(const std::string& name): SynchronizerBase(name){}
virtual ~Synchronizer() {}
using SynchronizerBase::attach;
using SynchronizerBase::getWriteSlotID;
using SynchronizerBase::consumersActiveLock;
using SynchronizerBase::consumersActiveUnlock;
};
#endif /* SYNCHRONIZER_H_ */