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
|
internal void *linux_thread_entry(void *raw)
{
// NOTE: the ThreadStart lives in the launching arena, copy it out but never free() it
ThreadStart start = *(ThreadStart *)raw;
tls_scratch_arena = ArenaCreate(MiB(64));
start.proc(start.param);
ArenaDestroy(tls_scratch_arena);
return 0;
}
internal Thread thread_launch(Arena *arena, ThreadProc *proc, void *param)
{
Thread thread = {};
ThreadStart *start = PushStruct(arena, ThreadStart);
start->proc = proc;
start->param = param;
pthread_t handle;
int error = pthread_create(&handle, 0, linux_thread_entry, start);
AssertLog(!error, "failed to launch thread");
thread.data[0] = (u64)handle;
return thread;
}
internal b32 thread_join(Thread thread)
{
return pthread_join((pthread_t)thread.data[0], 0) == 0;
}
internal void thread_detach(Thread thread)
{
pthread_detach((pthread_t)thread.data[0]);
}
internal void thread_set_name(String8 name)
{
char buffer[16] = {0};
MemCpy(buffer, name.data, Min(name.size, ArrayCount(buffer) - 1));
#if PLATFORM_APPLE
pthread_setname_np(buffer); // macos: only names the calling thread
#else
pthread_setname_np(pthread_self(), buffer);
#endif
}
internal u64 thread_current_id(void)
{
#if PLATFORM_APPLE
u64 id = 0;
pthread_threadid_np(NULL, &id);
return id;
#else
return (u64)gettid();
#endif
}
|