Redesign of threads module hierarchy: concept static_assert instead common wrapper

This commit is contained in:
Andrew Golovashevich 2025-10-27 09:25:19 +03:00
parent 600806d911
commit 78702435a7
4 changed files with 30 additions and 53 deletions

View File

@ -8,7 +8,7 @@ target_sources(
src/common.cppm src/common.cppm
src/thread_routine.cppm src/thread_routine.cppm
src/highlevel_impl.cppm src/highlevel_impl.cppm
src/windows.cppm src/platform/windows.cppm
) )
target_link_libraries(threads PRIVATE exceptions Synchronization) target_link_libraries(threads PRIVATE exceptions Synchronization)

View File

@ -1,48 +1,21 @@
export module ru.landgrafhomyak.BGTU.networks_1.threads; export module ru.landgrafhomyak.BGTU.networks_1.threads;
import std; import std;
export import :thread_routine; export import :thread_routine;
import :impl; export import :impl.windows;
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))};
}
}
module:private; module:private;
namespace LdH {
template<class thread_controller_t>
concept _ThreadControllerChecks = std::movable<thread_controller_t> &&
!std::copyable<thread_controller_t> &&
std::destructible<thread_controller_t> &&
std::is_move_assignable_v<thread_controller_t> &&
requires(thread_controller_t self)
{
{ self.join() } -> std::same_as<void>;
{ self.destroy() } -> std::same_as<void>;
};
static_assert(_ThreadControllerChecks<ThreadController>);
}

View File

@ -13,7 +13,7 @@ namespace LdH {
concept NativeHighLevelThreadWrapper = std::movable<native_wrapper_t> && concept NativeHighLevelThreadWrapper = std::movable<native_wrapper_t> &&
std::destructible<native_wrapper_t> && std::destructible<native_wrapper_t> &&
std::default_initializable<native_wrapper_t> && std::default_initializable<native_wrapper_t> &&
requires(native_wrapper_t &&self) requires(native_wrapper_t self)
{ {
{ self.join() } -> std::same_as<void>; { self.join() } -> std::same_as<void>;
{ self.destroy() } -> std::same_as<void>; { self.destroy() } -> std::same_as<void>;
@ -81,13 +81,13 @@ namespace LdH {
CommonHighLevelThreadWrapper(CommonHighLevelThreadWrapper &&other) noexcept; CommonHighLevelThreadWrapper(CommonHighLevelThreadWrapper &&other) noexcept;
void operator=(CommonHighLevelThreadWrapper &&other) noexcept; CommonHighLevelThreadWrapper &operator=(CommonHighLevelThreadWrapper &&other) noexcept;
void join(); void join();
void destroy(); void destroy();
~CommonHighLevelThreadWrapper() noexcept(false); ~CommonHighLevelThreadWrapper() noexcept;
private: private:
template<ThreadRoutine thread_routine_t> template<ThreadRoutine thread_routine_t>
@ -157,7 +157,7 @@ LdH::CommonHighLevelThreadWrapper<native_wrapper_t>::CommonHighLevelThreadWrappe
template<class native_wrapper_t> requires LdH::NativeHighLevelThreadWrapper<native_wrapper_t> template<class native_wrapper_t> requires LdH::NativeHighLevelThreadWrapper<native_wrapper_t>
void LdH::CommonHighLevelThreadWrapper<native_wrapper_t>::operator=(CommonHighLevelThreadWrapper &&other) noexcept { LdH::CommonHighLevelThreadWrapper<native_wrapper_t> &LdH::CommonHighLevelThreadWrapper<native_wrapper_t>::operator=(CommonHighLevelThreadWrapper &&other) noexcept {
switch (this->_state.load()) { switch (this->_state.load()) {
case _thread_state_UNINITIALIZED: case _thread_state_UNINITIALIZED:
case _thread_state_MOVED: case _thread_state_MOVED:
@ -169,6 +169,7 @@ void LdH::CommonHighLevelThreadWrapper<native_wrapper_t>::operator=(CommonHighLe
throw LdH::Exception{"Variable already initialized"}; throw LdH::Exception{"Variable already initialized"};
} }
new(this)CommonHighLevelThreadWrapper{std::move(other)}; new(this)CommonHighLevelThreadWrapper{std::move(other)};
return *this;
} }
template<class native_wrapper_t> requires LdH::NativeHighLevelThreadWrapper<native_wrapper_t> template<class native_wrapper_t> requires LdH::NativeHighLevelThreadWrapper<native_wrapper_t>
@ -211,7 +212,8 @@ void LdH::CommonHighLevelThreadWrapper<native_wrapper_t>::destroy() {
} }
template<class native_wrapper_t> requires LdH::NativeHighLevelThreadWrapper<native_wrapper_t> template<class native_wrapper_t> requires LdH::NativeHighLevelThreadWrapper<native_wrapper_t>
LdH::CommonHighLevelThreadWrapper<native_wrapper_t>::~CommonHighLevelThreadWrapper() noexcept(false) { LdH::CommonHighLevelThreadWrapper<native_wrapper_t>::~CommonHighLevelThreadWrapper() noexcept {
// todo fatal error
switch (this->_state.load()) { switch (this->_state.load()) {
case _thread_state_UNINITIALIZED: case _thread_state_UNINITIALIZED:
break; break;

View File

@ -4,16 +4,18 @@ module;
#include <Windows.h> #include <Windows.h>
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 std;
import ru.landgrafhomyak.BGTU.networks_1.exceptions; import ru.landgrafhomyak.BGTU.networks_1.exceptions;
import ru.landgrafhomyak.BGTU.networks_1.exceptions.windows; import ru.landgrafhomyak.BGTU.networks_1.exceptions.windows;
import :thread_routine; import :thread_routine;
import :highlevel_impl; import :highlevel_impl;
namespace LdH::_NativeThreads { namespace LdH {
class NativeThreadWrapperForWindows { class NativeThreadWrapperForWindows {
private: private:
HANDLE _hThread; HANDLE _hThread;