blob: b1008de0c40a747df1f7f837bb6263b0733a85ae [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/**
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. */
45typedef pthread_cond_t cnd_t;
46/** The type for a thread. */
47typedef pthread_t thrd_t;
48/** The type for a thread-specific storage key. */
49typedef pthread_key_t tss_t;
50/** The type for a mutex. */
51typedef pthread_mutex_t mtx_t;
52
53/** The type for a thread-specific storage destructor. */
zijunzhaoe5895742023-03-10 06:03:58 +000054typedef void (*tss_dtor_t)(void* _Nullable);
Elliott Hughes42067112019-04-18 14:27:24 -070055/** The type of the function passed to thrd_create() to create a new thread. */
zijunzhaoe5895742023-03-10 06:03:58 +000056typedef int (*thrd_start_t)(void* _Nullable);
Elliott Hughes42067112019-04-18 14:27:24 -070057
58/** The type used by call_once(). */
59typedef pthread_once_t once_flag;
60
61enum {
62 mtx_plain = 0x1,
63 mtx_recursive = 0x2,
64 mtx_timed = 0x4,
65};
66
67enum {
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 Hughes95c6cd72019-12-20 13:26:14 -080081#if __ANDROID_API__ >= 30
Elliott Hughes42067112019-04-18 14:27:24 -070082// This file is implemented as static inlines before API level 30.
83
84/** Uses `__flag` to ensure that `__function` is called exactly once. */
zijunzhaoe5895742023-03-10 06:03:58 +000085void call_once(once_flag* _Nonnull __flag, void (* _Nonnull __function)(void)) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -070086
87
88
89/**
90 * Unblocks all threads blocked on `__cond`.
91 */
zijunzhaoe5895742023-03-10 06:03:58 +000092int cnd_broadcast(cnd_t* _Nonnull __cond) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -070093
94/**
95 * Destroys a condition variable.
96 */
zijunzhaoe5895742023-03-10 06:03:58 +000097void cnd_destroy(cnd_t* _Nonnull __cond) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -070098
99/**
100 * Creates a condition variable.
101 */
zijunzhaoe5895742023-03-10 06:03:58 +0000102int cnd_init(cnd_t* _Nonnull __cond) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700103
104/**
105 * Unblocks one thread blocked on `__cond`.
106 */
zijunzhaoe5895742023-03-10 06:03:58 +0000107int cnd_signal(cnd_t* _Nonnull __cond) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700108
109/**
110 * Unlocks `__mutex` and blocks until `__cond` is signaled or `__timeout` occurs.
111 */
zijunzhaoe5895742023-03-10 06:03:58 +0000112int cnd_timedwait(cnd_t* _Nonnull __cond, mtx_t* _Nonnull __mutex, const struct timespec* _Nonnull __timeout)
Logan Chien235aad12019-08-27 20:29:29 -0700113 __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700114
115/**
116 * Unlocks `__mutex` and blocks until `__cond` is signaled.
117 */
zijunzhaoe5895742023-03-10 06:03:58 +0000118int cnd_wait(cnd_t* _Nonnull __cond, mtx_t* _Nonnull __mutex) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700119
120
121
122/**
123 * Destroys a mutex.
124 */
zijunzhaoe5895742023-03-10 06:03:58 +0000125void mtx_destroy(mtx_t* _Nonnull __mutex) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700126
127/**
128 * Creates a mutex.
129 */
zijunzhaoe5895742023-03-10 06:03:58 +0000130int mtx_init(mtx_t* _Nonnull __mutex, int __type) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700131
132/**
133 * Blocks until `__mutex` is acquired.
134 */
zijunzhaoe5895742023-03-10 06:03:58 +0000135int mtx_lock(mtx_t* _Nonnull __mutex) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700136
137/**
138 * Blocks until `__mutex` is acquired or `__timeout` expires.
139 */
zijunzhaoe5895742023-03-10 06:03:58 +0000140int mtx_timedlock(mtx_t* _Nonnull __mutex, const struct timespec* _Nonnull __timeout) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700141
142/**
143 * Acquires `__mutex` or returns `thrd_busy`.
144 */
zijunzhaoe5895742023-03-10 06:03:58 +0000145int mtx_trylock(mtx_t* _Nonnull __mutex) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700146
147/**
148 * Unlocks `__mutex`.
149 */
zijunzhaoe5895742023-03-10 06:03:58 +0000150int mtx_unlock(mtx_t* _Nonnull __mutex) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700151
152
153
154/**
155 * Creates a new thread running `__function(__arg)`, and sets `*__thrd` to
156 * the new thread.
157 */
zijunzhaoe5895742023-03-10 06:03:58 +0000158int thrd_create(thrd_t* _Nonnull __thrd, thrd_start_t _Nonnull __function, void* _Nullable __arg) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700159
160/**
161 * Returns the `thrd_t` corresponding to the caller.
162 */
Logan Chien235aad12019-08-27 20:29:29 -0700163thrd_t thrd_current(void) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700164
165/**
166 * Tells the OS to automatically dispose of `__thrd` when it exits.
167 */
Logan Chien235aad12019-08-27 20:29:29 -0700168int thrd_detach(thrd_t __thrd) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700169
170/**
171 * Tests whether two threads are the same thread.
172 */
Logan Chien235aad12019-08-27 20:29:29 -0700173int thrd_equal(thrd_t __lhs, thrd_t __rhs) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700174
175/**
176 * Terminates the calling thread, setting its result to `__result`.
177 */
Logan Chien235aad12019-08-27 20:29:29 -0700178void thrd_exit(int __result) __noreturn __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700179
180/**
181 * Blocks until `__thrd` terminates. If `__result` is not null, `*__result`
182 * is set to the exiting thread's result.
183 */
zijunzhaoe5895742023-03-10 06:03:58 +0000184int thrd_join(thrd_t __thrd, int* _Nullable __result) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700185
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 */
zijunzhaoe5895742023-03-10 06:03:58 +0000193int thrd_sleep(const struct timespec* _Nonnull __duration, struct timespec* _Nullable __remaining) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700194
195/**
196 * Request that other threads should be scheduled.
197 */
Logan Chien235aad12019-08-27 20:29:29 -0700198void thrd_yield(void) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700199
200
201
202/**
203 * Creates a thread-specific storage key with the associated destructor (which
204 * may be null).
205 */
zijunzhaoe5895742023-03-10 06:03:58 +0000206int tss_create(tss_t* _Nonnull __key, tss_dtor_t _Nullable __dtor) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700207
208/**
209 * Destroys a thread-specific storage key.
210 */
Logan Chien235aad12019-08-27 20:29:29 -0700211void tss_delete(tss_t __key) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700212
213/**
214 * Returns the value for the current thread held in the thread-specific storage
215 * identified by `__key`.
216 */
zijunzhaoe5895742023-03-10 06:03:58 +0000217void* _Nullable tss_get(tss_t __key) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700218
219/**
220 * Sets the current thread's value for the thread-specific storage identified
221 * by `__key` to `__value`.
222 */
zijunzhaoe5895742023-03-10 06:03:58 +0000223int tss_set(tss_t __key, void* _Nonnull __value) __INTRODUCED_IN(30);
Elliott Hughes42067112019-04-18 14:27:24 -0700224
225#endif
226
227__END_DECLS
228
229#include <android/legacy_threads_inlines.h>