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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
// +build linux, darwin, freebsd
// +private
package thread
import "core:runtime"
import "core:intrinsics"
import "core:sync"
import "core:sys/unix"
// NOTE(tetra): Aligned here because of core/unix/pthread_linux.odin/pthread_t.
// Also see core/sys/darwin/mach_darwin.odin/semaphore_t.
Thread_Os_Specific :: struct #align 16 {
unix_thread: unix.pthread_t, // NOTE: very large on Darwin, small on Linux.
// NOTE: pthread has a proc to query this, but it is marked
// as non-portable ("np") so we do this instead.
done: bool,
// since libpthread doesn't seem to have a way to create a thread
// in a suspended state, we have it wait on this gate, which we
// signal to start it.
// destroyed after thread is started.
start_gate: sync.Condition,
start_mutex: sync.Mutex,
// if true, the thread has been started and the start_gate has been destroyed.
started: bool,
// NOTE: with pthreads, it is undefined behavior for multiple threads
// to call join on the same thread at the same time.
// this value is atomically updated to detect this.
// See the comment in `join`.
already_joined: bool,
}
//
// Creates a thread which will run the given procedure.
// It then waits for `start` to be called.
//
_create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^Thread {
__linux_thread_entry_proc :: proc "c" (t: rawptr) -> rawptr {
context = runtime.default_context();
t := (^Thread)(t);
sync.condition_wait_for(&t.start_gate);
sync.condition_destroy(&t.start_gate);
sync.mutex_destroy(&t.start_mutex);
t.start_gate = {};
t.start_mutex = {};
c := context;
if ic, ok := t.init_context.?; ok {
c = ic;
}
context = c;
t.procedure(t);
if t.init_context == nil {
if context.temp_allocator.data == &runtime.global_default_temp_allocator_data {
runtime.default_temp_allocator_destroy(auto_cast context.temp_allocator.data);
}
}
intrinsics.atomic_store(&t.done, true);
return nil;
}
attrs: unix.pthread_attr_t;
if unix.pthread_attr_init(&attrs) != 0 {
return nil; // NOTE(tetra, 2019-11-01): POSIX OOM.
}
defer unix.pthread_attr_destroy(&attrs);
// NOTE(tetra, 2019-11-01): These only fail if their argument is invalid.
assert(unix.pthread_attr_setdetachstate(&attrs, unix.PTHREAD_CREATE_JOINABLE) == 0);
assert(unix.pthread_attr_setinheritsched(&attrs, unix.PTHREAD_EXPLICIT_SCHED) == 0);
thread := new(Thread);
if thread == nil {
return nil;
}
thread.creation_allocator = context.allocator;
// Set thread priority.
policy: i32;
res := unix.pthread_attr_getschedpolicy(&attrs, &policy);
assert(res == 0);
params: unix.sched_param;
res = unix.pthread_attr_getschedparam(&attrs, ¶ms);
assert(res == 0);
low := unix.sched_get_priority_min(policy);
high := unix.sched_get_priority_max(policy);
switch priority {
case .Normal: // Okay
case .Low: params.sched_priority = low + 1;
case .High: params.sched_priority = high;
}
res = unix.pthread_attr_setschedparam(&attrs, ¶ms);
assert(res == 0);
if unix.pthread_create(&thread.unix_thread, &attrs, __linux_thread_entry_proc, thread) != 0 {
free(thread, thread.creation_allocator);
return nil;
}
thread.procedure = procedure;
sync.mutex_init(&thread.start_mutex);
sync.condition_init(&thread.start_gate, &thread.start_mutex);
return thread;
}
_start :: proc(t: ^Thread) {
if intrinsics.atomic_xchg(&t.started, true) {
return;
}
sync.condition_signal(&t.start_gate);
}
_is_done :: proc(t: ^Thread) -> bool {
return intrinsics.atomic_load(&t.done);
}
_join :: proc(t: ^Thread) {
if unix.pthread_equal(unix.pthread_self(), t.unix_thread) {
return;
}
// if unix.pthread_self().x == t.unix_thread.x do return;
// NOTE(tetra): It's apparently UB for multiple threads to join the same thread
// at the same time.
// If someone else already did, spin until the thread dies.
// See note on `already_joined` field.
// TODO(tetra): I'm not sure if we should do this, or panic, since I'm not
// sure it makes sense to need to join from multiple threads?
if intrinsics.atomic_xchg(&t.already_joined, true) {
for {
if intrinsics.atomic_load(&t.done) {
return;
}
intrinsics.cpu_relax();
}
}
// NOTE(tetra): If we're already dead, don't bother calling to pthread_join as that
// will just return 3 (ESRCH).
// We do this instead because I don't know if there is a danger
// that you may join a different thread from the one you called join on,
// if the thread handle is reused.
if intrinsics.atomic_load(&t.done) {
return;
}
ret_val: rawptr;
_ = unix.pthread_join(t.unix_thread, &ret_val);
if !intrinsics.atomic_load(&t.done) {
panic("thread not done after join");
}
}
_join_multiple :: proc(threads: ..^Thread) {
for t in threads {
_join(t);
}
}
_destroy :: proc(t: ^Thread) {
_join(t);
sync.condition_destroy(&t.start_gate);
sync.mutex_destroy(&t.start_mutex);
t.unix_thread = {};
free(t, t.creation_allocator);
}
_terminate :: proc(t: ^Thread, exit_code: int) {
// TODO(bill)
}
_yield :: proc() {
unix.sched_yield();
}
|