blob: 8230c1ca8e8246164374121a4a7924c33a333bb2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
#include "Thread.h"
#include <cassert>
#include <exception>
#include <pthread.h>
#include <utility>
namespace cru {
Thread::Thread(Thread &&other) noexcept
: detached_(other.detached_), joined_(other.joined_),
#ifdef CRU_WINDOWS
thread_handle_(other.thread_handle_)
#else
thread_(std::move(other.thread_))
#endif
{
other.joined_ = false;
#ifdef CRU_WINDOWS
other.thread_handle_ = nullptr;
#endif
} // namespace cru
Thread &Thread::operator=(Thread &&other) noexcept {
if (this != &other) {
detached_ = other.detached_;
joined_ = other.joined_;
#ifdef CRU_WINDOWS
thread_handle_ = other.thread_handle_;
other.thread_handle_ = nullptr;
#else
thread_ = std::move(other.thread_);
#endif
other.detached_ = false;
other.joined_ = false;
}
return *this;
}
Thread::~Thread() { Destroy(); }
void Thread::Join() {
joined_ = true;
#ifdef CRU_WINDOWS
assert(thread_handle_);
WaitForSingleObject(thread_handle_, INFINITE);
#else
assert(thread_);
auto c = pthread_join(*thread_, nullptr);
assert(c == 0);
#endif
}
void Thread::Detach() {
#ifdef CRU_WINDOWS
assert(thread_handle_);
#else
assert(thread_);
#endif
detached_ = true;
}
void Thread::swap(Thread &other) noexcept {
#ifdef CRU_WINDOWS
Thread temp = std::move(*this);
*this = std::move(other);
other = std::move(temp);
#else
#endif
}
void Thread::Destroy() noexcept {
if (!detached_ && !joined_ &&
#ifdef CRU_WINDOWS
thread_handle_ != nullptr
#else
thread_ != nullptr
#endif
) {
std::terminate();
} else {
detached_ = false;
joined_ = false;
#ifdef CRU_WINDOWS
thread_handle_ = nullptr;
#else
thread_ = nullptr;
#endif
}
}
namespace details {
#ifdef CRU_WINDOWS
DWORD WINAPI ThreadProc(_In_ LPVOID lpParameter) {
auto p = static_cast<std::function<void()> *>(lpParameter);
(*p)();
delete p;
return 0;
}
#else
void *ThreadProc(void *data) {
auto p = static_cast<std::function<void()> *>(data);
(*p)();
delete p;
return nullptr;
}
#endif
} // namespace details
} // namespace cru
|