blob: 2c43b0b65797de6ee0605baa53cf0e51736ed1d1 [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. */
54typedef void (*tss_dtor_t)(void*);
55/** The type of the function passed to thrd_create() to create a new thread. */
56typedef int (*thrd_start_t)(void*);
57
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
81#if __ANDROID_API__ >= __ANDROID_API_R__
82// This file is implemented as static inlines before API level 30.
83
84/** Uses `__flag` to ensure that `__function` is called exactly once. */
85void call_once(once_flag* __flag, void (*__function)(void));
86
87
88
89/**
90 * Unblocks all threads blocked on `__cond`.
91 */
92int cnd_broadcast(cnd_t* __cond);
93
94/**
95 * Destroys a condition variable.
96 */
97void cnd_destroy(cnd_t* __cond);
98
99/**
100 * Creates a condition variable.
101 */
102int cnd_init(cnd_t* __cond);
103
104/**
105 * Unblocks one thread blocked on `__cond`.
106 */
107int cnd_signal(cnd_t* __cond);
108
109/**
110 * Unlocks `__mutex` and blocks until `__cond` is signaled or `__timeout` occurs.
111 */
112int cnd_timedwait(cnd_t* __cond, mtx_t* __mutex, const struct timespec* __timeout);
113
114/**
115 * Unlocks `__mutex` and blocks until `__cond` is signaled.
116 */
117int cnd_wait(cnd_t* __cond, mtx_t* __mutex);
118
119
120
121/**
122 * Destroys a mutex.
123 */
124void mtx_destroy(mtx_t* __mutex);
125
126/**
127 * Creates a mutex.
128 */
129int mtx_init(mtx_t* __mutex, int __type);
130
131/**
132 * Blocks until `__mutex` is acquired.
133 */
134int mtx_lock(mtx_t* __mutex);
135
136/**
137 * Blocks until `__mutex` is acquired or `__timeout` expires.
138 */
139int mtx_timedlock(mtx_t* __mutex, const struct timespec* __timeout);
140
141/**
142 * Acquires `__mutex` or returns `thrd_busy`.
143 */
144int mtx_trylock(mtx_t* __mutex);
145
146/**
147 * Unlocks `__mutex`.
148 */
149int mtx_unlock(mtx_t* __mutex);
150
151
152
153/**
154 * Creates a new thread running `__function(__arg)`, and sets `*__thrd` to
155 * the new thread.
156 */
157int thrd_create(thrd_t* __thrd, thrd_start_t __function, void* __arg);
158
159/**
160 * Returns the `thrd_t` corresponding to the caller.
161 */
162thrd_t thrd_current(void);
163
164/**
165 * Tells the OS to automatically dispose of `__thrd` when it exits.
166 */
167int thrd_detach(thrd_t __thrd);
168
169/**
170 * Tests whether two threads are the same thread.
171 */
172int thrd_equal(thrd_t __lhs, thrd_t __rhs);
173
174/**
175 * Terminates the calling thread, setting its result to `__result`.
176 */
177void thrd_exit(int __result) __noreturn;
178
179/**
180 * Blocks until `__thrd` terminates. If `__result` is not null, `*__result`
181 * is set to the exiting thread's result.
182 */
183int thrd_join(thrd_t __thrd, int* __result);
184
185/**
186 * Blocks the caller for at least `__duration` unless a signal is delivered.
187 * If a signal causes the sleep to end early and `__remaining` is not null,
188 * `*__remaining` is set to the time remaining.
189 *
190 * Returns 0 on success, or -1 if a signal was delivered.
191 */
192int thrd_sleep(const struct timespec* __duration, struct timespec* __remaining);
193
194/**
195 * Request that other threads should be scheduled.
196 */
197void thrd_yield(void);
198
199
200
201/**
202 * Creates a thread-specific storage key with the associated destructor (which
203 * may be null).
204 */
205int tss_create(tss_t* __key, tss_dtor_t __dtor);
206
207/**
208 * Destroys a thread-specific storage key.
209 */
210void tss_delete(tss_t __key);
211
212/**
213 * Returns the value for the current thread held in the thread-specific storage
214 * identified by `__key`.
215 */
216void* tss_get(tss_t __key);
217
218/**
219 * Sets the current thread's value for the thread-specific storage identified
220 * by `__key` to `__value`.
221 */
222int tss_set(tss_t __key, void* __value);
223
224#endif
225
226__END_DECLS
227
228#include <android/legacy_threads_inlines.h>