10#if !defined(HPP_ALIB_THREADS_DETAIL_THREADMAP)
14#if !defined (HPP_ALIB_STRINGS_LOCALSTRING)
18#if !defined (_GLIBCXX_CONDITION_VARIABLE) && !defined(_CONDITION_VARIABLE_)
19# include <condition_variable>
22#if !defined (_GLIBCXX_ATOMIC) && !defined(_ATOMIC_)
28# if !defined(HPP_ALIB_ENUMS_RECORDBOOTSTRAP)
31# if !defined(HPP_ALIB_ENUMS_SERIALIZATION)
64 std::atomic<ThreadID> nextThreadIdx(1);
65 Thread* mainThread =
nullptr;
81 #define LOCK_THREADS ALIB_OWN( monomem::GlobalAllocatorLock )
84 std::unordered_map<std::thread::id, Thread *>
threadMap;
86 #define LOCK_THREADS std::unique_lock<std::mutex> uniqueLock( moduleLock );
98 ALIB_ASSERT_ERROR( mainThread,
"THREADS",
"ALib Module Threads not initialized." )
112 result= search->second;
118 result->
id = nextSystemThreadId--;
122 threadMap.EmplaceUnique( c11ID, result );
124 threadMap.insert( std::make_pair( c11ID, result) );
135using namespace detail;
147 mainThread->id=
static_cast<ThreadID>(-1);
148 mainThread->SetName(
A_CHAR(
"MAIN_THREAD"));
152 threadMap.EmplaceUnique( std::this_thread::get_id(), mainThread );
154 threadMap.insert( std::make_pair( std::this_thread::get_id(), mainThread) );
158 "Error initializing threads. Probably forbidden repeated initialization from different thread." )
161 #if ALIB_ENUMS && ALIB_BOXING && !ALIB_CAMP
182 if( mainThread ==
nullptr )
195 if( qtyThreads != 1 )
198 NString4K dbgThreadList(
"ALib Termination: More than one thread running: ");
199 dbgThreadList << static_cast<integer>(qtyThreads) <<
NewLine();
202 dbgThreadList << ++tNr <<
": " << it.second->GetName()
204 #if ALIB_ENUMS && ALIB_BOXING
207 int(it.second->state )
220 "threads::Shutdown: last thread is not the main system thread detected "
221 "in threads::Bootstrap" )
238 name <<
'(' <<
id <<
')';
246 msg <<
"Thread \"" <<
GetName()
247 <<
"\" was not terminated before destruction. Use Thread::Terminate() to "
248 "avoid this message.";
261 msg <<
"Terminating thread \"" <<
GetName() <<
"\" which is not in state 'Stopped'. State: '"
262 #if ALIB_ENUMS && ALIB_BOXING
265 << int(
state) <<
"'.";
276 msg <<
"Thread \"" <<
GetName() <<
"\" not joinable. State: '"
277 #if ALIB_ENUMS && ALIB_BOXING
280 << int(
state) <<
"'.";
305 "Double invocation of Thread::Terminate for thread {!Q}",
309 "Terminating thread {!Q} which is not started or not managed by ALIB",
const TChar * Terminate() const
constexpr bool IsEmpty() const
virtual ALIB_API void Start()
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.
virtual const CString GetName()
static ALIB_API Thread * GetMain()
virtual ALIB_API void Terminate()
ALIB_API Thread(const String &pName=EmptyString())
virtual void Run() override
#define ALIB_WARNING(...)
#define ALIB_ASSERT_RESULT_EQUALS( func, value)
#define ALIB_BOXING_BOOTSTRAP_REGISTER_FAPPEND_FOR_APPENDABLE_TYPE(TAppendable)
#define ALIB_ASSERT_ERROR(cond,...)
#define ALIB_ASSERT_WARNING(cond,...)
MonoAllocator GlobalAllocator(8 *1024)
ALIB_API HashMap< std::thread::id, Thread * > threadMap
ALIB_API std::mutex moduleLock
void threadStart(Thread *thread)
ALIB_API Thread * getThread(std::thread::id c11ID)
LocalString< 64 > String64
Type alias name for TLocalString<character,64> .
NLocalString< 128 > NString128
Type alias name for TLocalString<nchar,128> .
constexpr CString NewLine()
threads::Thread Thread
Type alias in namespace alib.
NLocalString< 256 > NString256
Type alias name for TLocalString<nchar,256> .
lang::integer integer
Type alias in namespace alib.
static void Bootstrap(TEnum element, TArgs &&... args) noexcept