mirror of https://git.musl-libc.org/git/musl
Browse Source
based on patch by Jens Gustedt. the main difficulty here is handling the difference between start function signatures and thread return types for C11 threads versus POSIX threads. pointers to void are assumed to be able to represent faithfully all values of int. the function pointer for the thread start function is cast to an incorrect type for passing through pthread_create, but is cast back to its correct type before calling so that the behavior of the call is well-defined. changes to the existing threads implementation were kept minimal to reduce the risk of regressions, and duplication of code that carries implementation-specific assumptions was avoided for ease and safety of future maintenance.master
10 changed files with 84 additions and 7 deletions
@ -1,6 +1,11 @@ |
|||||
#include <pthread.h> |
#include <pthread.h> |
||||
|
#include <threads.h> |
||||
|
#include "libc.h" |
||||
|
|
||||
int (pthread_equal)(pthread_t a, pthread_t b) |
static int __pthread_equal(pthread_t a, pthread_t b) |
||||
{ |
{ |
||||
return a==b; |
return a==b; |
||||
} |
} |
||||
|
|
||||
|
weak_alias(__pthread_equal, pthread_equal); |
||||
|
weak_alias(__pthread_equal, thrd_equal); |
||||
|
|||||
@ -1,6 +1,11 @@ |
|||||
#include "pthread_impl.h" |
#include "pthread_impl.h" |
||||
|
#include <threads.h> |
||||
|
#include "libc.h" |
||||
|
|
||||
pthread_t pthread_self() |
static pthread_t __pthread_self_internal() |
||||
{ |
{ |
||||
return __pthread_self(); |
return __pthread_self(); |
||||
} |
} |
||||
|
|
||||
|
weak_alias(__pthread_self_internal, pthread_self); |
||||
|
weak_alias(__pthread_self_internal, thrd_current); |
||||
|
|||||
@ -0,0 +1,14 @@ |
|||||
|
#include "pthread_impl.h" |
||||
|
#include <threads.h> |
||||
|
|
||||
|
int __pthread_create(pthread_t *restrict, const pthread_attr_t *restrict, void *(*)(void *), void *restrict); |
||||
|
|
||||
|
int thrd_create(thrd_t *thr, thrd_start_t func, void *arg) |
||||
|
{ |
||||
|
int ret = __pthread_create(thr, __ATTRP_C11_THREAD, (void *(*)(void *))func, arg); |
||||
|
switch (ret) { |
||||
|
case 0: return thrd_success; |
||||
|
case EAGAIN: return thrd_nomem; |
||||
|
default: return thrd_error; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
#include "pthread_impl.h" |
||||
|
#include <threads.h> |
||||
|
|
||||
|
_Noreturn void __pthread_exit(void *); |
||||
|
|
||||
|
_Noreturn void thrd_exit(int result) |
||||
|
{ |
||||
|
__pthread_exit((void*)(intptr_t)result); |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
#include <stdint.h> |
||||
|
#include <threads.h> |
||||
|
|
||||
|
int __pthread_join(thrd_t, void**); |
||||
|
|
||||
|
int thrd_join(thrd_t t, int *res) |
||||
|
{ |
||||
|
void *pthread_res; |
||||
|
__pthread_join(t, &pthread_res); |
||||
|
if (res) *res = (int)(intptr_t)pthread_res; |
||||
|
return thrd_success; |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
#include <threads.h> |
||||
|
#include <errno.h> |
||||
|
#include "syscall.h" |
||||
|
|
||||
|
int thrd_sleep(const struct timespec *req, struct timespec *rem) |
||||
|
{ |
||||
|
int ret = __syscall(SYS_nanosleep, req, rem); |
||||
|
switch (ret) { |
||||
|
case 0: return 0; |
||||
|
case -EINTR: return -1; /* value specified by C11 */ |
||||
|
default: return -2; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
#include <threads.h> |
||||
|
#include "syscall.h" |
||||
|
|
||||
|
void thrd_yield() |
||||
|
{ |
||||
|
__syscall(SYS_sched_yield); |
||||
|
} |
||||
Loading…
Reference in new issue