ld-tool portability fix: MacOS build experience
[lunaix-os.git] / lunaix-os / usr / libc / src / pthread.c
1 #include <lunaix/syscall.h>
2 #include <pthread.h>
3
4 static void*
5 __pthread_routine_wrapper(void *(*start_routine)(void*), void* arg)
6 {
7     void* ret = start_routine(arg);
8
9     do_lunaix_syscall(__SYSCALL_th_exit, ret);
10
11     return ret; // should not reach
12 }
13
14 int 
15 pthread_create(pthread_t* thread,
16                 const pthread_attr_t* attr,
17                 void *(*start_routine)(void*), void* arg)
18 {
19     // FIXME attr currently not used
20
21     struct uthread_info th_info;
22     int ret = do_lunaix_syscall(__SYSCALL_th_create, thread, &th_info, __pthread_routine_wrapper, NULL);
23
24     if (ret) {
25         return ret;
26     }
27
28     // FIXME we should encapsulate these parameter into struct
29     //       and pass it as a single thread param.
30
31     void** th_stack = (void**) th_info.th_stack_top; 
32     th_stack[1] = (void*)start_routine;
33     th_stack[2] = arg;
34
35     return ret;
36 }
37
38 int 
39 pthread_detach(pthread_t thread)
40 {
41     return do_lunaix_syscall(__SYSCALL_th_detach, thread);
42 }
43
44 void 
45 pthread_exit(void *value_ptr)
46 {
47     do_lunaix_syscall(__SYSCALL_th_exit, value_ptr);
48 }
49
50 int 
51 pthread_join(pthread_t thread, void **value_ptr)
52 {
53     return do_lunaix_syscall(__SYSCALL_th_join, thread, value_ptr);
54 }
55
56 int 
57 pthread_kill(pthread_t thread, int sig)
58 {
59     return do_lunaix_syscall(__SYSCALL_th_kill, thread, sig);
60 }
61
62 pthread_t 
63 pthread_self(void)
64 {
65     return do_lunaix_syscall(__SYSCALL_th_self);
66 }