12# include <condition_variable>
46 std::atomic<ThreadID> nextThreadIdx(1);
47 Thread* MAIN_THREAD =
nullptr;
59#if (ALIB_MONOMEM && ALIB_CONTAINERS) || DOXYGEN
66 #define LOCK_THREADS ALIB_LOCK_RECURSIVE_WITH( monomem::GLOBAL_ALLOCATOR_LOCK )
68 std::unordered_map<std::thread::id, Thread *>
THREAD_MAP;
70 #define LOCK_THREADS std::unique_lock<std::mutex> uniqueLock( MODULE_LOCK );
84using namespace detail;
89#if ALIB_DEBUG && !DOXYGEN
90 namespace{
unsigned int initFlag= 0; }
95 ALIB_ASSERT_ERROR( initFlag == 0,
"THREADS",
"This method must not be invoked twice." )
107 #if ALIB_MONOMEM && ALIB_CONTAINERS
108 THREAD_MAP.EmplaceUnique( std::this_thread::get_id(), mainThread );
110 THREAD_MAP.insert( std::make_pair( std::this_thread::get_id(), mainThread) );
114 #if ALIB_ENUMS && ALIB_BOXING && !ALIB_CAMP
129 MAIN_THREAD= mainThread;
135 ALIB_ASSERT_ERROR( initFlag == 0x92A3EF61,
"THREADS",
"Not initialized when calling shutdown." )
139 if( MAIN_THREAD ==
nullptr )
146 #if ALIB_MONOMEM && ALIB_CONTAINERS
152 if( qtyThreads != 1 )
155 String4K dbgThreadList(
"ALib Termination: More than one thread running: ");
156 dbgThreadList << static_cast<integer>(qtyThreads) <<
NEW_LINE;
159 dbgThreadList << ++tNr <<
": " << it.second <<
",\tState::" <<
160 #if ALIB_ENUMS && ALIB_BOXING
163 int(it.second->state )
171 MAIN_THREAD=
nullptr;
177 "threads::Shutdown: last thread is not the main system thread detected "
178 "in threads::Bootstrap" )
181 MAIN_THREAD=
nullptr;
182 #if ALIB_MONOMEM && ALIB_CONTAINERS
202 name._( String128()<<
'(' << id <<
')');
209 "Thread \""<<
this <<
"\" destructed, while it was never started." )
214 "Thread \"" <<
this <<
"\" was not terminated before destruction.\n"
215 "Use Thread::Join() to avoid this message. Joining now!..." )
227 msg <<
"Terminating thread \"" <<
this <<
"\" which is not in state 'Stopped'. State: '"
228 #if ALIB_ENUMS && ALIB_BOXING
231 << int(
state) <<
"'.";
242 msg <<
"Thread \"" <<
this <<
"\" not joinable. State: '"
243 #if ALIB_ENUMS && ALIB_BOXING
246 << int(
state) <<
"'.";
256 #if ALIB_MONOMEM && ALIB_CONTAINERS
273 "Double invocation of Thread::Terminate for thread {!Q}",
277 "Terminating thread {!Q} which is not started or not managed by ALIB",
307 #if ALIB_MONOMEM && ALIB_CONTAINERS
332 #if ALIB_MONOMEM && ALIB_CONTAINERS
340 result= search->second;
346 result->
id = nextSystemThreadId--;
349 #if ALIB_MONOMEM && ALIB_CONTAINERS
366{ target << src->GetName() <<
'(' << src->GetID() <<
')';} )
369{ target << src.GetName() <<
'(' << src.GetID() <<
')';} )
372{ target << src->GetName() <<
'(' << src->GetID() <<
')';} )
375{ target << src.GetName() <<
'(' << src.GetID() <<
')';} )
constexpr const TChar * Terminate() const
constexpr bool IsEmpty() const
virtual ALIB_API void Start()
ThreadID id
The id of the thread.
virtual void SetName(const String &newName)
virtual ALIB_API ~Thread() override
@ Running
The thread's Run method is currently processed.
@ Started
Method Start was invoked but not running, yet.
@ Terminated
The thread is terminated.
AString name
The name of the thread.
State state
Internal flag to indicate if the thread is alive.
std::thread * c11Thread
The internal C++ thread object.
static ALIB_API Thread * GetMain()
ALIB_API Thread(const String &pName=EMPTY_STRING)
virtual void Run() override
static ALIB_API Thread * Get(std::thread::id nativeID)
virtual ALIB_API void Join()
std::thread::id nativeID
The internal C++ thread id.
#define ALIB_WARNING(...)
#define ALIB_ASSERT_RESULT_EQUALS( func, value)
#define ALIB_STRINGS_APPENDABLE_TYPE_DEF_W(TYPE, IMPL)
#define ALIB_STRINGS_TO_NARROW( src, dest, bufSize)
#define ALIB_BOXING_BOOTSTRAP_REGISTER_FAPPEND_FOR_APPENDABLE_TYPE(TAppendable)
#define ALIB_ASSERT_ERROR(cond,...)
#define ALIB_STRINGS_APPENDABLE_TYPE_DEF_N(TYPE, IMPL)
#define ALIB_ASSERT_WARNING(cond,...)
constexpr bool IsNull(const T &t)
ALIB_API MonoAllocator GLOBAL_ALLOCATOR
ALIB_API std::mutex MODULE_LOCK
void threadStart(Thread *thread)
ALIB_API HashMap< MonoAllocator, std::thread::id, Thread * > THREAD_MAP
Directory which assigns system thread IDs to ALib Thread objects.
integer ThreadID
The ALib thread identifier type.
LocalString< 64 > String64
Type alias name for TLocalString<character,64>.
NLocalString< 128 > NString128
Type alias name for TLocalString<nchar,128>.
threads::Thread Thread
Type alias in namespace alib.
NLocalString< 256 > NString256
Type alias name for TLocalString<nchar,256>.
constexpr CString NEW_LINE
A zero-terminated string containing the new-line character sequence.
NLocalString< 512 > NString512
Type alias name for TLocalString<nchar,512>.
lang::integer integer
Type alias in namespace alib.
containers::HashMap< TAllocator, TKey, TMapped, THash, TEqual, THashCaching, TRecycling > HashMap
Type alias in namespace alib.
static void Bootstrap(TEnum element, TArgs &&... args) noexcept