blob: ccafd784a09fb7d856d6d58237f37281d388f185 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright (C) 2008 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 */
Elliott Hughesc3f11402013-10-30 14:40:09 -070028
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080029#ifndef _PTHREAD_H_
30#define _PTHREAD_H_
31
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080032#include <limits.h>
Elliott Hughes5704c422016-01-25 18:06:24 -080033#include <bits/pthread_types.h>
Dan Albert75ef63d2014-11-21 00:18:07 -080034#include <sched.h>
35#include <sys/cdefs.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036#include <sys/types.h>
Dan Albert75ef63d2014-11-21 00:18:07 -080037#include <time.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038
Elliott Hughes5470c182016-07-22 11:36:17 -070039__BEGIN_DECLS
40
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041enum {
42 PTHREAD_MUTEX_NORMAL = 0,
43 PTHREAD_MUTEX_RECURSIVE = 1,
44 PTHREAD_MUTEX_ERRORCHECK = 2,
45
46 PTHREAD_MUTEX_ERRORCHECK_NP = PTHREAD_MUTEX_ERRORCHECK,
47 PTHREAD_MUTEX_RECURSIVE_NP = PTHREAD_MUTEX_RECURSIVE,
48
49 PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
50};
51
Yabin Cui17393b02015-03-21 15:08:25 -070052#define PTHREAD_MUTEX_INITIALIZER { { ((PTHREAD_MUTEX_NORMAL & 3) << 14) } }
53#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP { { ((PTHREAD_MUTEX_RECURSIVE & 3) << 14) } }
54#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP { { ((PTHREAD_MUTEX_ERRORCHECK & 3) << 14) } }
55
Yabin Cui32651b82015-03-13 20:30:00 -070056#define PTHREAD_COND_INITIALIZER { { 0 } }
Elliott Hughesc3f11402013-10-30 14:40:09 -070057
Yabin Cui2fabea42015-03-13 14:22:05 -070058#define PTHREAD_RWLOCK_INITIALIZER { { 0 } }
Elliott Hughesc3f11402013-10-30 14:40:09 -070059
Yabin Cui76615da2015-03-17 14:22:09 -070060enum {
61 PTHREAD_RWLOCK_PREFER_READER_NP = 0,
62 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP = 1,
63};
64
Calin Juravleb1cacd42014-04-23 17:35:04 +010065#define PTHREAD_ONCE_INIT 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066
Elliott Hughes468a5d22017-03-21 09:39:56 -070067#if __ANDROID_API__ >= __ANDROID_API_N__
Yabin Cuie7c2fff2015-11-05 22:06:09 -080068#define PTHREAD_BARRIER_SERIAL_THREAD -1
Elliott Hughes468a5d22017-03-21 09:39:56 -070069#endif
Yabin Cuie7c2fff2015-11-05 22:06:09 -080070
Christopher Ferrisea5248e2014-05-14 17:58:29 -070071#if defined(__LP64__)
72#define PTHREAD_STACK_MIN (4 * PAGE_SIZE)
73#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074#define PTHREAD_STACK_MIN (2 * PAGE_SIZE)
Christopher Ferrisea5248e2014-05-14 17:58:29 -070075#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080076
77#define PTHREAD_CREATE_DETACHED 0x00000001
78#define PTHREAD_CREATE_JOINABLE 0x00000000
79
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080#define PTHREAD_PROCESS_PRIVATE 0
81#define PTHREAD_PROCESS_SHARED 1
82
83#define PTHREAD_SCOPE_SYSTEM 0
84#define PTHREAD_SCOPE_PROCESS 1
85
Josh Gao46b44162016-05-27 11:14:16 -070086int pthread_atfork(void (*)(void), void (*)(void), void (*)(void)) __INTRODUCED_IN(12);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080087
Elliott Hughes3f66e742017-08-01 13:24:40 -070088int pthread_attr_destroy(pthread_attr_t*);
89int pthread_attr_getdetachstate(const pthread_attr_t*, int*);
90int pthread_attr_getguardsize(const pthread_attr_t*, size_t*);
91int pthread_attr_getschedparam(const pthread_attr_t*, struct sched_param*);
92int pthread_attr_getschedpolicy(const pthread_attr_t*, int*);
93int pthread_attr_getscope(const pthread_attr_t*, int*);
94int pthread_attr_getstack(const pthread_attr_t*, void**, size_t*);
95int pthread_attr_getstacksize(const pthread_attr_t*, size_t*);
96int pthread_attr_init(pthread_attr_t*);
97int pthread_attr_setdetachstate(pthread_attr_t*, int);
98int pthread_attr_setguardsize(pthread_attr_t*, size_t);
99int pthread_attr_setschedparam(pthread_attr_t*, const struct sched_param*);
100int pthread_attr_setschedpolicy(pthread_attr_t*, int);
101int pthread_attr_setscope(pthread_attr_t*, int);
102int pthread_attr_setstack(pthread_attr_t*, void*, size_t);
103int pthread_attr_setstacksize(pthread_attr_t*, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800104
Elliott Hughes3f66e742017-08-01 13:24:40 -0700105int pthread_condattr_destroy(pthread_condattr_t*);
106int pthread_condattr_getclock(const pthread_condattr_t*, clockid_t*) __INTRODUCED_IN(21);
107int pthread_condattr_getpshared(const pthread_condattr_t*, int*);
108int pthread_condattr_init(pthread_condattr_t*);
109int pthread_condattr_setclock(pthread_condattr_t*, clockid_t) __INTRODUCED_IN(21);
110int pthread_condattr_setpshared(pthread_condattr_t*, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111
Elliott Hughes3f66e742017-08-01 13:24:40 -0700112int pthread_cond_broadcast(pthread_cond_t*);
113int pthread_cond_destroy(pthread_cond_t*);
114int pthread_cond_init(pthread_cond_t*, const pthread_condattr_t*);
115int pthread_cond_signal(pthread_cond_t*);
116int pthread_cond_timedwait(pthread_cond_t*, pthread_mutex_t*, const struct timespec*);
117int pthread_cond_wait(pthread_cond_t*, pthread_mutex_t*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118
Elliott Hughes3f66e742017-08-01 13:24:40 -0700119int pthread_create(pthread_t*, pthread_attr_t const*, void* (*start_routine)(void*), void*);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700120int pthread_detach(pthread_t);
121void pthread_exit(void*) __noreturn;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122
Elliott Hughesc3f11402013-10-30 14:40:09 -0700123int pthread_equal(pthread_t, pthread_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124
Elliott Hughes3f66e742017-08-01 13:24:40 -0700125int pthread_getattr_np(pthread_t, pthread_attr_t*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800126
Elliott Hughes3f66e742017-08-01 13:24:40 -0700127int pthread_getcpuclockid(pthread_t, clockid_t*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128
Elliott Hughes3f66e742017-08-01 13:24:40 -0700129int pthread_getschedparam(pthread_t, int*, struct sched_param*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800130
Elliott Hughesc3f11402013-10-30 14:40:09 -0700131void* pthread_getspecific(pthread_key_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800132
Josh Gao14adff12016-04-29 12:00:55 -0700133pid_t pthread_gettid_np(pthread_t) __INTRODUCED_IN(21);
Elliott Hughesa4831cb2014-09-11 16:11:43 -0700134
Elliott Hughesc3f11402013-10-30 14:40:09 -0700135int pthread_join(pthread_t, void**);
136
Elliott Hughes3f66e742017-08-01 13:24:40 -0700137int pthread_key_create(pthread_key_t*, void (*)(void*));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700138int pthread_key_delete(pthread_key_t);
139
Elliott Hughes3f66e742017-08-01 13:24:40 -0700140int pthread_mutexattr_destroy(pthread_mutexattr_t*);
141int pthread_mutexattr_getpshared(const pthread_mutexattr_t*, int*);
142int pthread_mutexattr_gettype(const pthread_mutexattr_t*, int*);
143int pthread_mutexattr_init(pthread_mutexattr_t*);
144int pthread_mutexattr_setpshared(pthread_mutexattr_t*, int);
145int pthread_mutexattr_settype(pthread_mutexattr_t*, int);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700146
Elliott Hughes3f66e742017-08-01 13:24:40 -0700147int pthread_mutex_destroy(pthread_mutex_t*);
148int pthread_mutex_init(pthread_mutex_t*, const pthread_mutexattr_t*);
149int pthread_mutex_lock(pthread_mutex_t*);
150int pthread_mutex_timedlock(pthread_mutex_t*, const struct timespec* )
Josh Gao14adff12016-04-29 12:00:55 -0700151 __INTRODUCED_IN(21);
Elliott Hughes3f66e742017-08-01 13:24:40 -0700152int pthread_mutex_trylock(pthread_mutex_t*);
153int pthread_mutex_unlock(pthread_mutex_t*);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700154
Elliott Hughes0138c462017-08-07 09:04:08 -0700155#if __ANDROID_API__ < 21
Dan Albert8c67f1e2017-06-12 14:17:49 -0700156/*
Dan Albertc2ce1202017-06-22 12:40:55 -0700157 * Cruft for supporting old API levels. Pre-L we didn't have the proper POSIX
158 * APIs for things, but instead had some locally grown, artisan equivalents.
159 * Keep exposing the old prototypes on old API levels so we don't regress
160 * functionality.
Dan Albert8c67f1e2017-06-12 14:17:49 -0700161 *
Dan Albertc2ce1202017-06-22 12:40:55 -0700162 * See the following bugs:
163 * * https://github.com/android-ndk/ndk/issues/420
164 * * https://github.com/android-ndk/ndk/issues/423
165 * * https://stackoverflow.com/q/44580542/632035
Dan Albert8c67f1e2017-06-12 14:17:49 -0700166 */
167int pthread_mutex_lock_timeout_np(pthread_mutex_t* mutex, unsigned msecs);
Dan Albertc2ce1202017-06-22 12:40:55 -0700168int pthread_cond_timeout_np(pthread_cond_t* cond, pthread_mutex_t* mutex, unsigned msecs);
169int pthread_cond_timedwait_monotonic_np(pthread_cond_t*, pthread_mutex_t*, const struct timespec*);
170int pthread_cond_timedwait_relative_np(pthread_cond_t* cond, pthread_mutex_t* mutex,
171 const struct timespec* reltime);
Dan Albert8c67f1e2017-06-12 14:17:49 -0700172#endif
173
Elliott Hughes3f66e742017-08-01 13:24:40 -0700174int pthread_once(pthread_once_t*, void (*init_routine)(void));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700175
Elliott Hughes3f66e742017-08-01 13:24:40 -0700176int pthread_rwlockattr_init(pthread_rwlockattr_t*);
177int pthread_rwlockattr_destroy(pthread_rwlockattr_t*);
178int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t*, int*);
179int pthread_rwlockattr_setpshared(pthread_rwlockattr_t*, int);
180int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t*, int*)
Josh Gao14adff12016-04-29 12:00:55 -0700181 __INTRODUCED_IN(23);
Elliott Hughes3f66e742017-08-01 13:24:40 -0700182int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t*, int) __INTRODUCED_IN(23);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700183
Elliott Hughes3f66e742017-08-01 13:24:40 -0700184int pthread_rwlock_destroy(pthread_rwlock_t*);
185int pthread_rwlock_init(pthread_rwlock_t*, const pthread_rwlockattr_t*);
186int pthread_rwlock_rdlock(pthread_rwlock_t*);
187int pthread_rwlock_timedrdlock(pthread_rwlock_t*, const struct timespec*);
188int pthread_rwlock_timedwrlock(pthread_rwlock_t*, const struct timespec*);
189int pthread_rwlock_tryrdlock(pthread_rwlock_t*);
190int pthread_rwlock_trywrlock(pthread_rwlock_t*);
191int pthread_rwlock_unlock(pthread_rwlock_t*);
192int pthread_rwlock_wrlock(pthread_rwlock_t*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800193
Elliott Hughes468a5d22017-03-21 09:39:56 -0700194#if __ANDROID_API__ >= __ANDROID_API_N__
Elliott Hughes3f66e742017-08-01 13:24:40 -0700195int pthread_barrierattr_init(pthread_barrierattr_t* attr) __INTRODUCED_IN(24);
196int pthread_barrierattr_destroy(pthread_barrierattr_t* attr) __INTRODUCED_IN(24);
197int pthread_barrierattr_getpshared(const pthread_barrierattr_t* attr,
198 int* pshared) __INTRODUCED_IN(24);
199int pthread_barrierattr_setpshared(pthread_barrierattr_t* attr, int pshared)
Josh Gao14adff12016-04-29 12:00:55 -0700200 __INTRODUCED_IN(24);
Elliott Hughes468a5d22017-03-21 09:39:56 -0700201#endif
Yabin Cuie7c2fff2015-11-05 22:06:09 -0800202
Elliott Hughes468a5d22017-03-21 09:39:56 -0700203#if __ANDROID_API__ >= __ANDROID_API_N__
Elliott Hughes3f66e742017-08-01 13:24:40 -0700204int pthread_barrier_init(pthread_barrier_t*, const pthread_barrierattr_t*, unsigned)
Josh Gao14adff12016-04-29 12:00:55 -0700205 __INTRODUCED_IN(24);
Elliott Hughes3f66e742017-08-01 13:24:40 -0700206int pthread_barrier_destroy(pthread_barrier_t*) __INTRODUCED_IN(24);
207int pthread_barrier_wait(pthread_barrier_t*) __INTRODUCED_IN(24);
Elliott Hughes468a5d22017-03-21 09:39:56 -0700208#endif
Yabin Cuie7c2fff2015-11-05 22:06:09 -0800209
Elliott Hughes468a5d22017-03-21 09:39:56 -0700210#if __ANDROID_API__ >= __ANDROID_API_N__
Elliott Hughes3f66e742017-08-01 13:24:40 -0700211int pthread_spin_destroy(pthread_spinlock_t*) __INTRODUCED_IN(24);
212int pthread_spin_init(pthread_spinlock_t*, int) __INTRODUCED_IN(24);
213int pthread_spin_lock(pthread_spinlock_t*) __INTRODUCED_IN(24);
214int pthread_spin_trylock(pthread_spinlock_t*) __INTRODUCED_IN(24);
215int pthread_spin_unlock(pthread_spinlock_t*) __INTRODUCED_IN(24);
Elliott Hughes468a5d22017-03-21 09:39:56 -0700216#endif
Yabin Cuife3a83a2015-11-17 16:03:18 -0800217
Elliott Hughes95fa0612016-09-28 12:29:52 -0700218pthread_t pthread_self(void) __attribute_const__;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800219
Elliott Hughes725b2a92016-03-23 11:20:47 -0700220#if defined(__USE_GNU)
Elliott Hughes3f66e742017-08-01 13:24:40 -0700221int pthread_getname_np(pthread_t, char*, size_t) __INTRODUCED_IN(26);
Elliott Hughes725b2a92016-03-23 11:20:47 -0700222#endif
Elliott Hughescc78b252016-03-23 20:26:35 -0700223/* TODO: this should be __USE_GNU too. */
Elliott Hughes3f66e742017-08-01 13:24:40 -0700224int pthread_setname_np(pthread_t, const char*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800225
Elliott Hughes3f66e742017-08-01 13:24:40 -0700226int pthread_setschedparam(pthread_t, int, const struct sched_param*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800227
Elliott Hughesc3f11402013-10-30 14:40:09 -0700228int pthread_setspecific(pthread_key_t, const void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800229
Elliott Hughesc3f11402013-10-30 14:40:09 -0700230typedef void (*__pthread_cleanup_func_t)(void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800231
232typedef struct __pthread_cleanup_t {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700233 struct __pthread_cleanup_t* __cleanup_prev;
234 __pthread_cleanup_func_t __cleanup_routine;
235 void* __cleanup_arg;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800236} __pthread_cleanup_t;
237
Elliott Hughes5470c182016-07-22 11:36:17 -0700238void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t, void*);
239void __pthread_cleanup_pop(__pthread_cleanup_t*, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800240
241/* Believe or not, the definitions of pthread_cleanup_push and
242 * pthread_cleanup_pop below are correct. Posix states that these
243 * can be implemented as macros that might introduce opening and
244 * closing braces, and that using setjmp/longjmp/return/break/continue
Elliott Hughesc3f11402013-10-30 14:40:09 -0700245 * between them results in undefined behavior.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800246 */
247#define pthread_cleanup_push(routine, arg) \
248 do { \
249 __pthread_cleanup_t __cleanup; \
250 __pthread_cleanup_push( &__cleanup, (routine), (arg) ); \
251
252#define pthread_cleanup_pop(execute) \
253 __pthread_cleanup_pop( &__cleanup, (execute)); \
Elliott Hughesc3f11402013-10-30 14:40:09 -0700254 } while (0); \
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800255
Elliott Hughesc3f11402013-10-30 14:40:09 -0700256__END_DECLS
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800257
David 'Digit' Turnerbd8d9872010-09-26 23:00:36 +0200258#endif /* _PTHREAD_H_ */