1#if !ALIB_SINGLE_THREADED
28 std::atomic<ThreadID> nextThreadIdx(1);
29 Thread* MAIN_THREAD =
nullptr;
31 #if ALIB_MONOMEM && ALIB_CONTAINERS
33 #define LOCK_THREADS ALIB_LOCK_RECURSIVE_WITH( monomem::GLOBAL_ALLOCATOR_LOCK )
35 std::unordered_map< std::thread::id, Thread*> THREAD_MAP;
37 #define LOCK_THREADS ALIB_LOCK_WITH( MODULE_LOCK );
60using namespace detail;
65#if ALIB_DEBUG && !DOXYGEN
66namespace{
unsigned initFlag= 0; }
70 #if ALIB_MONOMEM && ALIB_CONTAINERS
73 THREAD_MAP.reserve(
size_t(qty) );
77 ALIB_ASSERT_ERROR( initFlag == 0,
"THREADS",
"This method must not be invoked twice." )
89 #if ALIB_MONOMEM && ALIB_CONTAINERS
90 THREAD_MAP.EmplaceUnique( std::this_thread::get_id(), mainThread );
92 THREAD_MAP.insert( std::make_pair( std::this_thread::get_id(), mainThread) );
95 MAIN_THREAD= mainThread;
100 ALIB_ASSERT_ERROR( initFlag == 0x92A3EF61,
"THREADS",
"Not initialized when calling shutdown." )
104 if( MAIN_THREAD ==
nullptr )
111 #if ALIB_MONOMEM && ALIB_CONTAINERS
112 auto qtyThreads= THREAD_MAP.Size();
114 auto qtyThreads= THREAD_MAP.size();
117 if( qtyThreads != 1 ) {
119 std::vector<std::any> msgs;
121 msgs.emplace_back(
"ALib Termination: Still {} threads running.\n" );
122 msgs.emplace_back( qtyThreads);
124 for(
auto& it : THREAD_MAP ) {
125 msgs.emplace_back(
" {}: {},\tState::{}\n");
126 msgs.emplace_back(++tNr);
127 msgs.emplace_back(it.second);
128 msgs.emplace_back(it.second->state);
134 MAIN_THREAD=
nullptr;
138 Thread* lastThread= THREAD_MAP.begin()->second;
140 "Last thread {} is not the main system thread detected in bootstrap.", lastThread->
id )
143 MAIN_THREAD=
nullptr;
144 #if ALIB_MONOMEM && ALIB_CONTAINERS
166 "Thread \"{}\" destructed, while it was never started.",
this )
170 "Thread \"{}\" was not terminated before destruction.\n"
171 "Use Thread::Join() to avoid this message. Joining now...",
this )
186 "Terminating thread \"{}\" which is not in state 'Done'. State: {}.",
193 ALIB_WARNING(
"THREADS",
"Thread \"{}\" not joinable. State is '{}'.",
this,
state )
199 #if ALIB_MONOMEM && ALIB_CONTAINERS
215 "Double invocation of Thread::Terminate for thread \"{}\".",
this )
218 "Terminating thread \"{}\" which is not started or not managed by ALIB.",
226 ALIB_ERROR(
"THREADS",
"Thread with ID {} was already started.",
GetID() )
231 ALIB_ERROR(
"THREADS",
"System threads cannot be started. (ID={}).",
GetID() )
241 #if ALIB_MONOMEM && ALIB_CONTAINERS
242 THREAD_MAP.EmplaceUnique(
nativeID,
this );
244 THREAD_MAP.insert( std::make_pair(
nativeID,
this) );
263 #if ALIB_MONOMEM && ALIB_CONTAINERS
264 auto search= THREAD_MAP.Find(
nativeID );
266 auto search= THREAD_MAP.find(
nativeID );
270 if ( search != THREAD_MAP.end() )
271 result= search->second;
276 result->
id = nextSystemThreadId--;
279 #if ALIB_MONOMEM && ALIB_CONTAINERS
280 THREAD_MAP.EmplaceUnique(
nativeID, result );
282 THREAD_MAP.insert( std::make_pair(
nativeID, result) );
#define ALIB_ASSERT_RESULT_EQUALS( func, value)
#define ALIB_WARNING(domain,...)
#define ALIB_ASSERT_WARNING(cond, domain,...)
#define ALIB_ERROR(domain,...)
#define ALIB_ASSERT_ERROR(cond, domain,...)
#define ALIB_CALLER_PRUNED
@ Running
The thread's #".Run" method is currently processed.
@ Started
Method #".Start" was invoked but not running, yet.
@ Terminated
The thread is terminated.
static Thread * GetCurrent()
std::thread::id nativeID
The internal C++ thread id.
const character * name
The name of the thread.
Thread(const character *pName=A_CHAR(""))
virtual void SetName(const character *newName)
virtual void Run() override
std::thread * c11Thread
The internal C++ thread object.
State state
Internal flag to indicate if the thread is alive.
Runnable * runnable
The runnable to execute.
ThreadID id
The id of the thread.
static Thread * GetMain()
static Thread * Get(std::thread::id nativeID)
void raise(const CallerInfo &ci, int type, std::string_view domain, const std::span< std::any > &args)
constexpr bool IsNull(const T &t)
@ Absolute
Referring to an absolute value.
TMonoAllocator< lang::HeapAllocator > GLOBAL_ALLOCATOR
Details of namespace #"alib::threads;2".
void threadStart(Thread *thread)
void BootstrapThreadMap(integer qty)
Thread * THIS_THREAD
A thread-local pointer to the ALib representation of the actual thread.
integer ThreadID
The ALib thread identifier type.
threads::Thread Thread
Type alias in namespace #"%alib".
lang::integer integer
Type alias in namespace #"%alib".
threads::Runnable Runnable
Type alias in namespace #"%alib".
containers::HashMap< TAllocator, TKey, TMapped, THash, TEqual, THashCaching, TRecycling > HashMap
Type alias in namespace #"%alib".
characters::character character
Type alias in namespace #"%alib".