diff --git a/modules/threads/CMakeLists.txt b/modules/threads/CMakeLists.txt index 0b896f8..932b808 100644 --- a/modules/threads/CMakeLists.txt +++ b/modules/threads/CMakeLists.txt @@ -8,7 +8,7 @@ target_sources( src/common.cppm src/thread_routine.cppm src/highlevel_impl.cppm - src/windows.cppm + src/platform/windows.cppm ) target_link_libraries(threads PRIVATE exceptions Synchronization) \ No newline at end of file diff --git a/modules/threads/src/common.cppm b/modules/threads/src/common.cppm index fa7e9ff..2d7d1cd 100644 --- a/modules/threads/src/common.cppm +++ b/modules/threads/src/common.cppm @@ -1,48 +1,21 @@ export module ru.landgrafhomyak.BGTU.networks_1.threads; import std; export import :thread_routine; -import :impl; - - -namespace LdH { - export class ThreadController { - private: - LdH::_NativeThreads::ThreadController _value; - - public: - ThreadController() = default; - - ThreadController(LdH::ThreadController &) = delete; - - ThreadController(LdH::ThreadController const &) = delete; - - ThreadController(LdH::ThreadController &&other) noexcept : _value{std::move(other._value)} { - } - - void operator=(LdH::ThreadController &&other) noexcept { - this->_value = std::move(other._value); - } - - void join() { - this->_value.join(); - } - - void destroy() { - this->_value.destroy(); - } - - private: - ThreadController(LdH::_NativeThreads::ThreadController &&value) : _value{std::move(value)} { - }; - - friend - LdH::ThreadController fork(std::string &&name, LdH::ThreadRoutine auto &&routine); - }; - - - export LdH::ThreadController fork(std::string &&name, ThreadRoutine auto &&routine) { - return LdH::ThreadController{LdH::_NativeThreads::fork(std::move(name), std::move(routine))}; - } -} +export import :impl.windows; module:private; + +namespace LdH { + template + concept _ThreadControllerChecks = std::movable && + !std::copyable && + std::destructible && + std::is_move_assignable_v && + requires(thread_controller_t self) + { + { self.join() } -> std::same_as; + { self.destroy() } -> std::same_as; + }; + + static_assert(_ThreadControllerChecks); +} diff --git a/modules/threads/src/highlevel_impl.cppm b/modules/threads/src/highlevel_impl.cppm index a2e3131..40532cc 100644 --- a/modules/threads/src/highlevel_impl.cppm +++ b/modules/threads/src/highlevel_impl.cppm @@ -13,7 +13,7 @@ namespace LdH { concept NativeHighLevelThreadWrapper = std::movable && std::destructible && std::default_initializable && - requires(native_wrapper_t &&self) + requires(native_wrapper_t self) { { self.join() } -> std::same_as; { self.destroy() } -> std::same_as; @@ -81,13 +81,13 @@ namespace LdH { CommonHighLevelThreadWrapper(CommonHighLevelThreadWrapper &&other) noexcept; - void operator=(CommonHighLevelThreadWrapper &&other) noexcept; + CommonHighLevelThreadWrapper &operator=(CommonHighLevelThreadWrapper &&other) noexcept; void join(); void destroy(); - ~CommonHighLevelThreadWrapper() noexcept(false); + ~CommonHighLevelThreadWrapper() noexcept; private: template @@ -157,7 +157,7 @@ LdH::CommonHighLevelThreadWrapper::CommonHighLevelThreadWrappe template requires LdH::NativeHighLevelThreadWrapper -void LdH::CommonHighLevelThreadWrapper::operator=(CommonHighLevelThreadWrapper &&other) noexcept { +LdH::CommonHighLevelThreadWrapper &LdH::CommonHighLevelThreadWrapper::operator=(CommonHighLevelThreadWrapper &&other) noexcept { switch (this->_state.load()) { case _thread_state_UNINITIALIZED: case _thread_state_MOVED: @@ -169,6 +169,7 @@ void LdH::CommonHighLevelThreadWrapper::operator=(CommonHighLe throw LdH::Exception{"Variable already initialized"}; } new(this)CommonHighLevelThreadWrapper{std::move(other)}; + return *this; } template requires LdH::NativeHighLevelThreadWrapper @@ -211,7 +212,8 @@ void LdH::CommonHighLevelThreadWrapper::destroy() { } template requires LdH::NativeHighLevelThreadWrapper -LdH::CommonHighLevelThreadWrapper::~CommonHighLevelThreadWrapper() noexcept(false) { +LdH::CommonHighLevelThreadWrapper::~CommonHighLevelThreadWrapper() noexcept { + // todo fatal error switch (this->_state.load()) { case _thread_state_UNINITIALIZED: break; diff --git a/modules/threads/src/windows.cppm b/modules/threads/src/platform/windows.cppm similarity index 95% rename from modules/threads/src/windows.cppm rename to modules/threads/src/platform/windows.cppm index 3a1d3d5..b683306 100644 --- a/modules/threads/src/windows.cppm +++ b/modules/threads/src/platform/windows.cppm @@ -4,16 +4,18 @@ module; #include -export module ru.landgrafhomyak.BGTU.networks_1.threads:impl; +#endif + +export module ru.landgrafhomyak.BGTU.networks_1.threads:impl.windows; + +#ifdef _WIN32 import std; - - import ru.landgrafhomyak.BGTU.networks_1.exceptions; import ru.landgrafhomyak.BGTU.networks_1.exceptions.windows; import :thread_routine; import :highlevel_impl; -namespace LdH::_NativeThreads { +namespace LdH { class NativeThreadWrapperForWindows { private: HANDLE _hThread;