1 #define _GNU_SOURCE
2 #include "pthread_impl.h"
3 #include <sys/mman.h>
4 #ifndef __LITEOS__
5 #include "param_check.h"
6 #include "musl_log.h"
7 #endif
8
dummy1(pthread_t t)9 static void dummy1(pthread_t t)
10 {
11 }
12 weak_alias(dummy1, __tl_sync);
13
__pthread_timedjoin_np(pthread_t t,void ** res,const struct timespec * at)14 static int __pthread_timedjoin_np(pthread_t t, void **res, const struct timespec *at)
15 {
16 int state, cs, r = 0;
17 #ifdef __LITEOS_A__
18 unsigned int tid;
19 pthread_t self = __pthread_self();
20 #endif
21 __pthread_testcancel();
22 __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
23 if (cs == PTHREAD_CANCEL_ENABLE) __pthread_setcancelstate(cs, 0);
24 #ifdef __LITEOS_A__
25 if (t == self) {
26 r = EINVAL;
27 goto out;
28 }
29 switch (t->detach_state) {
30 case DT_JOINABLE: {
31 r = __syscall(SYS_pthread_join, t->tid);
32 break;
33 }
34 case DT_EXITING:
35 break;
36 case DT_DETACHED:
37 default:
38 r = EINVAL;
39 break;
40 }
41 out:
42 __pthread_setcancelstate(cs, 0);
43 if (r == ESRCH || r == EINVAL) return r;
44 __tl_sync(t);
45 if (res) *res = t->result;
46 tid = t->tid;
47 t->tid = 0;
48 return __syscall(SYS_pthread_deatch, tid);
49 #else
50 while ((state = t->detach_state) && r != ETIMEDOUT && r != EINVAL) {
51 if (state >= DT_DETACHED) a_crash();
52 r = __timedwait_cp(&t->detach_state, state, CLOCK_REALTIME, at, 1);
53 }
54 __pthread_setcancelstate(cs, 0);
55 if (r == ETIMEDOUT || r == EINVAL) return r;
56 __tl_sync(t);
57 if (res) *res = t->result;
58 if (t->map_base) __munmap(t->map_base, t->map_size);
59 return 0;
60 #endif
61 }
62
__pthread_join(pthread_t t,void ** res)63 int __pthread_join(pthread_t t, void **res)
64 {
65 #ifndef __LITEOS__
66 if (t == NULL) {
67 MUSL_LOGE("musl libc: invalid pthread_t (0) passed to pthread_join");
68 return ESRCH;
69 }
70 #endif
71 return __pthread_timedjoin_np(t, res, 0);
72 }
73
__pthread_tryjoin_np(pthread_t t,void ** res)74 static int __pthread_tryjoin_np(pthread_t t, void **res)
75 {
76 return t->detach_state==DT_JOINABLE ? EBUSY : __pthread_join(t, res);
77 }
78
79 weak_alias(__pthread_tryjoin_np, pthread_tryjoin_np);
80 weak_alias(__pthread_timedjoin_np, pthread_timedjoin_np);
81 weak_alias(__pthread_join, pthread_join);
82