Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 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 | /** |
| 32 | * @file threads.h |
| 33 | * @brief C11 threads. |
| 34 | */ |
| 35 | |
| 36 | #include <sys/cdefs.h> |
| 37 | |
| 38 | #include <pthread.h> |
| 39 | #include <time.h> |
| 40 | |
| 41 | #define ONCE_FLAG_INIT PTHREAD_ONCE_INIT |
| 42 | #define TSS_DTOR_ITERATIONS PTHREAD_DESTRUCTOR_ITERATIONS |
| 43 | |
| 44 | /** The type for a condition variable. */ |
| 45 | typedef pthread_cond_t cnd_t; |
| 46 | /** The type for a thread. */ |
| 47 | typedef pthread_t thrd_t; |
| 48 | /** The type for a thread-specific storage key. */ |
| 49 | typedef pthread_key_t tss_t; |
| 50 | /** The type for a mutex. */ |
| 51 | typedef pthread_mutex_t mtx_t; |
| 52 | |
| 53 | /** The type for a thread-specific storage destructor. */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 54 | typedef void (*tss_dtor_t)(void* _Nullable); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 55 | /** The type of the function passed to thrd_create() to create a new thread. */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 56 | typedef int (*thrd_start_t)(void* _Nullable); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 57 | |
| 58 | /** The type used by call_once(). */ |
| 59 | typedef pthread_once_t once_flag; |
| 60 | |
| 61 | enum { |
| 62 | mtx_plain = 0x1, |
| 63 | mtx_recursive = 0x2, |
| 64 | mtx_timed = 0x4, |
| 65 | }; |
| 66 | |
| 67 | enum { |
| 68 | thrd_success = 0, |
| 69 | thrd_busy = 1, |
| 70 | thrd_error = 2, |
| 71 | thrd_nomem = 3, |
| 72 | thrd_timedout = 4, |
| 73 | }; |
| 74 | |
| 75 | #if !defined(__cplusplus) |
| 76 | #define thread_local _Thread_local |
| 77 | #endif |
| 78 | |
| 79 | __BEGIN_DECLS |
| 80 | |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 81 | #if __ANDROID_API__ >= 30 |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 82 | // This file is implemented as static inlines before API level 30. |
| 83 | |
| 84 | /** Uses `__flag` to ensure that `__function` is called exactly once. */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 85 | void call_once(once_flag* _Nonnull __flag, void (* _Nonnull __function)(void)) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 86 | |
| 87 | |
| 88 | |
| 89 | /** |
| 90 | * Unblocks all threads blocked on `__cond`. |
| 91 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 92 | int cnd_broadcast(cnd_t* _Nonnull __cond) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Destroys a condition variable. |
| 96 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 97 | void cnd_destroy(cnd_t* _Nonnull __cond) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Creates a condition variable. |
| 101 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 102 | int cnd_init(cnd_t* _Nonnull __cond) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * Unblocks one thread blocked on `__cond`. |
| 106 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 107 | int cnd_signal(cnd_t* _Nonnull __cond) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Unlocks `__mutex` and blocks until `__cond` is signaled or `__timeout` occurs. |
| 111 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 112 | int cnd_timedwait(cnd_t* _Nonnull __cond, mtx_t* _Nonnull __mutex, const struct timespec* _Nonnull __timeout) |
Logan Chien | 235aad1 | 2019-08-27 20:29:29 -0700 | [diff] [blame] | 113 | __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * Unlocks `__mutex` and blocks until `__cond` is signaled. |
| 117 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 118 | int cnd_wait(cnd_t* _Nonnull __cond, mtx_t* _Nonnull __mutex) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 119 | |
| 120 | |
| 121 | |
| 122 | /** |
| 123 | * Destroys a mutex. |
| 124 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 125 | void mtx_destroy(mtx_t* _Nonnull __mutex) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * Creates a mutex. |
| 129 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 130 | int mtx_init(mtx_t* _Nonnull __mutex, int __type) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Blocks until `__mutex` is acquired. |
| 134 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 135 | int mtx_lock(mtx_t* _Nonnull __mutex) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * Blocks until `__mutex` is acquired or `__timeout` expires. |
| 139 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 140 | int mtx_timedlock(mtx_t* _Nonnull __mutex, const struct timespec* _Nonnull __timeout) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Acquires `__mutex` or returns `thrd_busy`. |
| 144 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 145 | int mtx_trylock(mtx_t* _Nonnull __mutex) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * Unlocks `__mutex`. |
| 149 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 150 | int mtx_unlock(mtx_t* _Nonnull __mutex) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 151 | |
| 152 | |
| 153 | |
| 154 | /** |
| 155 | * Creates a new thread running `__function(__arg)`, and sets `*__thrd` to |
| 156 | * the new thread. |
| 157 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 158 | int thrd_create(thrd_t* _Nonnull __thrd, thrd_start_t _Nonnull __function, void* _Nullable __arg) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * Returns the `thrd_t` corresponding to the caller. |
| 162 | */ |
Logan Chien | 235aad1 | 2019-08-27 20:29:29 -0700 | [diff] [blame] | 163 | thrd_t thrd_current(void) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * Tells the OS to automatically dispose of `__thrd` when it exits. |
| 167 | */ |
Logan Chien | 235aad1 | 2019-08-27 20:29:29 -0700 | [diff] [blame] | 168 | int thrd_detach(thrd_t __thrd) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * Tests whether two threads are the same thread. |
| 172 | */ |
Logan Chien | 235aad1 | 2019-08-27 20:29:29 -0700 | [diff] [blame] | 173 | int thrd_equal(thrd_t __lhs, thrd_t __rhs) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * Terminates the calling thread, setting its result to `__result`. |
| 177 | */ |
Logan Chien | 235aad1 | 2019-08-27 20:29:29 -0700 | [diff] [blame] | 178 | void thrd_exit(int __result) __noreturn __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 179 | |
| 180 | /** |
| 181 | * Blocks until `__thrd` terminates. If `__result` is not null, `*__result` |
| 182 | * is set to the exiting thread's result. |
| 183 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 184 | int thrd_join(thrd_t __thrd, int* _Nullable __result) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 185 | |
| 186 | /** |
| 187 | * Blocks the caller for at least `__duration` unless a signal is delivered. |
| 188 | * If a signal causes the sleep to end early and `__remaining` is not null, |
| 189 | * `*__remaining` is set to the time remaining. |
| 190 | * |
| 191 | * Returns 0 on success, or -1 if a signal was delivered. |
| 192 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 193 | int thrd_sleep(const struct timespec* _Nonnull __duration, struct timespec* _Nullable __remaining) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 194 | |
| 195 | /** |
| 196 | * Request that other threads should be scheduled. |
| 197 | */ |
Logan Chien | 235aad1 | 2019-08-27 20:29:29 -0700 | [diff] [blame] | 198 | void thrd_yield(void) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 199 | |
| 200 | |
| 201 | |
| 202 | /** |
| 203 | * Creates a thread-specific storage key with the associated destructor (which |
| 204 | * may be null). |
| 205 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 206 | int tss_create(tss_t* _Nonnull __key, tss_dtor_t _Nullable __dtor) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 207 | |
| 208 | /** |
| 209 | * Destroys a thread-specific storage key. |
| 210 | */ |
Logan Chien | 235aad1 | 2019-08-27 20:29:29 -0700 | [diff] [blame] | 211 | void tss_delete(tss_t __key) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 212 | |
| 213 | /** |
| 214 | * Returns the value for the current thread held in the thread-specific storage |
| 215 | * identified by `__key`. |
| 216 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 217 | void* _Nullable tss_get(tss_t __key) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 218 | |
| 219 | /** |
| 220 | * Sets the current thread's value for the thread-specific storage identified |
| 221 | * by `__key` to `__value`. |
| 222 | */ |
zijunzhao | e589574 | 2023-03-10 06:03:58 +0000 | [diff] [blame] | 223 | int tss_set(tss_t __key, void* _Nonnull __value) __INTRODUCED_IN(30); |
Elliott Hughes | 4206711 | 2019-04-18 14:27:24 -0700 | [diff] [blame] | 224 | |
| 225 | #endif |
| 226 | |
| 227 | __END_DECLS |
| 228 | |
| 229 | #include <android/legacy_threads_inlines.h> |