CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
ThreadManager.cpp
Go to the documentation of this file.
1
15#include "ThreadManager.h"
16#include "PsyTime.h"
17#include "Utils.h"
18#include "UnitTestFramework.h"
19
20namespace cmlabs {
21
23
24// Single-teardown guard. shutdown() force-terminates and joins the registered
25// worker threads while holding the registry mutex. If a SECOND driver enters
26// shutdown() concurrently on another thread - e.g. a signal-driven exit() runs
27// CleanShutdown::~CleanShutdown() -> ThreadManager::Shutdown() on a worker while
28// main is already tearing the node down - the two collide on the mutex/join and
29// deadlock. This flag lets only the FIRST caller run the teardown; a concurrent
30// second caller returns immediately. It is scoped to the in-flight teardown (set
31// on entry, cleared on exit), NOT a permanent latch, so sequential teardowns
32// (e.g. one per CMSDK network unit test in fork=0 mode) still work.
33static volatile int32 g_threadMgrTearingDown = 0;
34
35// Atomic test-and-set: returns true iff it flipped 0 -> 1 (i.e. WE won the race
36// and now own the teardown). Mirrors the WINDOWS / __sync idiom used in Utils.cpp.
38 #if defined WINDOWS
39 return (_InterlockedCompareExchange((volatile long*)&g_threadMgrTearingDown, 1, 0) == 0);
40 #else
41 return __sync_bool_compare_and_swap(&g_threadMgrTearingDown, 0, 1);
42 #endif
43}
44
45static void EndThreadMgrTeardown() {
46 #if defined WINDOWS
47 _InterlockedExchange((volatile long*)&g_threadMgrTearingDown, 0);
48 #else
49 __sync_lock_test_and_set(&g_threadMgrTearingDown, 0);
50 #endif
51}
52
54 ThreadManager* threadManager = new ThreadManager();
55 if (!threadManager->init()) {
56 //fprintf(stderr, "ThreadManager init() failed...\n");
57 delete(threadManager);
58 return false;
59 }
60 return true;
61}
62
63// Static
64// Create a new thread and start it
65bool ThreadManager::CreateThread(THREAD_FUNCTION func, void* args, uint32& newID, uint32 reqID) {
68 return false;
69 return ThreadManager::Singleton->createThread(func, args, newID, reqID);
70}
71
72// Static
73// Pause the thread wherever it is now
77 return false;
78 return ThreadManager::Singleton->pauseThread(id);
79}
80
81// Static
82// Allow the thread to continue running
86 return false;
87 return ThreadManager::Singleton->continueThread(id);
88}
89
90// Static
91// Terminate the thread and restart it from its base location
95 return false;
96 return ThreadManager::Singleton->interruptThread(id);
97}
98
99// Static
100// Terminate and destroy the thread
103 if (!CreateThreadManager())
104 return false;
105 return ThreadManager::Singleton->terminateThread(id);
106}
107
108
109// Static
110// Terminate and destroy the thread
113 if (!CreateThreadManager())
114 return false;
115 return ThreadManager::Singleton->isThreadRunning(id);
116}
117
118
119// Static
120// Wait for a thread to have COMPLETELY finished, then release it - exactly once
123 if (!CreateThreadManager())
124 return false;
125 return ThreadManager::Singleton->joinThread(id);
126}
127
128
129// Static
130// Copy out the OS-level ThreadHandle for a manager slot
133 if (!CreateThreadManager())
134 return false;
135 return ThreadManager::Singleton->getThreadHandle(id, out);
136}
137
138
139// Static
140// Add thread stats for local thread - needed for pthreads
143 if (!CreateThreadManager())
144 return false;
145 return ThreadManager::Singleton->addLocalThreadStats();
146}
147
148// Static
149// Add thread stats for local thread - needed for pthreads
152 if (!CreateThreadManager())
153 return false;
154 return ThreadManager::Singleton->getLocalThreadID(id);
155}
156
157
158// Static
159// Get the statistics for local thread
161 ThreadStats stats;
162 stats.created = 0;
164 if (!CreateThreadManager())
165 return stats;
166 uint32 id;
168 return stats;
169 return ThreadManager::Singleton->getThreadStats(id);
170}
171
172// Static
173// Get the statistics for a thread
175 ThreadStats stats;
176 stats.created = 0;
178 if (!CreateThreadManager())
179 return stats;
180 return ThreadManager::Singleton->getThreadStats(id);
181}
182
183// Static
184// Get the statistics for all threads
187 if (!CreateThreadManager())
188 return NULL;
189 return ThreadManager::Singleton->getAllThreadStats(count);
190}
191
192// Static
193// Shutdown and delete the ThreadManager
196 return true;
198 return true;
201 return (ThreadManager::Singleton == NULL);
202}
203
204
205
206
208 // Initially allocate space for 1024 threads
209 data = NULL;
210 // Make the registry lock cancel-safe: teardown force-terminates worker threads
211 // via pthread_cancel, and a cancel landing on a thread that holds this lock
212 // (e.g. mid createThread/addLocalThreadStats) would orphan it and hang every
213 // later terminate/isRunning call for 30s. Disabling cancellation while the lock
214 // is held closes that window. See utils::Mutex::setCancelSafe().
215 mutex.setCancelSafe(true);
217 shouldContinue = true;
218 isRunning = false;
219}
220
222 if (ThreadManager::Singleton == NULL)
223 return;
224 if (!mutex.enter())
225 return;
226 shutdown();
227 // Deallocate data
229 free(data);
230 data = NULL;
231 mutex.leave();
232}
233
235 if (!mutex.enter())
236 return false;
237 // Startup the root thread for Windows only
238 uint32 newID = 0;
240 if ((!createThread(ThreadMonitoring, NULL, newID, 0)) || (newID != 0)) {
241 mutex.leave();
242 return false;
243 }
245 header->activeCount = 1;
246 mutex.leave();
247 return true;
248}
249
251 // Single-teardown guard: if another thread is already tearing the registry
252 // down, do NOT run a second concurrent teardown (it would collide on the
253 // registry mutex / thread joins and deadlock). Return false so the static
254 // Shutdown() wrapper skips deleting the singleton out from under the
255 // in-flight teardown. See g_threadMgrTearingDown.
257 return false;
258 stop();
259 if (!mutex.enter()) {
261 return false;
262 }
263 // Give running threads a chance to exit on their own before resorting to
264 // utils::TerminateThread(). Forcibly terminating a thread that is right now
265 // exiting naturally (worker loops poll their stop flags every <=1s) can kill
266 // it inside DLL_THREAD_DETACH / CRT teardown while it holds the loader or
267 // heap lock. That abandoned lock then blocks every NEWLY created thread
268 // before it reaches its thread function - the cause of the intermittent
269 // CMSDK processmemory/network test failures on Windows.
271 ThreadStats* stats;
272#ifdef WINDOWS
273 // The natural-exit wait is only needed on Windows, where the OS
274 // TerminateThread() can abandon the loader/heap lock. On POSIX,
275 // utils::TerminateThread() is pthread_cancel()+pthread_join(), which is
276 // safe to apply immediately (and waiting merely shifts where the
277 // cancellation lands, which destabilised the network tests on Linux).
278 uint64 waitStart = GetTimeNow();
279 bool anyRunning = true;
280 while (anyRunning && GetTimeAgeMS(waitStart) < 2500) {
281 anyRunning = false;
282 stats = GETTHREADSTATS(data,0);
283 for (uint32 n=0; n<header->count; n++, stats++) {
284 if (stats->status > THREAD_TERMINATED &&
285 !utils::TryReapThread(stats->hThread)) {
286 anyRunning = true;
287 break;
288 }
289 }
290 if (anyRunning)
291 utils::Sleep(50);
292 }
293#endif
294 // Terminate whatever is genuinely still running, mark the rest terminated
295 stats = GETTHREADSTATS(data,0);
296 for (uint32 n=0; n<header->count; n++) {
297 if (stats->status > THREAD_TERMINATED) {
298 terminateThread(stats->id);
299 }
300 stats += 1;
301 }
302 header->activeCount = 0;
303 mutex.leave();
305 return true;
306}
307
308
309
310
311// Create a new thread and start it
312bool ThreadManager::createThread(THREAD_FUNCTION func, void* args, uint32& newID, uint32 reqID) {
313// fprintf(stderr, "ThreadManager creating thread enter...\n");
314 if (!mutex.enter())
315 return false;
316// fprintf(stderr, "ThreadManager creating thread got mutex...\n");
318 if (header->activeCount == header->count) {
319 // we need to resize Thread storage
320 if (!resizeThreadStorage(header->count * 4)) {
321 mutex.leave();
322 return false;
323 }
324 header = (ThreadDataHeader*)data;
325 }
326
327// fprintf(stderr, "ThreadManager creating thread 1...\n");
328 ThreadStats* stats;
329 if (reqID == 0) {
330 // A bit cheeky as root = 0, but it works well for the root thread as this is the first one requested anyway.
331 if (!utils::GetFirstFreeBitLoc(((const char*)data+sizeof(ThreadDataHeader)), header->bitFieldSize, newID) || (newID >= header->count)) {
332 mutex.leave();
333 return false;
334 }
335 stats = GETTHREADSTATS(data, newID);
336 stats->id = newID;
337 }
338 else {
339 newID = reqID;
340 stats = GETTHREADSTATS(data, newID);
341 stats->id = newID;
342 }
343
344// fprintf(stderr, "ThreadManager creating thread 2...\n");
345 stats->created = GetTimeNow();
346 stats->status = THREAD_INIT;
347 // We assume the stats are 0 already
348 // Create the thread
349 if (!utils::CreateThread(func, args, stats->hThread, stats->osID)) {
350 newID = 0;
351 stats->created = 0;
352 stats->status = THREAD_NONE;
353 mutex.leave();
354 return false;
355 }
356
357 // printf("Create thread %u = %u\n", stats->id, (uint32)stats->hThread);
358 utils::SetBit(stats->id, BITOCCUPIED, ((char*)data+sizeof(ThreadDataHeader)), header->bitFieldSize);
359 stats->func = func;
360 stats->args = args;
361 stats->status = THREAD_RUNNING;
362 header->activeCount++;
363 //fprintf(stderr, "ThreadManager creating thread ID %u, trace:\n%s", stats->osID);
364 //fprintf(stderr, "ThreadManager creating thread ID %u, trace:\n%s", stats->osID, utils::PrintProgramTrace(3, 6).c_str());
365 mutex.leave();
366// fprintf(stderr, "ThreadManager creating thread 4...\n");
367 return true;
368}
369
370// Pause the thread wherever it is now
372 if (!mutex.enter())
373 return false;
374 ThreadStats* stats = GETTHREADSTATS(data, id);
375 // Pause the thread
376 if (!utils::PauseThread(stats->hThread)) {
377 mutex.leave();
378 return false;
379 }
380 stats->status = THREAD_PAUSED;
381 mutex.leave();
382 return true;
383}
384
385// Allow the thread to continue running
387 if (!mutex.enter())
388 return false;
389 ThreadStats* stats = GETTHREADSTATS(data, id);
390 // Continue the thread
391 if (!utils::ContinueThread(stats->hThread)) {
392 mutex.leave();
393 return false;
394 }
395 stats->status = THREAD_RUNNING;
396 mutex.leave();
397 return true;
398}
399
400// Terminate the thread and restart it from its base location
402 if (!mutex.enter())
403 return false;
404 ThreadStats* stats = GETTHREADSTATS(data, id);
405 stats->status = THREAD_INTERRUPTED;
406 // Terminate the thread
407 // Terminate the thread
408 if (!utils::TerminateThread(stats->hThread)) {
409 mutex.leave();
410 return false;
411 }
413 header->activeCount--; // it will be increased again in createThread
414 // Create the thread again
415 uint32 newID = 0;
416 if (!createThread(stats->func, stats->args, newID, id)) {
417 stats->status = THREAD_TERMINATED;
418 stats->func = NULL;
419 stats->created = 0;
420 header->activeCount--;
421 mutex.leave();
422 return false;
423 }
424 mutex.leave();
425 return false;
426}
427
428// Terminate and destroy the thread
430 if (!mutex.enter())
431 return false;
433 ThreadStats* stats = GETTHREADSTATS(data, id);
434 // Only force-terminate a thread that is actually still running. Killing a
435 // thread that already exited (or is mid-exit) can abandon the loader/heap
436 // lock and silently break all subsequent thread creation (see shutdown()).
437 if (!utils::TryReapThread(stats->hThread)) {
438#ifdef WINDOWS
439 // brief grace period for threads that are just about to exit; killing a
440 // thread inside its exit path can abandon the loader/heap lock (Windows)
441 uint64 graceStart = GetTimeNow();
442 while (!utils::TryReapThread(stats->hThread) && GetTimeAgeMS(graceStart) < 250)
443 utils::Sleep(25);
444 if (stats->hThread) {
445 utils::TerminateThread(stats->hThread); // terminates+closes (Windows)
446 stats->hThread = 0;
447 }
448#else
449 if (stats->hThread) {
450 // Never cancel+join the CALLING thread. If teardown is being driven from
451 // a registered worker (e.g. exit() -> CleanShutdown -> Shutdown() runs on
452 // a bake/network worker), shutdown()'s loop will reach that worker's own
453 // slot; pthread_cancel+pthread_join on self is undefined / self-deadlocks.
454 // Just detach the handle and mark it terminated; the thread is on its way
455 // out through exit() anyway.
456 if (pthread_equal(stats->hThread, pthread_self())) {
457 stats->hThread = 0;
458 }
459 else {
460 // The blocking part of TerminateThread on POSIX is pthread_cancel +
461 // pthread_join. It MUST NOT run while we hold the registry mutex: the
462 // target may be blocked in Mutex::enter() for this same lock, and while
463 // the lock is held it cannot acquire it - so joining here would deadlock
464 // main against the worker. Snapshot the handle, zero it in the slot so no
465 // other caller races the same join (double-join is undefined behaviour),
466 // release the mutex, then cancel+join. Re-acquire and re-fetch afterwards
467 // because the storage block may have been resized meanwhile.
468 ThreadHandle h = stats->hThread;
469 stats->hThread = 0;
470 mutex.leave();
471 utils::TerminateThread(h); // cancels + joins - NO registry lock held
472 if (!mutex.enter())
473 return false;
474 header = (ThreadDataHeader*) data;
475 stats = GETTHREADSTATS(data, id);
476 }
477 }
478#endif
479 }
480 //if (utils::IsThreadRunning(stats->hThread)) {
481 //if (!utils::TerminateThread(stats->hThread)) {
482 // mutex.leave();
483 // return false;
484 //}
485 //}
486 // Finalize idempotently: another terminate/monitor pass may have already
487 // reaped this slot while the mutex was released above.
488 finaliseSlot(stats, header);
489 mutex.leave();
490 return true;
491}
492
493
494// Has the thread terminated or exited
496 if (!mutex.enter())
497 return false;
499 ThreadStats* stats = GETTHREADSTATS(data, id);
500 if (stats->hThread == 0) {
501 mutex.leave(); // was leaking the manager mutex on this early return
502 return false;
503 }
504 bool res = utils::IsThreadRunning(stats->hThread);
505 mutex.leave();
506 return res;
507}
508
509
510// Copy out the OS-level ThreadHandle for a slot (by value, under the mutex)
512 if (!mutex.enter())
513 return false;
514 ThreadStats* stats = GETTHREADSTATS(data, id);
515 if (stats->hThread == 0) {
516 mutex.leave();
517 return false;
518 }
519 out = stats->hThread;
520 mutex.leave();
521 return true;
522}
523
524
525// Wait for a thread to have COMPLETELY finished, then release it - exactly once.
526// This is the ONLY sound "that thread is entirely gone" signal. A flag the worker sets
527// itself is necessarily set BEFORE it returns from its thread function, and long before
528// the runtime tears down its stack/TSD - so a reaper that frees on such a flag always
529// races the thread's own exit path (observed: a worker still inside pthread_exit/TSD
530// cleanup after its owning object had been freed).
532 if (!mutex.enter())
533 return false;
535 ThreadStats* stats = GETTHREADSTATS(data, id);
536 ThreadHandle h = stats->hThread;
537 if (h == 0) {
538 // Already reaped - by terminateThread(), the monitoring loop, or an earlier
539 // join. Nothing left to wait for.
540 mutex.leave();
541 return true;
542 }
543 // NEVER join the CALLING thread: teardown can be driven from a registered worker
544 // (exit() -> CleanShutdown -> Shutdown(), or a NetworkChannel worker's own exit-path
545 // endConnection()), and joining self deadlocks - pthread_join on self is undefined, and
546 // WaitForSingleObject on our own handle never returns. Leave the handle for the reaper
547 // that outlives us.
548 #ifndef WINDOWS
549 if (pthread_equal(h, pthread_self())) {
550 #else
551 if (::GetThreadId(h) == ::GetCurrentThreadId()) {
552 #endif
553 mutex.leave();
554 return true;
555 }
556 // TAKE EXCLUSIVE OWNERSHIP before joining: zero the slot so no other joiner - a
557 // concurrent terminateThread(), or the monitoring loop's TryReapThread() (Linux
558 // only; the loop exits immediately when statColPolicy is THREAD_STATS_OFF, which
559 // is the case on macOS) - can ever join the same pthread_t twice. Double-join is
560 // undefined behaviour and corrupts the heap. Correctness rests on THIS, not on
561 // the monitor being absent.
562 stats->hThread = 0;
563 // Release the registry mutex BEFORE joining: the target may itself be blocked in
564 // Mutex::enter() for this very lock, and while we hold it the target can never
565 // acquire it - so joining here would deadlock us against the thread we wait for.
566 mutex.leave();
567 #ifdef WINDOWS
568 // ReapThread() only CloseHandle()s on Windows - it does NOT wait - so the wait
569 // must be explicit first. Timeout 0 = infinite WaitForSingleObject.
572 #else
573 // POSIX: pthread_join both waits and releases the thread's resources, and
574 // ReapThread() zeroes its local copy of the handle afterwards.
575 // UNBOUNDED on purpose: a bounded join (pthread_timedjoin_np) that expires
576 // after we have zeroed the slot would leave the thread joinable by nobody -
577 // silently dropping the guarantee. Callers bound the COOPERATIVE phase
578 // (shouldContinue + a bounded isRunning wait + a loud log on overrun) and
579 // only then call this.
581 #endif
582 // Re-acquire and RE-FETCH: the storage block is malloc/memcpy'd and may have been
583 // resized while the mutex was released, so the earlier pointers can be stale.
584 if (!mutex.enter())
585 return true; // the thread IS joined; only the bookkeeping below is missed
586 header = (ThreadDataHeader*) data;
587 stats = GETTHREADSTATS(data, id);
588 // Finalise idempotently: terminateThread() or a monitor pass may already have
589 // marked this slot while our mutex was released. This RELEASES THE SLOT as well
590 // as marking it - see finaliseSlot(). It used to be an inline copy of
591 // terminateThread's block that did not free the bit, so every join leaked a
592 // registry slot: the leak survived its own fix on the path that matters most,
593 // since all four NetworkChannel teardown sites reap via JoinThread and on macOS
594 // the monitor loop never reaps at all. We have already zeroed stats->hThread
595 // above, so finaliseSlot's handle guard is always satisfied here.
596 finaliseSlot(stats, header);
597 mutex.leave();
598 return true;
599}
600
601
602// Mark a slot terminated and release it back to the allocation bitfield.
603// Caller must hold the registry mutex, with stats/header re-fetched after any
604// release of it. Shared by terminateThread() and joinThread() so the slot-release
605// cannot be present in one and missing in the other. @see finaliseSlot() docs.
607 if (!stats || !header)
608 return;
609 if (stats->status == THREAD_TERMINATED)
610 return; // already finalised by another terminate/join/monitor pass
611 stats->status = THREAD_TERMINATED;
612 stats->func = NULL;
613 stats->created = 0;
614 header->activeCount--;
615 // Release the registry slot back to the bitfield, or the process leaks one slot
616 // per thread lifetime: createThread only ever SetBit(BITOCCUPIED) and nothing
617 // used to clear it, so after `count` create/terminate cycles GetFirstFreeBitLoc
618 // had nothing left and thread creation failed forever. resizeThreadStorage could
619 // not rescue it either - it triggers on activeCount == count, and activeCount
620 // DOES decrement here, so the registry looked like it had spare capacity while
621 // being unable to allocate.
622 //
623 // ORDERING IS LOAD-BEARING: only release the bit once hThread is zero. Freeing it
624 // while the OS thread is still unwinding would let a concurrent createThread hand
625 // out this slot and overwrite ThreadStats under the dying thread - trading a
626 // capacity bug for a use-after-free. Callers either zero the handle themselves or
627 // went through TryReapThread (which zeroes it), but assume nothing: a non-zero
628 // handle here means an unreaped thread, so keep the slot occupied and leak it,
629 // which is the old, safe behaviour.
630 if (!stats->hThread)
631 utils::SetBit(stats->id, BITFREE, ((char*)data+sizeof(ThreadDataHeader)), header->bitFieldSize);
632}
633
634
635// Get the statistics for a thread
637 ThreadStats stats;
638 stats.created = 0;
639 if (!mutex.enter()) {
640 mutex.leave();
641 return stats;
642 }
643 stats = *GETTHREADSTATS(data, id);
644 mutex.leave();
645 return stats;
646}
647
648// Get the statistics for all threads
650 if (!mutex.enter())
651 return NULL;
653 count = header->activeCount;
654 ThreadStats* allStats = new ThreadStats[count];
655 uint32 p=0;
656 ThreadStats* stats = GETTHREADSTATS(data,0);
657 for (uint32 n=0; n<header->count; n++) {
658 if (stats->status > THREAD_TERMINATED)
659 allStats[p++] = *stats;
660 stats += 1;
661 if (p >= count) break;
662 }
663 mutex.leave();
664 return allStats;
665}
666
667// Resize the ThreadStats storage to be able to contain more threads
669
670 uint32 bitFieldSize = utils::Calc32BitFieldSize(newCount);
671 uint32 size = sizeof(ThreadDataHeader) + bitFieldSize + newCount*sizeof(ThreadStats);
672 unsigned char *newData = (unsigned char*) malloc(size);
673 memset(newData, 0, size);
674 ThreadDataHeader* header = (ThreadDataHeader*) newData;
675 header->bitFieldSize = bitFieldSize;
676 header->count = newCount;
677 header->size = size;
679
680 uint32 statsOffset = sizeof(header->size)+sizeof(header->count)+sizeof(header->activeCount)+sizeof(header->statColPolicy);
681
682 if (data == NULL) {
683 header->activeCount = 0;
684 // Set summary stats to 0
685 memset(newData+statsOffset, 0, 10*2*(sizeof(uint64)));
686 // Set the bitfield to unused
687 memset(newData+sizeof(ThreadDataHeader), 255, bitFieldSize);
688 // Set all thread stats to 0
689 ThreadStats* firstStat = GETTHREADSTATS(newData,0);
690 memset(firstStat, 0, newCount*sizeof(ThreadStats));
691 }
692 else {
693 ThreadDataHeader* oldHeader = (ThreadDataHeader*) data;
694 header->activeCount = oldHeader->activeCount;
695 // Copy old summary stats
696 memcpy(newData+statsOffset, data+statsOffset, 10*2*(sizeof(uint64)));
697 // Initially, set the bitfield to unused
698 memset(newData+sizeof(ThreadDataHeader), 255, bitFieldSize);
699 // Copy old bitfield
700 memcpy(newData+sizeof(ThreadDataHeader), data+sizeof(ThreadDataHeader), oldHeader->bitFieldSize);
701 ThreadStats* firstStat = GETTHREADSTATS(newData,0);
702 ThreadStats* oldFirstStat = GETTHREADSTATS(data,0);
703 memcpy(firstStat, oldFirstStat, oldHeader->count*sizeof(ThreadStats));
704 free(data);
705 }
706 data = newData;
707 return true;
708}
709
710// Add thread stats for local thread - needed for pthreads
712 if (!mutex.enter())
713 return false;
714
715 uint32 id = 0;
716 if (!getLocalThreadID(id)) {
717 mutex.leave();
718 return false;
719 }
720 ThreadStats* stats = GETTHREADSTATS(data,id);
721 if ( (stats == NULL) || (stats->created == 0) ) {
722 mutex.leave();
723 return false;
724 }
725
726 uint32 timeOffset = sizeof(stats->id) + sizeof(stats->created) + sizeof(stats->status)
727 + sizeof(stats->hThread) + sizeof(stats->func) + sizeof(stats->args);
728 uint32 cpuUsageOffset = timeOffset + 10 * sizeof(uint64);
729
730 ThreadDataHeader* header = NULL;
731
732// uint32 headerTimeOffset = sizeof(header->size)+sizeof(header->count)+sizeof(header->activeCount)+sizeof(header->statColPolicy);
733// uint32 headerCPUUsageOffset = headerTimeOffset + 10 * sizeof(uint64);
734
735 // Shift the previously stored values Time
736 memcpy(stats+timeOffset, stats+timeOffset+sizeof(uint64), 9*sizeof(uint64));
737 // CPU Usage
738 memcpy(stats+cpuUsageOffset, stats+cpuUsageOffset+sizeof(uint64), 9*sizeof(uint64));
739 // Read current stats from the thread
740 if (utils::GetCPUTicks(stats->currentCPUTicks[0])) {
741 stats->time[0] = GetTimeNow();
742 // add these to the header stat sums
743 header = (ThreadDataHeader*) data;
744 header->time[0] = stats->time[0];
745 header->currentCPUTicks[0] += stats->currentCPUTicks[0];
746 // but do not rotate
747 }
748 else {
749 stats->time[0] = stats->currentCPUTicks[0] = 0;
750 mutex.leave();
751 return false;
752 }
753
754 mutex.leave();
755 return true;
756}
757
758// Get the local thread ID
760 if (!mutex.enter())
761 return false;
762
763 uint32 osID;
765 mutex.leave();
766 return false;
767 }
769
770 ThreadStats* stats = GETTHREADSTATS(data,0);
771 for (uint32 n=0; n<header->count; n++) {
772 if (stats->osID == osID) {
773 id = stats->id;
774 mutex.leave();
775 return true;
776 }
777 stats += 1;
778 }
779
780 // for pthreads one has to delete the handle afterwards
781 //#ifndef WINDOWS
782 // delete(hThread);
783 //#endif
784 mutex.leave();
785 return false;
786}
787
788
789// Root Thread, for Windows only until pthreads support getrusage from other threads
791
792 #ifdef WINDOWS
793 #else
794 #ifndef Darwin
795 sigset_t cancel;
796 sigemptyset(&cancel);
797 sigaddset(&cancel, SIGQUIT);
798 pthread_sigmask(SIG_UNBLOCK, &cancel, NULL);
799 #endif
800 #endif
801
803 return 0;
804
806}
807
809
810 isRunning = true;
811
812 ThreadStats* stats = NULL;
813 uint32 timeOffset = sizeof(stats->id) + sizeof(stats->created) + sizeof(stats->status)
814 + sizeof(stats->hThread) + sizeof(stats->func) + sizeof(stats->args);
815 uint32 cpuUsageOffset = timeOffset + 10 * sizeof(uint64);
816
818 if (header->statColPolicy == THREAD_STATS_OFF) {
819 isRunning = false;
820 return 0; // do not continue as nothing is done anyway
821 }
822
823 uint32 headerTimeOffset = sizeof(header->size)+sizeof(header->count)+sizeof(header->activeCount)+sizeof(header->statColPolicy);
824 uint32 headerCPUUsageOffset = headerTimeOffset + 10 * sizeof(uint64);
825
826 uint64 sumUsage;
827 uint32 interval = 1000000, n;
828
829 uint64 lastCalc = 0, t;
830 while (shouldContinue) {
831 if ( (t = GetTimeNow()) - lastCalc > interval ) {
832 lastCalc = t;
833
834 if (!mutex.enter()) {
835 isRunning = false;
836 return -1;
837 }
838
839 header = (ThreadDataHeader*) data;
840 stats = GETTHREADSTATS(data,0);
841
842 if (header->statColPolicy == THREAD_STATS_ADHOC) {
843 // Threads have to call the stat gathering themselves
844 // so we just rotate the global stats
845
846 for (n=0; n<header->count; n++) {
847 if (stats->status > THREAD_TERMINATED) {
848 // check if it is still actually running
849 if (utils::TryReapThread(stats->hThread)) { // reaps exactly once; zeroes handle
850 stats->status = THREAD_TERMINATED;
851 LogPrint(0, LOG_PROCESS, 2, "Thread finished: ID %u (OSID %u) age: %s", stats->id, stats->osID, PrintTimeDifString((uint32)GetTimeAge(stats->created)).c_str());
852 }
853 }
854 stats += 1;
855 }
856
857 // Shift the previously stored values Time
858 memcpy(header+headerTimeOffset, header+headerTimeOffset+sizeof(uint64), 9*sizeof(uint64));
859 // CPU Usage
860 memcpy(header+headerCPUUsageOffset, header+headerCPUUsageOffset+sizeof(uint64), 9*sizeof(uint64));
861
862 header->time[0] = header->currentCPUTicks[0] = 0;
863 }
864 else {
865 // We have to gather the stats, sum them up and rotate all buffers
866 sumUsage = 0;
867
868 for (n=0; n<header->count; n++) {
869 if (stats->status > THREAD_TERMINATED) {
870 // Shift the previously stored values Time
871 memcpy(stats+timeOffset, stats+timeOffset+sizeof(uint64), 9*sizeof(uint64));
872 // CPU Usage
873 memcpy(stats+cpuUsageOffset, stats+cpuUsageOffset+sizeof(uint64), 9*sizeof(uint64));
874 // Read current stats from the thread
875 if (utils::GetCPUTicks(stats->hThread, stats->currentCPUTicks[0])) {
876 stats->time[0] = GetTimeNow();
877 sumUsage += stats->currentCPUTicks[0];
878 }
879 else {
880 stats->time[0] = stats->currentCPUTicks[0] = 0;
881 }
882 // check if it is still actually running
883 if (utils::TryReapThread(stats->hThread)) { // reaps exactly once; zeroes handle
884 stats->status = THREAD_TERMINATED;
885 LogPrint(0, LOG_PROCESS, 2, "Thread finished: ID %u (OSID %u) age: %s", stats->id, stats->osID, PrintTimeDifString((uint32)GetTimeAge(stats->created)).c_str());
886 }
887 }
888 stats += 1;
889 }
890
891 // Shift the previously stored values Time
892 memcpy(header+headerTimeOffset, header+headerTimeOffset+sizeof(uint64), 9*sizeof(uint64));
893 // CPU Usage
894 memcpy(header+headerCPUUsageOffset, header+headerCPUUsageOffset+sizeof(uint64), 9*sizeof(uint64));
895
896 header->time[0] = GetTimeNow();
897 header->currentCPUTicks[0] = sumUsage;
898 }
899
900 mutex.leave();
901 }
902
903 utils::Sleep(100);
904 }
905
906 isRunning = false;
907 return 0;
908}
909
910
911// ################# Unit test #################
912
913namespace {
914
915// Shared state for the ThreadManager unit test. Each worker thread does a
916// bounded amount of work (a fixed number of increments), accumulates its
917// result into a shared total under a mutex, then marks itself finished.
918struct TMTestState {
919 utils::Mutex mutex;
920 uint64 total; // sum of all per-thread work, protected by mutex
921 uint32 finishedCount; // number of worker threads that ran to completion
922 TMTestState() : total(0), finishedCount(0) {}
923};
924
925// Each worker performs this many increments. Kept modest so the whole test
926// finishes in well under a second, but large enough to be measurable.
927static const uint64 TM_ITERATIONS_PER_THREAD = 200000ULL;
928
929static THREAD_RET THREAD_FUNCTION_CALL TMTestWorker(THREAD_ARG arg) {
930 TMTestState* state = (TMTestState*)arg;
931 uint64 localSum = 0;
932 for (uint64 i = 0; i < TM_ITERATIONS_PER_THREAD; i++)
933 localSum += 1;
934 if (state->mutex.enter()) {
935 state->total += localSum;
936 state->finishedCount++;
937 state->mutex.leave();
938 }
940}
941
942// Worker for the slot-recycling test: exits immediately, so the test can churn
943// many thread LIFETIMES cheaply rather than doing work.
944static THREAD_RET THREAD_FUNCTION_CALL TMSlotChurnWorker(THREAD_ARG arg) {
945 (void)arg;
947}
948
949} // anonymous namespace
950
952
953 // 1. Bring up the ThreadManager singleton.
954 unittest::progress(5, "create thread manager");
956 unittest::fail("ThreadManager test: CreateThreadManager() failed");
957 return false;
958 }
959
960 const uint32 NUMTHREADS = 8;
961 TMTestState state;
962 uint32 ids[NUMTHREADS];
963 for (uint32 n = 0; n < NUMTHREADS; n++)
964 ids[n] = 0;
965
966 // 2. Start the worker threads.
967 unittest::progress(20, "start worker threads");
968 uint64 t0 = GetTimeNow();
969 uint32 started = 0;
970 for (uint32 n = 0; n < NUMTHREADS; n++) {
971 if (!ThreadManager::CreateThread(TMTestWorker, &state, ids[n])) {
972 unittest::fail("ThreadManager test: CreateThread() failed for worker %u", n);
974 return false;
975 }
976 unittest::detail("started worker %u with id %u", n, ids[n]);
977 started++;
978 }
979 if (started != NUMTHREADS) {
980 unittest::fail("ThreadManager test: started %u of %u threads", started, NUMTHREADS);
982 return false;
983 }
984
985 // 3. Wait (bounded) for every worker to finish its work. We poll the shared
986 // finished counter and also the OS-level running state, with a hard
987 // timeout so the test can never hang.
988 unittest::progress(50, "wait for workers");
989 const uint32 TIMEOUTMS = 5000;
990 uint32 waitedMs = 0;
991 bool allFinished = false;
992 while (waitedMs < TIMEOUTMS) {
993 uint32 done = 0;
994 if (state.mutex.enter()) {
995 done = state.finishedCount;
996 state.mutex.leave();
997 }
998 if (done >= NUMTHREADS) {
999 allFinished = true;
1000 break;
1001 }
1002 utils::Sleep(5);
1003 waitedMs += 5;
1004 }
1005
1006 if (!allFinished) {
1007 unittest::fail("ThreadManager test: only %u of %u workers finished within %ums",
1008 state.finishedCount, NUMTHREADS, TIMEOUTMS);
1010 return false;
1011 }
1012
1013 // 4. Verify the threads actually all ran and produced the expected total.
1014 unittest::progress(75, "verify results");
1015 uint64 expected = (uint64)NUMTHREADS * TM_ITERATIONS_PER_THREAD;
1016 if (state.total != expected) {
1017 unittest::fail("ThreadManager test: total work %llu != expected %llu",
1018 state.total, expected);
1020 return false;
1021 }
1022 if (state.finishedCount != NUMTHREADS) {
1023 unittest::fail("ThreadManager test: finishedCount %u != %u",
1024 state.finishedCount, NUMTHREADS);
1026 return false;
1027 }
1028
1029 // 5. Sanity-check the statistics API for one of the threads.
1031 if (stats.created == 0) {
1032 unittest::fail("ThreadManager test: GetThreadStats returned no creation time for id %u", ids[0]);
1034 return false;
1035 }
1036
1037 double us = (double)(GetTimeNow() - t0);
1038 if (us > 0.0) {
1039 unittest::metric("threads_per_sec", (double)NUMTHREADS / us * 1e6, "threads/s", true);
1040 unittest::metric("total_run_time", us, "us", false);
1041 }
1042
1043 // 6. Tear everything down cleanly. No threads remain after this point.
1044 unittest::progress(90, "shutdown");
1045 if (!ThreadManager::Shutdown()) {
1046 unittest::fail("ThreadManager test: Shutdown() failed");
1047 return false;
1048 }
1049
1050 unittest::progress(100, "done");
1051 return true;
1052}
1053
1054// Registry slot recycling: create and reap MORE threads than the registry has
1055// slots, in ONE process, and require that creation keeps working.
1056//
1057// This is the test that would have caught the slot leak by itself. createThread()
1058// marks a slot BITOCCUPIED, and until the D1 fix nothing ever cleared it, so after
1059// `count` lifetimes GetFirstFreeBitLoc had nothing left and thread creation failed
1060// permanently - while activeCount kept decrementing, so the registry reported spare
1061// capacity. Every existing suite test forks per run and creates a handful of
1062// threads, which is exactly why this stayed invisible.
1063//
1064// It deliberately reaps via ThreadManager::JoinThread(), NOT terminateThread():
1065// the initial D1 fix released the slot only in terminateThread's finalize block,
1066// and JoinThread had a second, divergent copy that did not - so a terminateThread
1067// based test would have passed against the leak. JoinThread is also the path every
1068// NetworkChannel teardown uses, and on macOS it is the ONLY reaper, because the
1069// monitoring loop returns early when statColPolicy is THREAD_STATS_OFF.
1071 unittest::progress(5, "create thread manager");
1073 unittest::fail("slot recycling: CreateThreadManager() failed");
1074 return false;
1075 }
1076
1077 // The registry starts with 1024 slots. Churn comfortably past that so an
1078 // unreleased slot per lifetime must exhaust the bitfield.
1079 const uint32 CYCLES = 1300;
1080 uint32 created = 0, failedAt = 0;
1081 uint32 maxID = 0;
1082
1083 unittest::progress(20, "churn thread lifetimes");
1084 for (uint32 n = 0; n < CYCLES; n++) {
1085 uint32 id = 0;
1086 if (!ThreadManager::CreateThread(TMSlotChurnWorker, NULL, id)) {
1087 failedAt = n;
1088 break;
1089 }
1090 created++;
1091 if (id > maxID)
1092 maxID = id;
1093 // Reap it. JoinThread waits for the OS thread and finalises the slot, which
1094 // is where the release has to happen.
1096 }
1097
1098 unittest::progress(80, "verify");
1099 if (failedAt) {
1100 unittest::fail("slot recycling: CreateThread() failed after %u lifetimes "
1101 "(highest slot id seen %u) - registry slots are not being released",
1102 failedAt, maxID);
1104 return false;
1105 }
1106 if (created != CYCLES) {
1107 unittest::fail("slot recycling: completed %u of %u lifetimes", created, CYCLES);
1109 return false;
1110 }
1111 // Slots must be REUSED, not merely available: if ids climb monotonically past
1112 // the registry size then something grew the storage instead of recycling.
1113 if (maxID >= CYCLES) {
1114 unittest::fail("slot recycling: highest slot id %u >= %u lifetimes - ids are "
1115 "not being recycled", maxID, CYCLES);
1117 return false;
1118 }
1119 unittest::detail("%u thread lifetimes, highest slot id %u", created, maxID);
1120 unittest::metric("thread_lifetimes", (double)created, "", true);
1121 unittest::metric("highest_slot_id", (double)maxID, "", false);
1122
1123 unittest::progress(90, "shutdown");
1124 if (!ThreadManager::Shutdown()) {
1125 unittest::fail("slot recycling: Shutdown() failed");
1126 return false;
1127 }
1128 unittest::progress(100, "done");
1129 return true;
1130}
1131
1134 "Thread manager create/run/join of multiple worker threads", "core");
1136 "Thread manager releases and reuses registry slots across >1024 lifetimes", "core");
1137}
1138
1139} // namespace cmlabs
CMSDK time: µs-resolution 64-bit timestamps and the Time Mapping Constant (TMC).
Process-wide thread registry and lifecycle manager: the concurrency core of CMSDK.
#define BITOCCUPIED
Definition Types.h:34
#define BITFREE
Definition Types.h:33
Small, dependency-free unit test harness used by all CMSDK object tests.
Cross-platform utility toolbox for CMSDK: threading, synchronization, shared memory,...
#define THREAD_NONE
Definition Utils.h:93
#define thread_ret_val(ret)
Definition Utils.h:131
#define THREAD_STATS_ADHOC
Definition Utils.h:102
#define THREAD_STATS_OFF
Definition Utils.h:100
#define LOG_PROCESS
Definition Utils.h:217
#define THREAD_RET
Definition Utils.h:127
#define THREAD_TERMINATED
Definition Utils.h:94
#define THREAD_FUNCTION_CALL
Definition Utils.h:129
#define LogPrint
Definition Utils.h:330
#define THREAD_PAUSED
Definition Utils.h:97
#define THREAD_RUNNING
Definition Utils.h:96
THREAD_RET(* THREAD_FUNCTION)(void *)
Definition Utils.h:128
#define THREAD_INTERRUPTED
Definition Utils.h:98
#define ThreadHandle
Definition Utils.h:125
#define THREAD_INIT
Definition Utils.h:95
#define THREAD_ARG
Definition Utils.h:130
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.
bool shouldContinue
Loop-continuation flag; cleared by stop().
Singleton registry that creates, controls and profiles all CMSDK threads.
static bool UnitTestSlotRecycling()
Churn MORE thread lifetimes than the registry has slots, in one process.
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().
bool joinThread(uint32 id)
Instance-side worker for JoinThread().
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.
static bool GetThreadHandle(uint32 id, ThreadHandle &out)
Copy out the OS-level handle of the thread in a manager slot.
~ThreadManager()
Destructor: shuts down all threads, frees the storage block and clears the singleton pointer.
bool getThreadHandle(uint32 id, ThreadHandle &out)
Instance-side worker for GetThreadHandle().
void finaliseSlot(ThreadStats *stats, ThreadDataHeader *header)
Mark a slot terminated and RELEASE it back to the allocation bitfield.
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).
static bool JoinThread(uint32 id)
Wait for a thread to have COMPLETELY finished, then release it - exactly once.
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().
static UnitTestRunner & instance()
Access the singleton (created on first use).
void registerTest(const char *name, UnitTestFunc func, const char *description="", const char *category="", bool inDefaultRun=true, uint32 timeoutSec=0)
Register a test with the runner.
Recursive mutual-exclusion lock, optionally named for cross-process use.
Definition Utils.h:480
bool leave()
Release the mutex.
Definition Utils.cpp:1704
bool enter()
Block until the mutex is acquired.
Definition Utils.cpp:1512
uint64 GetTimeNow()
Return the current absolute time (µs since year 0) according to the TMC.
Definition PsyTime.cpp:69
std::string PrintTimeDifString(uint64 t, bool us=true, bool ms=true)
Definition PsyTime.cpp:722
int32 GetTimeAgeMS(uint64 t)
Age of a timestamp relative to now, in milliseconds.
Definition PsyTime.cpp:35
int64 GetTimeAge(uint64 t)
Age of a timestamp relative to now.
Definition PsyTime.cpp:25
static THREAD_RET THREAD_FUNCTION_CALL ThreadMonitoring(void *arg)
Entry function of the monitoring thread; delegates to ThreadManager::threadMonitoring().
#define GETTHREADSTATS(data, id)
Compute the address of thread slot id inside storage block data.
uint32 GetThreadStatColAbility()
Report this platform's capability for per-thread CPU statistics collection.
Definition Utils.cpp:4541
bool WaitForThreadToFinish(ThreadHandle hThread, uint32 timeoutMS=0)
Join a thread.
Definition Utils.cpp:4266
bool Sleep(uint32 ms)
Suspend the calling thread.
Definition Utils.cpp:4078
bool PauseThread(ThreadHandle hThread)
Suspend a thread's execution.
Definition Utils.cpp:4441
bool IsThreadRunning(ThreadHandle hThread)
Check whether a thread is still alive.
Definition Utils.cpp:4511
bool CreateThread(THREAD_FUNCTION func, void *args, ThreadHandle &thread, uint32 &osID)
Start a new OS thread.
Definition Utils.cpp:4144
bool ReapThread(ThreadHandle &hThread)
Definition Utils.cpp:4224
bool TerminateThread(ThreadHandle hThread)
Forcibly kill a thread.
Definition Utils.cpp:4316
bool GetCurrentThreadUniqueID(uint32 &tid)
Get a process-unique id for the calling thread.
Definition Utils.cpp:4461
bool ContinueThread(ThreadHandle hThread)
Resume a thread paused with PauseThread().
Definition Utils.cpp:4450
bool GetFirstFreeBitLoc(const char *bitfield, uint32 bytesize, uint32 &loc)
Find the first 0 (free) bit.
Definition Utils.cpp:3690
bool GetCPUTicks(ThreadHandle hThread, uint64 &ticks)
Get accumulated CPU time of a specific thread.
Definition Utils.cpp:4553
bool SetBit(uint32 loc, bit value, char *bitfield, uint32 bytesize)
Set bit loc to value.
Definition Utils.cpp:3845
bool TryReapThread(ThreadHandle &hThread)
Definition Utils.cpp:4240
uint32 Calc32BitFieldSize(uint32 bitsize)
Compute the byte size needed for a bitfield of bitsize bits, rounded up to a 32-bit boundary.
Definition Utils.cpp:3680
void fail(const char *fmt,...)
Set an explanatory reason shown on the FAIL line.
void metric(const char *name, double value, const char *unit="", bool higherIsBetter=true)
Record a performance metric.
void detail(const char *fmt,...)
Verbose-only indented diagnostic line (shown only when verbose=1).
void progress(int percent, const char *action)
Report progress with a short description of the current action.
static bool TryBeginThreadMgrTeardown()
static volatile int32 g_threadMgrTearingDown
static void EndThreadMgrTeardown()
void Register_ThreadManager_Tests()
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.
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.
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.