blob: 05b785a81524b877218aeacb7e0df30a52ff66c0 [file] [log] [blame]
Elliott Hughes42067112019-04-18 14:27:24 -07001/*
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
Logan Chien235aad12019-08-27 20:29:29 -070037#if defined(__BIONIC_THREADS_INLINE)
Elliott Hughes42067112019-04-18 14:27:24 -070038
39__BEGIN_DECLS
40
Dan Albert2237fcf2024-05-14 17:53:58 +000041static __inline int __bionic_thrd_error(int __pthread_code) {
Elliott Hughes42067112019-04-18 14:27:24 -070042 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
zijunzhaoa8d42a42023-06-07 23:02:37 +000051__BIONIC_THREADS_INLINE void call_once(once_flag* _Nonnull __flag,
52 void (* _Nonnull __function)(void)) {
Elliott Hughes42067112019-04-18 14:27:24 -070053 pthread_once(__flag, __function);
54}
55
56
57
zijunzhaoa8d42a42023-06-07 23:02:37 +000058__BIONIC_THREADS_INLINE int cnd_broadcast(cnd_t* _Nonnull __cnd) {
Elliott Hughes42067112019-04-18 14:27:24 -070059 return __bionic_thrd_error(pthread_cond_broadcast(__cnd));
60}
61
zijunzhaoa8d42a42023-06-07 23:02:37 +000062__BIONIC_THREADS_INLINE void cnd_destroy(cnd_t* _Nonnull __cnd) {
Elliott Hughes42067112019-04-18 14:27:24 -070063 pthread_cond_destroy(__cnd);
64}
65
zijunzhaoa8d42a42023-06-07 23:02:37 +000066__BIONIC_THREADS_INLINE int cnd_init(cnd_t* _Nonnull __cnd) {
Elliott Hughes42067112019-04-18 14:27:24 -070067 return __bionic_thrd_error(pthread_cond_init(__cnd, NULL));
68}
69
zijunzhaoa8d42a42023-06-07 23:02:37 +000070__BIONIC_THREADS_INLINE int cnd_signal(cnd_t* _Nonnull __cnd) {
Elliott Hughes42067112019-04-18 14:27:24 -070071 return __bionic_thrd_error(pthread_cond_signal(__cnd));
72}
73
zijunzhaoa8d42a42023-06-07 23:02:37 +000074__BIONIC_THREADS_INLINE int cnd_timedwait(cnd_t* _Nonnull __cnd,
75 mtx_t* _Nonnull __mtx,
76 const struct timespec* _Nullable __timeout) {
Elliott Hughes42067112019-04-18 14:27:24 -070077 return __bionic_thrd_error(pthread_cond_timedwait(__cnd, __mtx, __timeout));
78}
79
zijunzhaoa8d42a42023-06-07 23:02:37 +000080__BIONIC_THREADS_INLINE int cnd_wait(cnd_t* _Nonnull __cnd, mtx_t* _Nonnull __mtx) {
Elliott Hughes42067112019-04-18 14:27:24 -070081 return __bionic_thrd_error(pthread_cond_wait(__cnd, __mtx));
82}
83
84
85
zijunzhaoa8d42a42023-06-07 23:02:37 +000086__BIONIC_THREADS_INLINE void mtx_destroy(mtx_t* _Nonnull __mtx) {
Elliott Hughes42067112019-04-18 14:27:24 -070087 pthread_mutex_destroy(__mtx);
88}
89
zijunzhaoa8d42a42023-06-07 23:02:37 +000090__BIONIC_THREADS_INLINE int mtx_init(mtx_t* _Nonnull __mtx, int __type) {
Elliott Hughes42067112019-04-18 14:27:24 -070091 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
zijunzhaoa8d42a42023-06-07 23:02:37 +0000102__BIONIC_THREADS_INLINE int mtx_lock(mtx_t* _Nonnull __mtx) {
Elliott Hughes42067112019-04-18 14:27:24 -0700103 return __bionic_thrd_error(pthread_mutex_lock(__mtx));
104}
105
zijunzhaoa8d42a42023-06-07 23:02:37 +0000106__BIONIC_THREADS_INLINE int mtx_timedlock(mtx_t* _Nonnull __mtx,
107 const struct timespec* _Nullable __timeout) {
Elliott Hughes42067112019-04-18 14:27:24 -0700108 return __bionic_thrd_error(pthread_mutex_timedlock(__mtx, __timeout));
109}
110
zijunzhaoa8d42a42023-06-07 23:02:37 +0000111__BIONIC_THREADS_INLINE int mtx_trylock(mtx_t* _Nonnull __mtx) {
Elliott Hughes42067112019-04-18 14:27:24 -0700112 return __bionic_thrd_error(pthread_mutex_trylock(__mtx));
113}
114
zijunzhaoa8d42a42023-06-07 23:02:37 +0000115__BIONIC_THREADS_INLINE int mtx_unlock(mtx_t* _Nonnull __mtx) {
Elliott Hughes42067112019-04-18 14:27:24 -0700116 return __bionic_thrd_error(pthread_mutex_unlock(__mtx));
117}
118
Elliott Hughes42067112019-04-18 14:27:24 -0700119struct __bionic_thrd_data {
Elliott Hughes2b27f572024-08-22 21:17:34 +0000120 thrd_start_t _Nonnull __func;
121 void* _Nullable __arg;
Elliott Hughes42067112019-04-18 14:27:24 -0700122};
123
Elliott Hughes3469e7d2024-05-15 16:06:07 +0000124static __inline void* _Nonnull __bionic_thrd_trampoline(void* _Nonnull __arg) {
Elliott Hughes42067112019-04-18 14:27:24 -0700125 struct __bionic_thrd_data __data =
126 *__BIONIC_CAST(static_cast, struct __bionic_thrd_data*, __arg);
127 free(__arg);
128 int __result = __data.__func(__data.__arg);
129 return __BIONIC_CAST(reinterpret_cast, void*,
130 __BIONIC_CAST(static_cast, uintptr_t, __result));
131}
132
zijunzhaoa8d42a42023-06-07 23:02:37 +0000133__BIONIC_THREADS_INLINE int thrd_create(thrd_t* _Nonnull __thrd,
134 thrd_start_t _Nonnull __func,
135 void* _Nullable __arg) {
Elliott Hughes42067112019-04-18 14:27:24 -0700136 struct __bionic_thrd_data* __pthread_arg =
137 __BIONIC_CAST(static_cast, struct __bionic_thrd_data*,
138 malloc(sizeof(struct __bionic_thrd_data)));
139 __pthread_arg->__func = __func;
140 __pthread_arg->__arg = __arg;
141 int __result = __bionic_thrd_error(pthread_create(__thrd, NULL,
142 __bionic_thrd_trampoline,
143 __pthread_arg));
144 if (__result != thrd_success) free(__pthread_arg);
145 return __result;
146}
147
148__BIONIC_THREADS_INLINE thrd_t thrd_current(void) {
149 return pthread_self();
150}
151
152__BIONIC_THREADS_INLINE int thrd_detach(thrd_t __thrd) {
153 return __bionic_thrd_error(pthread_detach(__thrd));
154}
155
156__BIONIC_THREADS_INLINE int thrd_equal(thrd_t __lhs, thrd_t __rhs) {
157 return pthread_equal(__lhs, __rhs);
158}
159
160__BIONIC_THREADS_INLINE void thrd_exit(int __result) {
161 pthread_exit(__BIONIC_CAST(reinterpret_cast, void*,
162 __BIONIC_CAST(static_cast, uintptr_t, __result)));
163}
164
zijunzhaoa8d42a42023-06-07 23:02:37 +0000165__BIONIC_THREADS_INLINE int thrd_join(thrd_t __thrd, int* _Nullable __result) {
Elliott Hughes42067112019-04-18 14:27:24 -0700166 void* __pthread_result;
167 if (pthread_join(__thrd, &__pthread_result) != 0) return thrd_error;
168 if (__result) {
169 *__result = __BIONIC_CAST(reinterpret_cast, intptr_t, __pthread_result);
170 }
171 return thrd_success;
172}
173
zijunzhaoa8d42a42023-06-07 23:02:37 +0000174__BIONIC_THREADS_INLINE int thrd_sleep(const struct timespec* _Nonnull __duration,
175 struct timespec* _Nullable __remaining) {
Elliott Hughes42067112019-04-18 14:27:24 -0700176 int __rc = nanosleep(__duration, __remaining);
177 if (__rc == 0) return 0;
178 return (errno == EINTR) ? -1 : -2;
179}
180
181__BIONIC_THREADS_INLINE void thrd_yield(void) {
182 sched_yield();
183}
184
185
186
zijunzhaoa8d42a42023-06-07 23:02:37 +0000187__BIONIC_THREADS_INLINE int tss_create(tss_t* _Nonnull __key, tss_dtor_t _Nullable __dtor) {
Elliott Hughes42067112019-04-18 14:27:24 -0700188 return __bionic_thrd_error(pthread_key_create(__key, __dtor));
189}
190
191__BIONIC_THREADS_INLINE void tss_delete(tss_t __key) {
192 pthread_key_delete(__key);
193}
194
zijunzhaoa8d42a42023-06-07 23:02:37 +0000195__BIONIC_THREADS_INLINE void* _Nullable tss_get(tss_t __key) {
Elliott Hughes42067112019-04-18 14:27:24 -0700196 return pthread_getspecific(__key);
197}
198
zijunzhaoa8d42a42023-06-07 23:02:37 +0000199__BIONIC_THREADS_INLINE int tss_set(tss_t __key, void* _Nonnull __value) {
Elliott Hughes42067112019-04-18 14:27:24 -0700200 return __bionic_thrd_error(pthread_setspecific(__key, __value));
201}
202
203__END_DECLS
Logan Chien235aad12019-08-27 20:29:29 -0700204
205#endif // __BIONIC_THREADS_INLINE