CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
ThreadManager.h
Go to the documentation of this file.
1
90#if !defined(_THREADMANAGER_H_)
91#define _THREADMANAGER_H_
92
93#include "MemoryManager.h"
94
95namespace cmlabs {
96
109 uint32 id;
110 uint64 created;
111 uint32 status;
112 uint32 osID;
115 void* args;
116 uint64 time[10];
117 uint64 currentCPUTicks[10];
118};
119
129 uint32 size;
130 uint32 count;
131 uint32 activeCount;
133 uint64 time[10];
134 uint64 currentCPUTicks[10];
136 // Bitfield
137 // Stats1, Stats2, ...
138};
139
140
153class Runnable {
154public:
156 Runnable() {threadID = 0; shouldContinue = true; isRunning = false;}
158 virtual ~Runnable() {
159 stop();
160 }
161
166 virtual bool stop(uint32 timeout = 200) {
167 shouldContinue = false;
168 int32 timeleft = (int32)timeout;
169 while (isRunning) {
170 utils::Sleep(5);
171 if ( (timeleft -= 5) <= 0) {
172 LogPrint(0,LOG_SYSTEM,0,"Thread id %u didn't terminate...\n", threadID);
173 return false;
174 }
175 }
176 return true;
177 }
178
179protected:
180 uint32 threadID;
183};
184
190#define GETTHREADSTATS(data,id) (ThreadStats*)(data + sizeof(ThreadDataHeader) + ((ThreadDataHeader*)data)->bitFieldSize + id*sizeof(ThreadStats))
191
213class ThreadManager : public Runnable {
214public:
217
221 static bool CreateThreadManager();
228 static bool CreateThread(THREAD_FUNCTION func, void* args, uint32& newID, uint32 reqID = 0);
235 static bool PauseThread(uint32 id);
239 static bool ContinueThread(uint32 id);
246 static bool InterruptThread(uint32 id);
251 static bool TerminateThread(uint32 id);
256 static bool IsThreadRunning(uint32 id);
257
264 static bool AddLocalThreadStats();
269 static bool GetLocalThreadID(uint32& id);
270
277 static ThreadStats GetThreadStats(uint32 id);
282 static ThreadStats* GetAllThreadStats(uint32& count);
283
288 static bool Shutdown();
289
292 static bool UnitTest();
293
301 bool init();
304 bool shutdown();
305
315 int32 threadMonitoring();
316
317protected:
319 bool createThread(THREAD_FUNCTION func, void* args, uint32& newID, uint32 reqID);
321 bool pauseThread(uint32 id);
323 bool continueThread(uint32 id);
325 bool interruptThread(uint32 id);
327 bool terminateThread(uint32 id);
329 bool isThreadRunning(uint32 id);
330
332 bool addLocalThreadStats();
334 bool getLocalThreadID(uint32& id);
335
337 ThreadStats getThreadStats(uint32 id);
339 ThreadStats* getAllThreadStats(uint32& count);
340
347 bool resizeThreadStorage(uint32 newCount);
348
350 unsigned char* data;
351};
352
355
361
362
363} // namespace cmlabs
364
365#endif //_THREADMANAGER_H_
366
Central shared-memory manager for a Psyclone node: master segment, per-subsystem shared maps and the ...
#define LOG_SYSTEM
Definition Utils.h:197
#define THREAD_RET
Definition Utils.h:127
#define THREAD_FUNCTION_CALL
Definition Utils.h:129
#define LogPrint
Definition Utils.h:313
THREAD_RET(* THREAD_FUNCTION)(void *)
Definition Utils.h:128
#define ThreadHandle
Definition Utils.h:125
bool isRunning
Set by the worker while its loop is active.
virtual bool stop(uint32 timeout=200)
Ask the worker loop to finish and wait for it to do so.
Runnable()
Initialise flags: not running, allowed to continue.
uint32 threadID
ThreadManager slot ID of the worker thread (0 until known).
virtual ~Runnable()
Destructor requests a stop (with the default timeout) before destruction proceeds.
bool shouldContinue
Loop-continuation flag; cleared by stop().
bool createThread(THREAD_FUNCTION func, void *args, uint32 &newID, uint32 reqID)
Instance-side worker for CreateThread(); requires and manages the mutex internally.
bool resizeThreadStorage(uint32 newCount)
Grow the storage block to hold newCount slots.
bool addLocalThreadStats()
Instance-side worker for AddLocalThreadStats() (pthreads self-reporting).
static bool CreateThreadManager()
Create and initialise the singleton (including its monitoring thread).
bool pauseThread(uint32 id)
Instance-side worker for PauseThread().
bool getLocalThreadID(uint32 &id)
Instance-side worker for GetLocalThreadID(): linear scan of slots for the calling thread's OS ID.
static bool GetLocalThreadID(uint32 &id)
Look up the manager slot ID of the calling thread.
bool continueThread(uint32 id)
Instance-side worker for ContinueThread().
ThreadManager()
Construct the manager and pre-allocate storage for 1024 thread slots. Prefer CreateThreadManager().
bool interruptThread(uint32 id)
Instance-side worker for InterruptThread().
ThreadStats * getAllThreadStats(uint32 &count)
Instance-side worker for GetAllThreadStats(): allocates and fills a snapshot array (caller frees with...
static bool InterruptThread(uint32 id)
Kill the thread and restart it from its original entry function.
~ThreadManager()
Destructor: shuts down all threads, frees the storage block and clears the singleton pointer.
bool shutdown()
Stop the monitoring loop and forcibly terminate all remaining threads.
utils::Mutex mutex
Single lock serialising all registry and statistics access.
static ThreadStats GetLocalThreadStats()
Get a copy of the statistics record for the calling thread.
bool terminateThread(uint32 id)
Instance-side worker for TerminateThread().
static ThreadStats GetThreadStats(uint32 id)
Get a copy of the statistics record for a specific thread.
static bool CreateThread(THREAD_FUNCTION func, void *args, uint32 &newID, uint32 reqID=0)
Create a new native thread and start it immediately.
int32 threadMonitoring()
Body of the monitoring thread (slot 0).
unsigned char * data
Contiguous storage block: ThreadDataHeader + bitfield + ThreadStats[].
ThreadStats getThreadStats(uint32 id)
Instance-side worker for GetThreadStats(): copies the slot under the mutex.
static ThreadManager * Singleton
The process-wide instance; NULL until CreateThreadManager() (or any lazy static call) runs.
static bool UnitTest()
Self-contained unit test (create/run/join worker threads, verify stats).
static bool IsThreadRunning(uint32 id)
Check whether the thread is still alive at the OS level.
bool init()
Second-phase init: registers this instance as the singleton and starts the monitoring thread in slot ...
static bool TerminateThread(uint32 id)
Forcibly terminate the thread and release its slot.
static bool AddLocalThreadStats()
Sample and record CPU statistics for the calling thread itself.
static bool Shutdown()
Terminate all managed threads, then destroy the singleton.
static ThreadStats * GetAllThreadStats(uint32 &count)
Snapshot the statistics of all live threads.
static bool PauseThread(uint32 id)
Suspend the thread at whatever point it is currently executing.
static bool ContinueThread(uint32 id)
Resume a thread previously suspended with PauseThread().
bool isThreadRunning(uint32 id)
Instance-side worker for IsThreadRunning().
Recursive mutual-exclusion lock, optionally named for cross-process use.
Definition Utils.h:463
static THREAD_RET THREAD_FUNCTION_CALL ThreadMonitoring(void *arg)
Entry function of the monitoring thread; delegates to ThreadManager::threadMonitoring().
bool Sleep(uint32 ms)
Suspend the calling thread.
Definition Utils.cpp:2830
bool TestThreadManager()
Legacy standalone smoke test for ThreadManager (predates the UnitTestRunner framework).
Header at the start of the ThreadManager's contiguous storage block.
uint32 size
Total size of the storage block in bytes.
uint32 statColPolicy
Statistics collection policy (THREAD_STATS_OFF / ADHOC / central), from utils::GetThreadStatColAbilit...
uint32 count
Capacity: number of ThreadStats slots allocated.
uint64 currentCPUTicks[10]
Rolling process-wide summed CPU ticks matching time.
uint32 bitFieldSize
Size in bytes of the slot-occupancy bitfield that follows this header.
uint32 activeCount
Number of slots currently holding a live (non-terminated) thread.
uint64 time[10]
Rolling timestamps of process-wide summary samples (newest first).
Bookkeeping record for a single managed thread.
uint32 status
Lifecycle state (THREAD_NONE/INIT/RUNNING/PAUSED/INTERRUPTED/TERMINATED).
ThreadHandle hThread
Native handle (HANDLE on Windows, pthread_t wrapper on POSIX).
void * args
Argument passed to func, retained for restart.
uint64 currentCPUTicks[10]
Rolling CPU-tick readings matching time (newest first).
uint64 time[10]
Rolling timestamps of the last 10 statistics samples (newest first).
uint32 id
ThreadManager-assigned slot ID (index into the stats array).
uint32 osID
Operating-system thread ID (as reported by the OS, not the slot ID).
THREAD_FUNCTION func
Entry function, retained so InterruptThread() can restart the thread.
uint64 created
Creation time (microseconds, GetTimeNow()); 0 = slot unused/invalid.