Repository navigation
Expand file tree
/
Copy paththread.cpp
More file actions
75 lines (64 loc) · 1.45 KB
/
Copy paththread.cpp
File metadata and controls
75 lines (64 loc) · 1.45 KB
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
#ifdef CBS_WIN32
DWORD __stdcall Thread::bootstrap(LPVOID param) {
Thread::Arg* thread_arg = (Thread::Arg*)param;
thread_arg->func(thread_arg->arg);
mem_free(thread_arg);
return 0;
}
void Thread::join(this Thread& self) {
if (self.exists == false) {
return;
}
if (::WaitForSingleObject(self.handle, INFINITE) != WAIT_OBJECT_0) {
ctk::panic("::WaitForSingleObject failed");
}
::CloseHandle(self.handle);
self.exists = false;
}
bool Thread::try_join(this Thread& self) {
if (self.exists == false) {
return true;
}
DWORD wait_value = ::WaitForSingleObject(self.handle, 0);
if (wait_value == WAIT_FAILED) {
ctk::panic("::WaitForSingleObject failed");
}
if (wait_value == WAIT_OBJECT_0) {
self.exists = false;
return true;
}
return false;
}
void Thread::detach(this Thread& self) {
::CloseHandle(self.handle);
}
#endif
#ifdef CBS_LINUX
void* Thread::bootstrap(void* param) {
Thread::Arg* thread_arg = (Thread::Arg*)param;
thread_arg->func(thread_arg->arg);
mem_free(thread_arg);
return nullptr;
}
void Thread::join(this Thread& self) {
if (self.exists == false) {
return;
}
::pthread_join(self.id, nullptr);
self.exists = false;
}
bool Thread::try_join(this Thread& self) {
if (self.exists == false) {
return true;
}
int join_value = ::pthread_tryjoin_np(self.id, nullptr);
if (join_value == 0) {
self.exists = false;
return true;
}
return false;
}
void Thread::detach(this Thread& self) {
::pthread_detach(self.id);
}
#endif