1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 #include <threads.h>
32 
33 #include <errno.h>
34 #include <sched.h>
35 #include <stdlib.h>
36 
37 #if defined(__BIONIC_THREADS_INLINE)
38 
39 __BEGIN_DECLS
40 
__bionic_thrd_error(int __pthread_code)41 static __inline int __bionic_thrd_error(int __pthread_code) {
42   switch (__pthread_code) {
43     case 0: return 0;
44     case ENOMEM: return thrd_nomem;
45     case ETIMEDOUT: return thrd_timedout;
46     case EBUSY: return thrd_busy;
47     default: return thrd_error;
48   }
49 }
50 
call_once(once_flag * __flag,void (* __function)(void))51 __BIONIC_THREADS_INLINE void call_once(once_flag* __flag,
52                                        void (*__function)(void)) {
53   pthread_once(__flag, __function);
54 }
55 
56 
57 
cnd_broadcast(cnd_t * __cnd)58 __BIONIC_THREADS_INLINE int cnd_broadcast(cnd_t* __cnd) {
59   return __bionic_thrd_error(pthread_cond_broadcast(__cnd));
60 }
61 
cnd_destroy(cnd_t * __cnd)62 __BIONIC_THREADS_INLINE void cnd_destroy(cnd_t* __cnd) {
63   pthread_cond_destroy(__cnd);
64 }
65 
cnd_init(cnd_t * __cnd)66 __BIONIC_THREADS_INLINE int cnd_init(cnd_t* __cnd) {
67   return __bionic_thrd_error(pthread_cond_init(__cnd, NULL));
68 }
69 
cnd_signal(cnd_t * __cnd)70 __BIONIC_THREADS_INLINE int cnd_signal(cnd_t* __cnd) {
71   return __bionic_thrd_error(pthread_cond_signal(__cnd));
72 }
73 
cnd_timedwait(cnd_t * __cnd,mtx_t * __mtx,const struct timespec * __timeout)74 __BIONIC_THREADS_INLINE int cnd_timedwait(cnd_t* __cnd,
75                                           mtx_t* __mtx,
76                                           const struct timespec* __timeout) {
77   return __bionic_thrd_error(pthread_cond_timedwait(__cnd, __mtx, __timeout));
78 }
79 
cnd_wait(cnd_t * __cnd,mtx_t * __mtx)80 __BIONIC_THREADS_INLINE int cnd_wait(cnd_t* __cnd, mtx_t* __mtx) {
81   return __bionic_thrd_error(pthread_cond_wait(__cnd, __mtx));
82 }
83 
84 
85 
mtx_destroy(mtx_t * __mtx)86 __BIONIC_THREADS_INLINE void mtx_destroy(mtx_t* __mtx) {
87   pthread_mutex_destroy(__mtx);
88 }
89 
mtx_init(mtx_t * __mtx,int __type)90 __BIONIC_THREADS_INLINE int mtx_init(mtx_t* __mtx, int __type) {
91   int __pthread_type = (__type & mtx_recursive) ? PTHREAD_MUTEX_RECURSIVE
92                                                 : PTHREAD_MUTEX_NORMAL;
93   __type &= ~mtx_recursive;
94   if (__type != mtx_plain && __type != mtx_timed) return thrd_error;
95 
96   pthread_mutexattr_t __attr;
97   pthread_mutexattr_init(&__attr);
98   pthread_mutexattr_settype(&__attr, __pthread_type);
99   return __bionic_thrd_error(pthread_mutex_init(__mtx, &__attr));
100 }
101 
mtx_lock(mtx_t * __mtx)102 __BIONIC_THREADS_INLINE int mtx_lock(mtx_t* __mtx) {
103   return __bionic_thrd_error(pthread_mutex_lock(__mtx));
104 }
105 
106 #if __ANDROID_API__ >= 21
mtx_timedlock(mtx_t * __mtx,const struct timespec * __timeout)107 __BIONIC_THREADS_INLINE int mtx_timedlock(mtx_t* __mtx,
108                                           const struct timespec* __timeout) {
109   return __bionic_thrd_error(pthread_mutex_timedlock(__mtx, __timeout));
110 }
111 #endif
112 
mtx_trylock(mtx_t * __mtx)113 __BIONIC_THREADS_INLINE int mtx_trylock(mtx_t* __mtx) {
114   return __bionic_thrd_error(pthread_mutex_trylock(__mtx));
115 }
116 
mtx_unlock(mtx_t * __mtx)117 __BIONIC_THREADS_INLINE int mtx_unlock(mtx_t* __mtx) {
118   return __bionic_thrd_error(pthread_mutex_unlock(__mtx));
119 }
120 
121 
122 
123 struct __bionic_thrd_data {
124   thrd_start_t __func;
125   void* __arg;
126 };
127 
__bionic_thrd_trampoline(void * __arg)128 static inline void* __bionic_thrd_trampoline(void* __arg) {
129   struct __bionic_thrd_data __data =
130       *__BIONIC_CAST(static_cast, struct __bionic_thrd_data*, __arg);
131   free(__arg);
132   int __result = __data.__func(__data.__arg);
133   return __BIONIC_CAST(reinterpret_cast, void*,
134                        __BIONIC_CAST(static_cast, uintptr_t, __result));
135 }
136 
thrd_create(thrd_t * __thrd,thrd_start_t __func,void * __arg)137 __BIONIC_THREADS_INLINE int thrd_create(thrd_t* __thrd,
138                                         thrd_start_t __func,
139                                         void* __arg) {
140   struct __bionic_thrd_data* __pthread_arg =
141       __BIONIC_CAST(static_cast, struct __bionic_thrd_data*,
142                     malloc(sizeof(struct __bionic_thrd_data)));
143   __pthread_arg->__func = __func;
144   __pthread_arg->__arg = __arg;
145   int __result = __bionic_thrd_error(pthread_create(__thrd, NULL,
146                                                     __bionic_thrd_trampoline,
147                                                     __pthread_arg));
148   if (__result != thrd_success) free(__pthread_arg);
149   return __result;
150 }
151 
thrd_current(void)152 __BIONIC_THREADS_INLINE thrd_t thrd_current(void) {
153   return pthread_self();
154 }
155 
thrd_detach(thrd_t __thrd)156 __BIONIC_THREADS_INLINE int thrd_detach(thrd_t __thrd) {
157   return __bionic_thrd_error(pthread_detach(__thrd));
158 }
159 
thrd_equal(thrd_t __lhs,thrd_t __rhs)160 __BIONIC_THREADS_INLINE int thrd_equal(thrd_t __lhs, thrd_t __rhs) {
161   return pthread_equal(__lhs, __rhs);
162 }
163 
thrd_exit(int __result)164 __BIONIC_THREADS_INLINE void thrd_exit(int __result) {
165   pthread_exit(__BIONIC_CAST(reinterpret_cast, void*,
166                              __BIONIC_CAST(static_cast, uintptr_t, __result)));
167 }
168 
thrd_join(thrd_t __thrd,int * __result)169 __BIONIC_THREADS_INLINE int thrd_join(thrd_t __thrd, int* __result) {
170   void* __pthread_result;
171   if (pthread_join(__thrd, &__pthread_result) != 0) return thrd_error;
172   if (__result) {
173     *__result = __BIONIC_CAST(reinterpret_cast, intptr_t, __pthread_result);
174   }
175   return thrd_success;
176 }
177 
thrd_sleep(const struct timespec * __duration,struct timespec * __remaining)178 __BIONIC_THREADS_INLINE int thrd_sleep(const struct timespec* __duration,
179                                        struct timespec* __remaining) {
180   int __rc = nanosleep(__duration, __remaining);
181   if (__rc == 0) return 0;
182   return (errno == EINTR) ? -1 : -2;
183 }
184 
thrd_yield(void)185 __BIONIC_THREADS_INLINE void thrd_yield(void) {
186   sched_yield();
187 }
188 
189 
190 
tss_create(tss_t * __key,tss_dtor_t __dtor)191 __BIONIC_THREADS_INLINE int tss_create(tss_t* __key, tss_dtor_t __dtor) {
192   return __bionic_thrd_error(pthread_key_create(__key, __dtor));
193 }
194 
tss_delete(tss_t __key)195 __BIONIC_THREADS_INLINE void tss_delete(tss_t __key) {
196   pthread_key_delete(__key);
197 }
198 
tss_get(tss_t __key)199 __BIONIC_THREADS_INLINE void* tss_get(tss_t __key) {
200   return pthread_getspecific(__key);
201 }
202 
tss_set(tss_t __key,void * __value)203 __BIONIC_THREADS_INLINE int tss_set(tss_t __key, void* __value) {
204   return __bionic_thrd_error(pthread_setspecific(__key, __value));
205 }
206 
207 __END_DECLS
208 
209 #endif  // __BIONIC_THREADS_INLINE
210