CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
PsySpace.h
Go to the documentation of this file.
1
27#if !defined(_PSYSPACE_H_)
28#define _PSYSPACE_H_
29
30#include "MemoryManager.h"
31#include "PsyTime.h"
32#include "xml_parser.h"
33#include "DataMessage.h"
34#include "Subscriptions.h"
35
36namespace cmlabs {
37
38class PsyAPI;
49typedef int8 (* CrankFunction)(PsyAPI* api);
50
52#define LOG_SPACE 7
53
62public:
68 TimeQueueSchedule(uint32 interval, uint64 start = 0, uint64 end = 0);
71
72 uint32 id;
73 uint64 nextTrigger;
74
75 uint32 interval;
76 uint64 start;
77 uint64 end;
78
80 uint32 msgTag;
81};
82
91class TimeQueue {
92public:
94 TimeQueue();
96 ~TimeQueue();
97
102 bool addSchedule(TimeQueueSchedule* schedule);
107 bool removeSchedule(uint32 id);
108
114 DataMessage* waitForNextEvent(uint32 ms);
115
116private:
117 utils::Timer* timer;
118 std::map<uint32, TimeQueueSchedule*> schedules;
119
120 utils::Mutex mutex;
121 utils::Semaphore semaphore;
122};
123
135
140#define PROCMESSAGE 0
141#define PROCSIGNAL 1
142#define PROCREQUEST 2
144
164class PsySpace : public Runnable, public LogReceiver {
165 friend class PsyAPI;
166public:
173
174 // ***************** Constructors *****************
175 // Create and destroy resources
176
183 PsySpace(const char* name = NULL, bool isAdHoc = true, uint16 procID = 0, bool isLocal = false);
187 ~PsySpace();
188
196 bool connect(uint16 systemID, bool isMaster = false, const char* cmdline = NULL);
201 bool isConnected(uint32 timeoutMS = 5000);
205 bool reset();
210 bool start(uint16 threadCount = 5);
223 bool shutdown();
225 bool hasShutdown();
227 uint16 getID();
228
234 bool postMessage(DataMessage* msg);
235
236 // ***************** Signals *****************
245 bool emitSignal(const PsyType& type, DataMessage* msg);
252 DataMessage* waitForSignal(const PsyType& type, uint32 timeout, uint64 lastReceivedTime = 0);
253
280 uint8 query(DataMessage* msg, DataMessage** result, uint32 timeout);
287 bool queryReply(uint32 id, uint8 status, DataMessage* result);
288
293 uint32 getComponentID(const char* name);
294// PsyAPI* getComponentAPI(const char* name);
295// PsyAPI* getComponentAPI(uint32 compID);
296// bool registerComponentCallback(const char* name, CrankFunction func);
297// bool registerComponentCallback(uint32 compID, CrankFunction func);
298
307 PsyAPI* getCrankAPI(const char* name);
313 bool registerCrankCallback(const char* name, CrankFunction func);
314
321 bool addPsyProbeCustomView(uint32 compID, const char* name, const char* templateURL);
322
327 bool logEntry(LogEntry* entry);
328
331private:
332
333 std::map<std::string, CrankFunction> internalCranks;
334
335 bool threadPoolDispatch();
336 bool runContinuousComponent();
337 bool startContinuousComponent(uint32 compID);
338 bool setThreadPoolSize(uint16 threadCount);
339 CrankFunction loadCrankFromLibrary(const char* crankName, const char* libraryFilename);
340
341 bool pullRemoteComponentData(uint32 compID, uint16 fromNodeID);
342
343 bool run();
344
345 std::string name;
346 bool isLocal;
347 bool isMaster;
348 bool isAdHoc;
349 bool finishedShuttingDown;
350 uint64 currentInstID;
351
352 uint16 procID;
353 uint64 masterCreatedTime;
354
355 uint32 waitCounters[3];
356 uint32 procCounters[3];
357
358 uint32 threadPoolWaitLowerThreshold;
359 uint32 threadPoolWaitUpperThreshold;
360 uint32 threadPoolWaitIncrement;
361 uint64 lastThreadPoolCheck;
362 uint64 threadPoolCheckInterval;
363 utils::Mutex threadPoolMutex;
364
365 TimeQueue* timeQ;
366
367 uint16 threadTarget;
368
369 std::map<uint16, CrankFunction> cranks;
370 std::map<uint32, uint16> compCrankIDs;
371 std::map<std::string, utils::Library*> libraries;
372 std::map<uint32, PsyAPI*> psyAPIs;
373 std::map<PsyType, SignalStruct*> signalList;
374 utils::Mutex signalsMutex;
375
376 std::map<uint32, uint8> threadPool;
377 std::map<uint32, uint32> continuousComponentThreads;
378};
379
386
387} // namespace cmlabs
388
389#include "PsyAPI.h"
390#include "PsyInternal.h"
391
392#endif //_PSYSPACE_H_
393
The binary DataMessage container — the central data-exchange object of Psyclone/CMSDK.
Central shared-memory manager for a Psyclone node: master segment, per-subsystem shared maps and the ...
PsyAPI — the component-facing API handle of the CMSDK.
Built-in internal cranks and message-script playback structures.
CMSDK time: µs-resolution 64-bit timestamps and the Time Mapping Constant (TMC).
Subscription, trigger, retrieval, query, post and signal specifications — the declarative wiring of a...
#define THREAD_RET
Definition Utils.h:127
#define THREAD_FUNCTION_CALL
Definition Utils.h:129
#define THREAD_ARG
Definition Utils.h:130
The central Psyclone data container: a self-contained binary message with typed, named user entries.
Top-level facade of the shared-memory subsystem for one process.
The API handle a component (crank) uses to talk to the Psyclone system.
Definition PsyAPI.h:82
bool postMessage(DataMessage *msg)
Post a raw message into the system for distribution to subscribers.
Definition PsySpace.cpp:465
bool reset()
Reset the space's runtime state (counters, queues) without disconnecting.
Definition PsySpace.cpp:249
bool connect(uint16 systemID, bool isMaster=false, const char *cmdline=NULL)
Join a Psyclone system (attach to the node's shared memory).
Definition PsySpace.cpp:134
bool shutdown()
Request an orderly shutdown of the space and all its components.
Definition PsySpace.cpp:282
friend THREAD_RET THREAD_FUNCTION_CALL PsySpaceRun(THREAD_ARG arg)
Thread entry for the space's main service thread.
bool start(uint16 threadCount=5)
Start processing: spin up the thread pool and begin dispatching triggers.
Definition PsySpace.cpp:276
uint8 query(DataMessage *msg, DataMessage **result, uint32 timeout)
Send a query message and wait for its reply.
Definition PsySpace.cpp:584
uint32 getComponentID(const char *name)
Look up the numeric component id for a component name.
Definition PsySpace.cpp:424
DataMessage * waitForSignal(const PsyType &type, uint32 timeout, uint64 lastReceivedTime=0)
Block until a signal of the given type arrives (or has already arrived).
Definition PsySpace.cpp:521
friend THREAD_RET THREAD_FUNCTION_CALL PsySpacePoolRun(THREAD_ARG arg)
Thread entry for pool worker threads.
friend THREAD_RET THREAD_FUNCTION_CALL PsySpaceContinuousRun(THREAD_ARG arg)
Thread entry for continuously running components.
~PsySpace()
Destructor.
Definition PsySpace.cpp:128
bool isConnected(uint32 timeoutMS=5000)
Wait until the space is fully connected to the system.
Definition PsySpace.cpp:226
MemoryManager * manager
The process-local memory manager: entry point to the node's shared-memory fabric. Owned by the space.
Definition PsySpace.h:330
friend class PsyAPI
Definition PsySpace.h:165
bool registerCrankCallback(const char *name, CrankFunction func)
Register a C/C++ entry function for a crank, to be invoked by the thread pool when the component trig...
Definition PsySpace.cpp:413
bool addPsyProbeCustomView(uint32 compID, const char *name, const char *templateURL)
Add a custom PsyProbe view tab for a component (space-level variant of PsyAPI::addPsyProbeCustomView(...
Definition PsySpace.cpp:336
PsyAPI * getCrankAPI(const char *name)
Get (or create) the PsyAPI handle for a crank hosted in this space.
Definition PsySpace.cpp:384
bool queryReply(uint32 id, uint8 status, DataMessage *result)
Reply to a previously received query.
Definition PsySpace.cpp:665
bool emitSignal(const PsyType &type, DataMessage *msg)
Emit a system-wide signal of the given type.
Definition PsySpace.cpp:489
bool logEntry(LogEntry *entry)
LogReceiver interface: accept a log entry produced within this space.
Definition PsySpace.cpp:346
PsySpace(const char *name=NULL, bool isAdHoc=true, uint16 procID=0, bool isLocal=false)
Create a space (does not yet join a system; call connect()).
Definition PsySpace.cpp:88
Runnable()
Initialise flags: not running, allowed to continue.
Time-binned activation queue used by a PsySpace to drive time-triggered modules.
Definition PsySpace.h:91
DataMessage * waitForNextEvent(uint32 ms)
Block until the next scheduled event is due, or the timeout expires.
Definition PsySpace.cpp:61
bool removeSchedule(uint32 id)
Remove (and delete) a schedule by id.
Definition PsySpace.cpp:47
bool addSchedule(TimeQueueSchedule *schedule)
Add a schedule to the queue.
Definition PsySpace.cpp:36
~TimeQueue()
Destroy the queue and delete all remaining schedules (owned by the queue).
Definition PsySpace.cpp:27
TimeQueue()
Create an empty time queue with its own timer.
Definition PsySpace.cpp:23
One recurring (or time-bounded) activation schedule managed by a TimeQueue.
Definition PsySpace.h:61
uint32 msgTag
Tag attached to the generated message.
Definition PsySpace.h:80
uint64 start
First-activation time (µs), or 0 for immediate.
Definition PsySpace.h:76
PsyType msgType
Type of the message generated on each activation.
Definition PsySpace.h:79
uint64 nextTrigger
Next activation time (PsyTime µs timestamp), maintained by the TimeQueue.
Definition PsySpace.h:73
uint32 interval
Repeat interval in milliseconds.
Definition PsySpace.h:75
TimeQueueSchedule(uint32 interval, uint64 start=0, uint64 end=0)
Create a schedule.
Definition PsySpace.cpp:13
~TimeQueueSchedule()
Destructor.
Definition PsySpace.cpp:19
uint64 end
Expiry time (µs), or 0 for no expiry.
Definition PsySpace.h:77
uint32 id
Unique schedule id, used for removal via TimeQueue::removeSchedule().
Definition PsySpace.h:72
Auto-reset notification event (condition variable style), optionally named for cross-process use.
Definition Utils.h:533
Recursive mutual-exclusion lock, optionally named for cross-process use.
Definition Utils.h:463
Counting semaphore, optionally named for cross-process use.
Definition Utils.h:502
Multiplexing timer: schedule many periodic timers and consume their expiries from one queue.
Definition Utils.h:656
int8(* CrankFunction)(PsyAPI *api)
Signature of a component (crank) entry function.
Definition PsySpace.h:49
THREAD_RET THREAD_FUNCTION_CALL PsySpaceContinuousRun(THREAD_ARG arg)
Continuous-component thread entry: runs PsySpace::runContinuousComponent() for the space passed in ar...
THREAD_RET THREAD_FUNCTION_CALL PsySpaceRun(THREAD_ARG arg)
Main service-thread entry: runs PsySpace::run() for the space passed in arg.
THREAD_RET THREAD_FUNCTION_CALL PsySpacePoolRun(THREAD_ARG arg)
Pool worker-thread entry: runs PsySpace::threadPoolDispatch() for the space passed in arg.
Hierarchical message type identifier — the key used for publish/subscribe matching in Psyclone.
Definition Types.h:123
Wire/storage layout of one log record: fixed header immediately followed by the message text.
Definition Utils.h:228
Per-signal bookkeeping for PsySpace signal distribution.
Definition PsySpace.h:130
utils::Mutex mutex
Protects lastSignalMsg.
Definition PsySpace.h:133
DataMessage * lastSignalMsg
Most recently emitted message for this signal type (owned by the space).
Definition PsySpace.h:131
utils::Event event
Signalled whenever a new message arrives; wakes waiters.
Definition PsySpace.h:132
Small recursive XML DOM parser (XMLNode) used by CMSDK for all PsySpec XML parsing.