YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
thread.cpp
Go to the documentation of this file.
2#include <yaclib/log.hpp>
3
4#include <cstdio>
5#include <system_error>
6#include <utility>
7
8namespace yaclib::detail::fiber {
9
10static unsigned int gHardwareConcurrency{0};
11
13
14void Thread::swap(Thread& t) noexcept {
15 std::swap(_impl, t._impl);
16}
17
21
23 if (_impl == nullptr) {
24 throw std::system_error{std::make_error_code(std::errc::no_such_process)};
25 }
26 if (!joinable()) {
27 throw std::system_error{std::make_error_code(std::errc::resource_deadlock_would_occur)};
28 }
29
30 // TODO(myannyax) allow joining from other threads
31 while (_impl->GetState() != Completed) {
34 }
35 AfterJoinOrDetach();
36}
37
39 if (_impl == nullptr) {
40 throw std::system_error{std::make_error_code(std::errc::no_such_process)};
41 }
42 if (!joinable()) {
43 throw std::system_error{std::make_error_code(std::errc::resource_deadlock_would_occur)};
44 }
45 AfterJoinOrDetach();
46}
47
49 return _impl != nullptr ? _impl->GetId() : FiberBase::Id{0};
50}
51
53 YACLIB_DEBUG(true, "native_hande is not supported for fibers");
54 return nullptr;
55}
56
58 return gHardwareConcurrency != 0 ? gHardwareConcurrency : std::thread::hardware_concurrency();
59}
60
62 if (_impl != nullptr) {
63 std::terminate();
64 }
65}
66
68 swap(t);
69 return *this;
70}
71
72Thread::Thread(Thread&& t) noexcept : _impl{std::exchange(t._impl, nullptr)} {
73}
74
75void Thread::AfterJoinOrDetach() {
76 YACLIB_ASSERT(_impl);
77 if (_impl->GetState() == Completed) {
78 delete _impl;
79 } else {
80 _impl->SetThreadDead();
81 }
82 _impl = nullptr;
83}
84
85void Thread::SetHardwareConcurrency(unsigned int hardware_concurrency) noexcept {
86 gHardwareConcurrency = hardware_concurrency;
87}
88
89} // namespace yaclib::detail::fiber
FiberState GetState() noexcept
void SetJoiningFiber(FiberBase *joining_fiber) noexcept
id get_id() const noexcept
Definition thread.cpp:48
native_handle_type native_handle() noexcept
Definition thread.cpp:52
static unsigned int hardware_concurrency() noexcept
Definition thread.cpp:57
bool joinable() const noexcept
Definition thread.cpp:18
Thread & operator=(const Thread &)=delete
static void SetHardwareConcurrency(unsigned int hardware_concurrency) noexcept
Definition thread.cpp:85
static detail::fiber::FiberBase::Id GetId()
Definition scheduler.cpp:52
static detail::fiber::FiberBase * Current() noexcept
Definition scheduler.cpp:48
#define YACLIB_DEBUG(cond, message)
Definition log.hpp:84
#define YACLIB_ASSERT(cond)
Definition log.hpp:85
static unsigned int gHardwareConcurrency
Definition thread.cpp:10
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25