diff --git a/modules/exceptions/src/exception.cppm b/modules/exceptions/src/exception.cppm index d81c350..38fb6b2 100644 --- a/modules/exceptions/src/exception.cppm +++ b/modules/exceptions/src/exception.cppm @@ -29,6 +29,13 @@ namespace LdH { std::cerr << this->formatOutput(); } }; + + export + [[noreturn]] + void abort(char const *msg) { + std::cerr << "Fatal error:\n" << (Exception{msg}).formatOutput() << std::endl; + std::abort(); + } } module : private; @@ -48,7 +55,7 @@ std::string LdH::Exception::formatOutput() const { std::stringstream builder; builder << "Exception: " << this->message << '\n'; for (auto const &frame: this->stacktrace) { - builder << "\t" << frame.description(); + builder << "\t" << frame.description(); if (!frame.source_file().empty()) { builder << "\t\t" << frame.source_file() << ":" << frame.source_line() << '\n'; } diff --git a/modules/threads/src/highlevel_impl.cppm b/modules/threads/src/highlevel_impl.cppm index 40532cc..e94e7c1 100644 --- a/modules/threads/src/highlevel_impl.cppm +++ b/modules/threads/src/highlevel_impl.cppm @@ -166,7 +166,7 @@ LdH::CommonHighLevelThreadWrapper &LdH::CommonHighLevelThreadW case _thread_state_RUNNING: case _thread_state_JOINING: case _thread_state_JOINED: - throw LdH::Exception{"Variable already initialized"}; + LdH::abort("Variable already initialized"); } new(this)CommonHighLevelThreadWrapper{std::move(other)}; return *this; @@ -176,16 +176,16 @@ template requires LdH::NativeHighLevelThreadWrapper::join() { switch (this->_cas_state(_thread_state_RUNNING, _thread_state_JOINING)) { case _thread_state_UNINITIALIZED: - throw LdH::Exception{"Variable not initialized"}; + LdH::abort("Variable not initialized"); case _thread_state_RUNNING: break; case _thread_state_JOINING: case _thread_state_JOINED: - throw LdH::Exception{"Thread already joined"}; + LdH::abort("Thread already joined"); case _thread_state_DESTROYED: - throw LdH::Exception{"Thread already destroyed"}; + LdH::abort("Thread already destroyed"); case _thread_state_MOVED: - throw LdH::Exception{"Content of this variable was moved to another variable"}; + LdH::abort("Content of this variable was moved to another variable"); break; } @@ -197,16 +197,16 @@ template requires LdH::NativeHighLevelThreadWrapper::destroy() { switch (this->_cas_state(_thread_state_JOINED, _thread_state_DESTROYED)) { case _thread_state_UNINITIALIZED: - throw LdH::Exception{"Variable not initialized"}; + LdH::abort("Variable not initialized"); case _thread_state_RUNNING: case _thread_state_JOINING: - throw LdH::Exception{"Thread not finished yet"}; + LdH::abort("Thread not finished yet"); case _thread_state_JOINED: break; case _thread_state_DESTROYED: - throw LdH::Exception{"Thread already destroyed"}; + LdH::abort("Thread already destroyed"); case _thread_state_MOVED: - throw LdH::Exception{"Content of this variable was moved to another variable"}; + LdH::abort("Content of this variable was moved to another variable"); } this->_native.destroy(); } @@ -219,9 +219,9 @@ LdH::CommonHighLevelThreadWrapper::~CommonHighLevelThreadWrapp break; case _thread_state_RUNNING: case _thread_state_JOINING: - throw LdH::Exception{"Thread not finished yet"}; + LdH::abort("Thread not finished yet"); case _thread_state_JOINED: - throw LdH::Exception{"Thread not destroyed yet"}; + LdH::abort("Thread not destroyed yet"); case _thread_state_DESTROYED: break; case _thread_state_MOVED: diff --git a/modules/threads/src/platform/windows.cppm b/modules/threads/src/platform/windows.cppm index b683306..442d222 100644 --- a/modules/threads/src/platform/windows.cppm +++ b/modules/threads/src/platform/windows.cppm @@ -32,11 +32,11 @@ namespace LdH { case WAIT_TIMEOUT: continue; case WAIT_ABANDONED: - throw LdH::Exception{"Unexpected return WAIT_ABANDONED of WaitForSingleObject"}; + LdH::abort("Unexpected return WAIT_ABANDONED of WaitForSingleObject"); case WAIT_OBJECT_0: return; default: - throw LdH::Exception{"Unexpected return of WaitForSingleObject"}; + LdH::abort("Unexpected return of WaitForSingleObject"); } } } @@ -79,9 +79,11 @@ namespace LdH { static DWORD kernel_ret() { return 0; } + private: explicit NativeThreadWrapperForWindows(HANDLE hThread) : _hThread{hThread} { } + public: static NativeThreadWrapperForWindows start(std::string &, DWORD (*kernel)(void *), void *kernel_arg) { HANDLE hThread = CreateThread(