-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux_thread.cpp
64 lines (50 loc) · 1.32 KB
/
linux_thread.cpp
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
#include "thread.h"
#include "core.h"
#include <pthread.h>
#include <errno.h>
struct Thread {
pthread_t handle;
ThreadProc user_proc;
void *user_data;
};
Mutex* create_mutex()
{
extern Allocator mem_sys;
pthread_mutex_t *mutex = ALLOC_T(mem_sys, pthread_mutex_t);
int r = pthread_mutex_init(mutex, nullptr);
PANIC_IF(r != 0, "failed to create mutex, errno: %d", errno);
return (Mutex*)mutex;
}
void lock_mutex(Mutex *m)
{
pthread_mutex_t *mutex = (pthread_mutex_t*)m;
int r = pthread_mutex_lock(mutex);
PANIC_IF(r != 0, "failed to lock mutex, errno: %d", errno);
}
void unlock_mutex(Mutex *m)
{
pthread_mutex_t *mutex = (pthread_mutex_t*)m;
int r = pthread_mutex_unlock(mutex);
PANIC_IF(r != 0, "failed to lock mutex, errno: %d", errno);
}
Thread* create_thread(ThreadProc proc, void *user_data)
{
extern Allocator mem_sys;
i32 result;
pthread_attr_t attr;
result = pthread_attr_init(&attr);
PANIC_IF(result != 0, "failed initialising thread attributes");
Thread *thread = ALLOC_T(mem_sys, Thread) {
.user_proc = proc,
.user_data = user_data,
};
result = pthread_create(&thread->handle, &attr,
[](void *data) -> void*
{
Thread *thread = (Thread*)data;
thread->user_proc(thread->user_data);
return nullptr;
}, thread);
PANIC_IF(result != 0, "failed creating thread");
return thread;
}