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