MNE-CPP 0.1.9
A Framework for Electrophysiology
Loading...
Searching...
No Matches
eventsharedmemmanager.h
Go to the documentation of this file.
1//=============================================================================================================
36#ifndef EVENTSHAREDMEMMANAGER_EVENTS_H
37#define EVENTSHAREDMEMMANAGER_EVENTS_H
38
39//=============================================================================================================
40// STD INCLUDES
41//=============================================================================================================
42
43#include <string>
44#include <memory>
45#include <chrono>
46#include <thread>
47#include <mutex>
48#include <atomic>
49
50//=============================================================================================================
51// Qt INCLUDES
52//=============================================================================================================
53
54#include <QSharedMemory>
55
56//=============================================================================================================
57// MNECPP INCLUDES
58//=============================================================================================================
59
60#include "event.h"
61#include "eventgroup.h"
62
63namespace EVENTSLIB {
64
65//=============================================================================================================
66// EVENTSLIB FORWARD DECLARATIONS
67//=============================================================================================================
68
69enum SharedMemoryMode { READ, WRITE, READWRITE };
70class EventManager;
71
72namespace EVENTSINTERNAL {
73
74//=========================================================================================================
78enum EventUpdateType{ NULL_EVENT, NEW_EVENT, DELETE_EVENT};
79
80//=========================================================================================================
84const std::string EventUpdateTypeString[3] = {"Null Event", "New Event", "Delete Event"};
85
91{
92public:
93 //=========================================================================================================
98
99 //=========================================================================================================
106 EventUpdate(int sample, int creator, enum EventUpdateType t);
107
108 //=========================================================================================================
113 long long getCreationTime() const;
114
115 //=========================================================================================================
120 int getSample() const;
121
122 //=========================================================================================================
127 int getCreatorId() const;
128
129 //=========================================================================================================
134 enum EventUpdateType getType() const;
135
136 //=========================================================================================================
141 void setType(enum EventUpdateType t);
142
143 //=========================================================================================================
148 std::string eventTypeToText();
149
150protected:
153 long long m_CreationTime;
155};
156
161{
162public:
163 //=========================================================================================================
168 explicit EventSharedMemManager(EVENTSLIB::EventManager* parent = nullptr);
169
170 //=========================================================================================================
175
176 //=========================================================================================================
181 void init(EVENTSLIB::SharedMemoryMode mode);
182
183 //=========================================================================================================
188 bool isInit() const;
189
190 //=========================================================================================================
194 void stop();
195
196 //=========================================================================================================
201 void addEvent(int sample);
202
203 //=========================================================================================================
208 void deleteEvent(int sample);
209
210 //=========================================================================================================
215 static long long getTimeNow();
216
217 //=========================================================================================================
222 void processEvent(const EventUpdate& ne);
223
224private:
225 //=========================================================================================================
230 void attachToSharedSegment(QSharedMemory::AccessMode mode);
231
232 //=========================================================================================================
239 bool createSharedSegment(int bufferSize, QSharedMemory::AccessMode mode);
240
241 //=========================================================================================================
245 void launchSharedMemoryWatcherThread();
246
247 //=========================================================================================================
252 void attachToOrCreateSharedSegment(QSharedMemory::AccessMode mode);
253
254 //=========================================================================================================
258 void stopSharedMemoryWatcherThread();
259
260 //=========================================================================================================
264 void detachFromSharedMemory();
265
266 //=========================================================================================================
271 inline static int generateId();
272
273 //=========================================================================================================
278 void processNewEvent(const EventUpdate& n);
279
280 //=========================================================================================================
285 void processDeleteEvent(const EventUpdate& n);
286
287 //=========================================================================================================
291 void printLocalBuffer();
292
293 //=========================================================================================================
298 void copyNewUpdateToSharedMemory(EventUpdate& newUpdate);
299
300 //=========================================================================================================
304 void initializeSharedMemory();
305
306 //=========================================================================================================
310 void copySharedMemoryToLocalBuffer();
311
312 //=========================================================================================================
316 void processLocalBuffer();
317
318 //=========================================================================================================
322 void bufferWatcher();
323
324 //=========================================================================================================
328 void createGroupIfNeeded();
329
330 static int m_iLastUpdateIndex;
331 EVENTSLIB::EventManager* m_pEventManager;
332 QSharedMemory m_SharedMemory;
333 std::atomic_bool m_IsInit;
334 std::string m_sGroupName;
335 bool m_bGroupCreated;
336 idNum m_GroupId;
337 int m_SharedMemorySize;
338 int m_fTimerCheckBuffer;
339 std::thread m_BufferWatcherThread;
340 std::atomic_bool m_BufferWatcherThreadRunning;
341 std::atomic_bool m_WritingToSharedMemory;
342 long long m_lastCheckTime;
343 EventUpdate* m_LocalBuffer;
344 EventUpdate* m_SharedBuffer;
345 int m_Id;
346 enum EVENTSLIB::SharedMemoryMode m_Mode;
347};
348
349//=========================================================================================================
354inline int EventSharedMemManager::generateId()
355{
356 auto t = static_cast<int>(EventSharedMemManager::getTimeNow());
357 return abs(t);
358}
359
360} //namespace EVENTSINTERNAL
361}//namespace EVENTSLIB
362#endif // EVENTSHAREDMEMMANAGER_EVENTS_H
363
const std::string EventUpdateTypeString[3]
EventGroup declaration.
Event declaration.