|
CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
|
Singleton registry that creates, controls and profiles all CMSDK threads. More...
#include <ThreadManager.h>
Public Member Functions | |
| ThreadManager () | |
| Construct the manager and pre-allocate storage for 1024 thread slots. Prefer CreateThreadManager(). | |
| ~ThreadManager () | |
| Destructor: shuts down all threads, frees the storage block and clears the singleton pointer. | |
| bool | init () |
| Second-phase init: registers this instance as the singleton and starts the monitoring thread in slot 0. | |
| bool | shutdown () |
| Stop the monitoring loop and forcibly terminate all remaining threads. | |
| int32 | threadMonitoring () |
| Body of the monitoring thread (slot 0). | |
| Public Member Functions inherited from cmlabs::Runnable | |
| Runnable () | |
| Initialise flags: not running, allowed to continue. | |
| virtual | ~Runnable () |
| Destructor requests a stop (with the default timeout) before destruction proceeds. | |
| virtual bool | stop (uint32 timeout=200) |
| Ask the worker loop to finish and wait for it to do so. | |
Static Public Member Functions | |
| static bool | CreateThreadManager () |
| Create and initialise the singleton (including its monitoring thread). | |
| static bool | CreateThread (THREAD_FUNCTION func, void *args, uint32 &newID, uint32 reqID=0) |
| Create a new native thread and start it immediately. | |
| 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(). | |
| static bool | InterruptThread (uint32 id) |
| Kill the thread and restart it from its original entry function. | |
| static bool | TerminateThread (uint32 id) |
| Forcibly terminate the thread and release its slot. | |
| static bool | IsThreadRunning (uint32 id) |
| Check whether the thread is still alive at the OS level. | |
| static bool | AddLocalThreadStats () |
| Sample and record CPU statistics for the calling thread itself. | |
| static bool | GetLocalThreadID (uint32 &id) |
| Look up the manager slot ID of the calling thread. | |
| static ThreadStats | GetLocalThreadStats () |
| Get a copy of the statistics record for the calling thread. | |
| static ThreadStats | GetThreadStats (uint32 id) |
| Get a copy of the statistics record for a specific thread. | |
| static ThreadStats * | GetAllThreadStats (uint32 &count) |
| Snapshot the statistics of all live threads. | |
| static bool | Shutdown () |
| Terminate all managed threads, then destroy the singleton. | |
| static bool | UnitTest () |
| Self-contained unit test (create/run/join worker threads, verify stats). | |
Static Public Attributes | |
| static ThreadManager * | Singleton = NULL |
| The process-wide instance; NULL until CreateThreadManager() (or any lazy static call) runs. | |
Protected Member Functions | |
| bool | createThread (THREAD_FUNCTION func, void *args, uint32 &newID, uint32 reqID) |
| Instance-side worker for CreateThread(); requires and manages the mutex internally. | |
| bool | pauseThread (uint32 id) |
| Instance-side worker for PauseThread(). | |
| bool | continueThread (uint32 id) |
| Instance-side worker for ContinueThread(). | |
| bool | interruptThread (uint32 id) |
| Instance-side worker for InterruptThread(). | |
| bool | terminateThread (uint32 id) |
| Instance-side worker for TerminateThread(). | |
| bool | isThreadRunning (uint32 id) |
| Instance-side worker for IsThreadRunning(). | |
| bool | addLocalThreadStats () |
| Instance-side worker for AddLocalThreadStats() (pthreads self-reporting). | |
| bool | getLocalThreadID (uint32 &id) |
| Instance-side worker for GetLocalThreadID(): linear scan of slots for the calling thread's OS ID. | |
| ThreadStats | getThreadStats (uint32 id) |
| Instance-side worker for GetThreadStats(): copies the slot under the mutex. | |
| ThreadStats * | getAllThreadStats (uint32 &count) |
| Instance-side worker for GetAllThreadStats(): allocates and fills a snapshot array (caller frees with delete[]). | |
| bool | resizeThreadStorage (uint32 newCount) |
| Grow the storage block to hold newCount slots. | |
Protected Attributes | |
| utils::Mutex | mutex |
| Single lock serialising all registry and statistics access. | |
| unsigned char * | data |
| Contiguous storage block: ThreadDataHeader + bitfield + ThreadStats[]. | |
| Protected Attributes inherited from cmlabs::Runnable | |
| uint32 | threadID |
| ThreadManager slot ID of the worker thread (0 until known). | |
| bool | shouldContinue |
| Loop-continuation flag; cleared by stop(). | |
| bool | isRunning |
| Set by the worker while its loop is active. | |
Singleton registry that creates, controls and profiles all CMSDK threads.
All public functionality is exposed as static methods that lazily create the singleton on first use, so callers never need to construct one explicitly. Internally every operation takes the single manager mutex, serialising thread creation/termination and statistics access.
The manager also runs its own monitoring thread (slot 0, see threadMonitoring()) which samples per-thread CPU usage about once per second and detects threads that have finished on their own.
Definition at line 213 of file ThreadManager.h.
| cmlabs::ThreadManager::ThreadManager | ( | ) |
Construct the manager and pre-allocate storage for 1024 thread slots. Prefer CreateThreadManager().
Definition at line 158 of file ThreadManager.cpp.
References data, cmlabs::Runnable::isRunning, resizeThreadStorage(), and cmlabs::Runnable::shouldContinue.
Referenced by CreateThreadManager().
| cmlabs::ThreadManager::~ThreadManager | ( | ) |
Destructor: shuts down all threads, frees the storage block and clears the singleton pointer.
Definition at line 166 of file ThreadManager.cpp.
References data, mutex, shutdown(), and Singleton.
|
static |
Sample and record CPU statistics for the calling thread itself.
Definition at line 92 of file ThreadManager.cpp.
References CreateThreadManager(), and Singleton.
|
protected |
Instance-side worker for AddLocalThreadStats() (pthreads self-reporting).
Definition at line 492 of file ThreadManager.cpp.
References cmlabs::ThreadStats::args, cmlabs::ThreadStats::created, cmlabs::ThreadStats::currentCPUTicks, data, cmlabs::ThreadStats::func, cmlabs::utils::GetCPUTicks(), getLocalThreadID(), GETTHREADSTATS, cmlabs::GetTimeNow(), cmlabs::ThreadStats::hThread, cmlabs::ThreadStats::id, mutex, cmlabs::ThreadStats::status, and cmlabs::ThreadStats::time.
|
static |
Resume a thread previously suspended with PauseThread().
| id | Manager slot ID. |
Definition at line 54 of file ThreadManager.cpp.
References CreateThreadManager(), and Singleton.
|
protected |
Instance-side worker for ContinueThread().
Definition at line 321 of file ThreadManager.cpp.
References cmlabs::utils::ContinueThread(), data, GETTHREADSTATS, cmlabs::ThreadStats::hThread, mutex, cmlabs::ThreadStats::status, and THREAD_RUNNING.
|
static |
Create a new native thread and start it immediately.
| func | Entry function (THREAD_FUNCTION signature). |
| args | Opaque pointer handed to func; must outlive the thread. |
| newID | Receives the manager-assigned slot ID of the new thread. |
| reqID | Optional specific slot to (re)use; 0 = allocate the first free slot. |
Definition at line 36 of file ThreadManager.cpp.
References CreateThreadManager(), and Singleton.
Referenced by cmlabs::NetworkChannel::autoDetectConnection(), cmlabs::MemoryManager::connect(), cmlabs::PsySpace::connect(), cmlabs::SSLConnection::connect(), cmlabs::SSLConnection::connect(), cmlabs::TCPConnection::connect(), cmlabs::TCPConnection::connect(), cmlabs::UDPConnection::connect(), cmlabs::MemoryManager::create(), cmlabs::SSLConnection::delayedConnect(), cmlabs::TCPConnection::delayedConnect(), cmlabs::RequestGateway::init(), cmlabs::TCPListener::init(), cmlabs::TestRequestClient::init(), cmlabs::TestWebRequestClient::init(), cmlabs::TestWebSocketRequestClient::init(), cmlabs::ProcessMemory::PerfTest(), cmlabs::RequestClient::RequestClient(), cmlabs::RequestExecutor::RequestExecutor(), cmlabs::NetworkChannel::startConnection(), cmlabs::NetworkChannel::startListener(), cmlabs::NetworkManager::TestHTTP(), cmlabs::TestRequestExecutor::TestRequestExecutor(), cmlabs::ProcessMemory::UnitTest(), and UnitTest().
|
protected |
Instance-side worker for CreateThread(); requires and manages the mutex internally.
Definition at line 247 of file ThreadManager.cpp.
References cmlabs::ThreadDataHeader::activeCount, cmlabs::ThreadStats::args, cmlabs::ThreadDataHeader::bitFieldSize, BITOCCUPIED, cmlabs::ThreadDataHeader::count, cmlabs::ThreadStats::created, cmlabs::utils::CreateThread(), data, cmlabs::ThreadStats::func, cmlabs::utils::GetFirstFreeBitLoc(), GETTHREADSTATS, cmlabs::GetTimeNow(), cmlabs::ThreadStats::hThread, cmlabs::ThreadStats::id, mutex, cmlabs::ThreadStats::osID, resizeThreadStorage(), cmlabs::utils::SetBit(), cmlabs::ThreadStats::status, THREAD_INIT, THREAD_NONE, and THREAD_RUNNING.
Referenced by init(), and interruptThread().
|
static |
Create and initialise the singleton (including its monitoring thread).
Definition at line 24 of file ThreadManager.cpp.
References init(), and ThreadManager().
Referenced by AddLocalThreadStats(), ContinueThread(), CreateThread(), GetAllThreadStats(), GetLocalThreadID(), GetLocalThreadStats(), GetThreadStats(), InterruptThread(), IsThreadRunning(), PauseThread(), TerminateThread(), and UnitTest().
|
static |
Snapshot the statistics of all live threads.
| count | Receives the number of records returned. |
delete[]. Definition at line 136 of file ThreadManager.cpp.
References CreateThreadManager(), and Singleton.
|
protected |
Instance-side worker for GetAllThreadStats(): allocates and fills a snapshot array (caller frees with delete[]).
Definition at line 430 of file ThreadManager.cpp.
References cmlabs::ThreadDataHeader::activeCount, cmlabs::ThreadDataHeader::count, data, GETTHREADSTATS, mutex, cmlabs::ThreadStats::status, and THREAD_TERMINATED.
|
static |
Look up the manager slot ID of the calling thread.
| id | Receives the slot ID on success. |
Definition at line 101 of file ThreadManager.cpp.
References CreateThreadManager(), and Singleton.
|
protected |
Instance-side worker for GetLocalThreadID(): linear scan of slots for the calling thread's OS ID.
Definition at line 540 of file ThreadManager.cpp.
References cmlabs::ThreadDataHeader::count, data, cmlabs::utils::GetCurrentThreadUniqueID(), GETTHREADSTATS, cmlabs::ThreadStats::id, mutex, and cmlabs::ThreadStats::osID.
Referenced by addLocalThreadStats(), and GetLocalThreadStats().
|
static |
Get a copy of the statistics record for the calling thread.
created == 0 signals "not found". Definition at line 111 of file ThreadManager.cpp.
References cmlabs::ThreadStats::created, CreateThreadManager(), getLocalThreadID(), and Singleton.
|
static |
Get a copy of the statistics record for a specific thread.
| id | Manager slot ID. |
created == 0 signals "not found". Definition at line 125 of file ThreadManager.cpp.
References cmlabs::ThreadStats::created, CreateThreadManager(), and Singleton.
Referenced by UnitTest().
|
protected |
Instance-side worker for GetThreadStats(): copies the slot under the mutex.
Definition at line 417 of file ThreadManager.cpp.
References cmlabs::ThreadStats::created, data, GETTHREADSTATS, and mutex.
| bool cmlabs::ThreadManager::init | ( | ) |
Second-phase init: registers this instance as the singleton and starts the monitoring thread in slot 0.
Definition at line 179 of file ThreadManager.cpp.
References cmlabs::ThreadDataHeader::activeCount, createThread(), data, mutex, Singleton, and cmlabs::ThreadMonitoring().
Referenced by CreateThreadManager().
|
static |
Kill the thread and restart it from its original entry function.
| id | Manager slot ID. |
Definition at line 63 of file ThreadManager.cpp.
References CreateThreadManager(), and Singleton.
|
protected |
Instance-side worker for InterruptThread().
Definition at line 336 of file ThreadManager.cpp.
References cmlabs::ThreadDataHeader::activeCount, cmlabs::ThreadStats::args, cmlabs::ThreadStats::created, createThread(), data, cmlabs::ThreadStats::func, GETTHREADSTATS, cmlabs::ThreadStats::hThread, mutex, cmlabs::ThreadStats::status, cmlabs::utils::TerminateThread(), THREAD_INTERRUPTED, and THREAD_TERMINATED.
|
static |
Check whether the thread is still alive at the OS level.
| id | Manager slot ID. |
Definition at line 82 of file ThreadManager.cpp.
References CreateThreadManager(), and Singleton.
Referenced by cmlabs::StopProcessTestThread(), cmlabs::NetworkManager::TestHTTP(), and cmlabs::RequestGateway::~RequestGateway().
|
protected |
Instance-side worker for IsThreadRunning().
Definition at line 401 of file ThreadManager.cpp.
References data, GETTHREADSTATS, cmlabs::ThreadStats::hThread, cmlabs::utils::IsThreadRunning(), and mutex.
|
static |
Suspend the thread at whatever point it is currently executing.
| id | Manager slot ID. |
Definition at line 45 of file ThreadManager.cpp.
References CreateThreadManager(), and Singleton.
|
protected |
Instance-side worker for PauseThread().
Definition at line 306 of file ThreadManager.cpp.
References data, GETTHREADSTATS, cmlabs::ThreadStats::hThread, mutex, cmlabs::utils::PauseThread(), cmlabs::ThreadStats::status, and THREAD_PAUSED.
|
protected |
Grow the storage block to hold newCount slots.
| newCount | New slot capacity (createThread grows by 4x when full). |
Definition at line 449 of file ThreadManager.cpp.
References cmlabs::ThreadDataHeader::activeCount, cmlabs::ThreadDataHeader::bitFieldSize, cmlabs::utils::Calc32BitFieldSize(), cmlabs::ThreadDataHeader::count, data, cmlabs::utils::GetThreadStatColAbility(), GETTHREADSTATS, cmlabs::ThreadDataHeader::size, and cmlabs::ThreadDataHeader::statColPolicy.
Referenced by createThread(), and ThreadManager().
|
static |
Terminate all managed threads, then destroy the singleton.
Definition at line 145 of file ThreadManager.cpp.
References shutdown(), and Singleton.
Referenced by cmlabs::NetworkManager::UnitTest(), UnitTest(), cmlabs::NetworkManager::UnitTestDelayedConnect(), cmlabs::NetworkManager::UnitTestHTTP(), cmlabs::NetworkManager::WebsocketTest(), cmlabs::CleanShutdown::~CleanShutdown(), and cmlabs::MemoryManager::~MemoryManager().
| bool cmlabs::ThreadManager::shutdown | ( | ) |
Stop the monitoring loop and forcibly terminate all remaining threads.
Definition at line 195 of file ThreadManager.cpp.
References cmlabs::ThreadDataHeader::activeCount, cmlabs::ThreadDataHeader::count, data, GETTHREADSTATS, cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), cmlabs::ThreadStats::hThread, cmlabs::ThreadStats::id, mutex, cmlabs::utils::Sleep(), cmlabs::ThreadStats::status, cmlabs::Runnable::stop(), terminateThread(), THREAD_TERMINATED, and cmlabs::utils::TryReapThread().
Referenced by Shutdown(), and ~ThreadManager().
|
static |
Forcibly terminate the thread and release its slot.
| id | Manager slot ID. |
Definition at line 72 of file ThreadManager.cpp.
References CreateThreadManager(), and Singleton.
Referenced by cmlabs::NetworkChannel::endConnection(), cmlabs::NetworkChannel::shutdown(), cmlabs::PsySpace::shutdown(), and cmlabs::NetworkChannel::stopListener().
|
protected |
Instance-side worker for TerminateThread().
Definition at line 364 of file ThreadManager.cpp.
References cmlabs::ThreadDataHeader::activeCount, cmlabs::ThreadStats::created, data, cmlabs::ThreadStats::func, GETTHREADSTATS, cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), cmlabs::ThreadStats::hThread, mutex, cmlabs::utils::Sleep(), cmlabs::ThreadStats::status, cmlabs::utils::TerminateThread(), THREAD_TERMINATED, and cmlabs::utils::TryReapThread().
Referenced by shutdown().
| int32 cmlabs::ThreadManager::threadMonitoring | ( | ) |
Body of the monitoring thread (slot 0).
Roughly once per second it either (a) under THREAD_STATS_ADHOC, rotates the process-wide summary buffers and reaps threads that finished, or (b) under central collection, samples every live thread's CPU ticks, sums them into the header and rotates all rolling windows. Runs until Runnable::stop() clears shouldContinue.
Definition at line 589 of file ThreadManager.cpp.
References cmlabs::ThreadStats::args, cmlabs::ThreadStats::created, cmlabs::ThreadStats::currentCPUTicks, data, cmlabs::ThreadStats::func, cmlabs::utils::GetCPUTicks(), GETTHREADSTATS, cmlabs::GetTimeAge(), cmlabs::GetTimeNow(), cmlabs::ThreadStats::hThread, cmlabs::ThreadStats::id, cmlabs::Runnable::isRunning, LOG_PROCESS, LogPrint, mutex, cmlabs::ThreadStats::osID, cmlabs::PrintTimeDifString(), cmlabs::Runnable::shouldContinue, cmlabs::utils::Sleep(), cmlabs::ThreadStats::status, THREAD_STATS_ADHOC, THREAD_STATS_OFF, THREAD_TERMINATED, cmlabs::ThreadStats::time, and cmlabs::utils::TryReapThread().
|
static |
Self-contained unit test (create/run/join worker threads, verify stats).
Definition at line 725 of file ThreadManager.cpp.
References cmlabs::ThreadStats::created, CreateThread(), CreateThreadManager(), cmlabs::unittest::detail(), cmlabs::utils::Mutex::enter(), cmlabs::unittest::fail(), GetThreadStats(), cmlabs::GetTimeNow(), cmlabs::utils::Mutex::leave(), cmlabs::unittest::metric(), cmlabs::unittest::progress(), Shutdown(), and cmlabs::utils::Sleep().
Referenced by cmlabs::Register_ThreadManager_Tests().
|
protected |
Contiguous storage block: ThreadDataHeader + bitfield + ThreadStats[].
Definition at line 350 of file ThreadManager.h.
Referenced by addLocalThreadStats(), continueThread(), createThread(), getAllThreadStats(), getLocalThreadID(), getThreadStats(), init(), interruptThread(), isThreadRunning(), pauseThread(), resizeThreadStorage(), shutdown(), terminateThread(), ThreadManager(), threadMonitoring(), and ~ThreadManager().
|
protected |
Single lock serialising all registry and statistics access.
Definition at line 349 of file ThreadManager.h.
Referenced by addLocalThreadStats(), continueThread(), createThread(), getAllThreadStats(), getLocalThreadID(), getThreadStats(), init(), interruptThread(), isThreadRunning(), pauseThread(), shutdown(), terminateThread(), threadMonitoring(), and ~ThreadManager().
|
static |
The process-wide instance; NULL until CreateThreadManager() (or any lazy static call) runs.
Definition at line 216 of file ThreadManager.h.
Referenced by AddLocalThreadStats(), ContinueThread(), CreateThread(), GetAllThreadStats(), GetLocalThreadID(), GetLocalThreadStats(), GetThreadStats(), init(), InterruptThread(), IsThreadRunning(), PauseThread(), Shutdown(), TerminateThread(), cmlabs::ThreadMonitoring(), and ~ThreadManager().