blob: 47dc1531352535f625f7ff6c57d234dbad7dc30f [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
41#if defined(__clang__)
42#pragma clang diagnostic push
43#pragma clang diagnostic ignored "-Wnullability-completeness"
44#endif
45
Elliott Hughesc3f11402013-10-30 14:40:09 -070046typedef struct {
Yabin Cui17393b02015-03-21 15:08:25 -070047#if defined(__LP64__)
48 int32_t __private[10];
49#else
50 int32_t __private[1];
Calin Juravleb1cacd42014-04-23 17:35:04 +010051#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052} pthread_mutex_t;
53
Yabin Cui17393b02015-03-21 15:08:25 -070054typedef long pthread_mutexattr_t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055
56enum {
57 PTHREAD_MUTEX_NORMAL = 0,
58 PTHREAD_MUTEX_RECURSIVE = 1,
59 PTHREAD_MUTEX_ERRORCHECK = 2,
60
61 PTHREAD_MUTEX_ERRORCHECK_NP = PTHREAD_MUTEX_ERRORCHECK,
62 PTHREAD_MUTEX_RECURSIVE_NP = PTHREAD_MUTEX_RECURSIVE,
63
64 PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
65};
66
Yabin Cui17393b02015-03-21 15:08:25 -070067#define PTHREAD_MUTEX_INITIALIZER { { ((PTHREAD_MUTEX_NORMAL & 3) << 14) } }
68#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP { { ((PTHREAD_MUTEX_RECURSIVE & 3) << 14) } }
69#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP { { ((PTHREAD_MUTEX_ERRORCHECK & 3) << 14) } }
70
Elliott Hughesc3f11402013-10-30 14:40:09 -070071typedef struct {
Yabin Cui32651b82015-03-13 20:30:00 -070072#if defined(__LP64__)
Yabin Cui17393b02015-03-21 15:08:25 -070073 int32_t __private[12];
Yabin Cui32651b82015-03-13 20:30:00 -070074#else
Yabin Cui17393b02015-03-21 15:08:25 -070075 int32_t __private[1];
Calin Juravleb1cacd42014-04-23 17:35:04 +010076#endif
Yabin Cui17393b02015-03-21 15:08:25 -070077} pthread_cond_t;
78
79typedef long pthread_condattr_t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080
Yabin Cui32651b82015-03-13 20:30:00 -070081#define PTHREAD_COND_INITIALIZER { { 0 } }
Elliott Hughesc3f11402013-10-30 14:40:09 -070082
Elliott Hughesc3f11402013-10-30 14:40:09 -070083typedef struct {
Yabin Cui2fabea42015-03-13 14:22:05 -070084#if defined(__LP64__)
Yabin Cui17393b02015-03-21 15:08:25 -070085 int32_t __private[14];
Calin Juravle92687e42014-05-22 19:21:22 +010086#else
Yabin Cui17393b02015-03-21 15:08:25 -070087 int32_t __private[10];
Calin Juravle92687e42014-05-22 19:21:22 +010088#endif
Yabin Cui17393b02015-03-21 15:08:25 -070089} pthread_rwlock_t;
90
91typedef long pthread_rwlockattr_t;
Elliott Hughesc3f11402013-10-30 14:40:09 -070092
Yabin Cui2fabea42015-03-13 14:22:05 -070093#define PTHREAD_RWLOCK_INITIALIZER { { 0 } }
Elliott Hughesc3f11402013-10-30 14:40:09 -070094
Yabin Cui76615da2015-03-17 14:22:09 -070095enum {
96 PTHREAD_RWLOCK_PREFER_READER_NP = 0,
97 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP = 1,
98};
99
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800100typedef int pthread_key_t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800101
Yabin Cui80e6d6d2015-01-23 18:21:46 -0800102typedef int pthread_once_t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103
Calin Juravleb1cacd42014-04-23 17:35:04 +0100104#define PTHREAD_ONCE_INIT 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105
Yabin Cuie7c2fff2015-11-05 22:06:09 -0800106typedef struct {
107#if defined(__LP64__)
108 int64_t __private[4];
109#else
110 int32_t __private[8];
111#endif
112} pthread_barrier_t;
113
114typedef int pthread_barrierattr_t;
115
116#define PTHREAD_BARRIER_SERIAL_THREAD -1
117
Yabin Cuife3a83a2015-11-17 16:03:18 -0800118typedef struct {
119#if defined(__LP64__)
120 int64_t __private;
121#else
122 int32_t __private[2];
123#endif
124} pthread_spinlock_t;
125
Christopher Ferrisea5248e2014-05-14 17:58:29 -0700126#if defined(__LP64__)
127#define PTHREAD_STACK_MIN (4 * PAGE_SIZE)
128#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129#define PTHREAD_STACK_MIN (2 * PAGE_SIZE)
Christopher Ferrisea5248e2014-05-14 17:58:29 -0700130#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131
132#define PTHREAD_CREATE_DETACHED 0x00000001
133#define PTHREAD_CREATE_JOINABLE 0x00000000
134
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135#define PTHREAD_PROCESS_PRIVATE 0
136#define PTHREAD_PROCESS_SHARED 1
137
138#define PTHREAD_SCOPE_SYSTEM 0
139#define PTHREAD_SCOPE_PROCESS 1
140
Josh Gao46b44162016-05-27 11:14:16 -0700141int pthread_atfork(void (*)(void), void (*)(void), void (*)(void)) __INTRODUCED_IN(12);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142
Dan Albertbaa2a972015-08-13 16:58:50 -0700143int pthread_attr_destroy(pthread_attr_t* _Nonnull);
144int pthread_attr_getdetachstate(const pthread_attr_t* _Nonnull, int* _Nonnull);
145int pthread_attr_getguardsize(const pthread_attr_t* _Nonnull, size_t* _Nonnull);
146int pthread_attr_getschedparam(const pthread_attr_t* _Nonnull, struct sched_param* _Nonnull);
147int pthread_attr_getschedpolicy(const pthread_attr_t* _Nonnull, int* _Nonnull);
148int pthread_attr_getscope(const pthread_attr_t* _Nonnull, int* _Nonnull);
149int pthread_attr_getstack(const pthread_attr_t* _Nonnull, void** _Nonnull, size_t* _Nonnull);
150int pthread_attr_getstacksize(const pthread_attr_t* _Nonnull, size_t* _Nonnull);
151int pthread_attr_init(pthread_attr_t* _Nonnull);
152int pthread_attr_setdetachstate(pthread_attr_t* _Nonnull, int);
153int pthread_attr_setguardsize(pthread_attr_t* _Nonnull, size_t);
154int pthread_attr_setschedparam(pthread_attr_t* _Nonnull, const struct sched_param* _Nonnull);
155int pthread_attr_setschedpolicy(pthread_attr_t* _Nonnull, int);
156int pthread_attr_setscope(pthread_attr_t* _Nonnull, int);
157int pthread_attr_setstack(pthread_attr_t* _Nonnull, void*, size_t);
158int pthread_attr_setstacksize(pthread_attr_t* _Nonnull, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159
Dan Albertbaa2a972015-08-13 16:58:50 -0700160int pthread_condattr_destroy(pthread_condattr_t* _Nonnull);
161int pthread_condattr_getclock(const pthread_condattr_t* _Nonnull, clockid_t* _Nonnull)
Josh Gao14adff12016-04-29 12:00:55 -0700162 __INTRODUCED_IN(21);
Dan Albertbaa2a972015-08-13 16:58:50 -0700163int pthread_condattr_getpshared(const pthread_condattr_t* _Nonnull, int* _Nonnull);
164int pthread_condattr_init(pthread_condattr_t* _Nonnull);
165int pthread_condattr_setclock(pthread_condattr_t* _Nonnull, clockid_t) __INTRODUCED_IN(21);
166int pthread_condattr_setpshared(pthread_condattr_t* _Nonnull, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800167
Dan Albertbaa2a972015-08-13 16:58:50 -0700168int pthread_cond_broadcast(pthread_cond_t* _Nonnull);
169int pthread_cond_destroy(pthread_cond_t* _Nonnull);
170int pthread_cond_init(pthread_cond_t* _Nonnull, const pthread_condattr_t*);
171int pthread_cond_signal(pthread_cond_t* _Nonnull);
172int pthread_cond_timedwait(pthread_cond_t* _Nonnull, pthread_mutex_t* _Nonnull,
173 const struct timespec* _Nonnull);
174int pthread_cond_wait(pthread_cond_t* _Nonnull, pthread_mutex_t* _Nonnull);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175
Dan Albertbaa2a972015-08-13 16:58:50 -0700176int pthread_create(pthread_t* _Nonnull, pthread_attr_t const*,
177 void* (* _Nonnull start_routine)(void*), void*);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700178int pthread_detach(pthread_t);
179void pthread_exit(void*) __noreturn;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800180
Elliott Hughesc3f11402013-10-30 14:40:09 -0700181int pthread_equal(pthread_t, pthread_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800182
Dan Albertbaa2a972015-08-13 16:58:50 -0700183int pthread_getattr_np(pthread_t, pthread_attr_t* _Nonnull);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800184
Dan Albertbaa2a972015-08-13 16:58:50 -0700185int pthread_getcpuclockid(pthread_t, clockid_t* _Nonnull);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186
Dan Albertbaa2a972015-08-13 16:58:50 -0700187int pthread_getschedparam(pthread_t, int* _Nonnull, struct sched_param* _Nonnull);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188
Elliott Hughesc3f11402013-10-30 14:40:09 -0700189void* pthread_getspecific(pthread_key_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800190
Josh Gao14adff12016-04-29 12:00:55 -0700191pid_t pthread_gettid_np(pthread_t) __INTRODUCED_IN(21);
Elliott Hughesa4831cb2014-09-11 16:11:43 -0700192
Elliott Hughesc3f11402013-10-30 14:40:09 -0700193int pthread_join(pthread_t, void**);
194
Dan Albertbaa2a972015-08-13 16:58:50 -0700195int pthread_key_create(pthread_key_t* _Nonnull, void (*)(void*));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700196int pthread_key_delete(pthread_key_t);
197
Dan Albertbaa2a972015-08-13 16:58:50 -0700198int pthread_mutexattr_destroy(pthread_mutexattr_t* _Nonnull);
199int pthread_mutexattr_getpshared(const pthread_mutexattr_t* _Nonnull, int* _Nonnull);
200int pthread_mutexattr_gettype(const pthread_mutexattr_t* _Nonnull, int* _Nonnull);
201int pthread_mutexattr_init(pthread_mutexattr_t* _Nonnull);
202int pthread_mutexattr_setpshared(pthread_mutexattr_t* _Nonnull, int);
203int pthread_mutexattr_settype(pthread_mutexattr_t* _Nonnull, int);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700204
Dan Albertbaa2a972015-08-13 16:58:50 -0700205int pthread_mutex_destroy(pthread_mutex_t* _Nonnull);
206int pthread_mutex_init(pthread_mutex_t* _Nonnull, const pthread_mutexattr_t*);
207int pthread_mutex_lock(pthread_mutex_t* _Nonnull);
208int pthread_mutex_timedlock(pthread_mutex_t* _Nonnull, const struct timespec* _Nonnull)
Josh Gao14adff12016-04-29 12:00:55 -0700209 __INTRODUCED_IN(21);
Dan Albertbaa2a972015-08-13 16:58:50 -0700210int pthread_mutex_trylock(pthread_mutex_t* _Nonnull);
211int pthread_mutex_unlock(pthread_mutex_t* _Nonnull);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700212
Dan Albertbaa2a972015-08-13 16:58:50 -0700213int pthread_once(pthread_once_t* _Nonnull, void (* _Nonnull init_routine)(void));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700214
Dan Albertbaa2a972015-08-13 16:58:50 -0700215int pthread_rwlockattr_init(pthread_rwlockattr_t* _Nonnull);
216int pthread_rwlockattr_destroy(pthread_rwlockattr_t* _Nonnull);
217int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t* _Nonnull, int* _Nonnull);
218int pthread_rwlockattr_setpshared(pthread_rwlockattr_t* _Nonnull, int);
219int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t* _Nonnull, int* _Nonnull)
Josh Gao14adff12016-04-29 12:00:55 -0700220 __INTRODUCED_IN(23);
Dan Albertbaa2a972015-08-13 16:58:50 -0700221int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t* _Nonnull, int) __INTRODUCED_IN(23);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700222
Dan Albertbaa2a972015-08-13 16:58:50 -0700223int pthread_rwlock_destroy(pthread_rwlock_t* _Nonnull);
224int pthread_rwlock_init(pthread_rwlock_t* _Nonnull, const pthread_rwlockattr_t*);
225int pthread_rwlock_rdlock(pthread_rwlock_t* _Nonnull);
226int pthread_rwlock_timedrdlock(pthread_rwlock_t* _Nonnull, const struct timespec* _Nonnull);
227int pthread_rwlock_timedwrlock(pthread_rwlock_t* _Nonnull, const struct timespec* _Nonnull);
228int pthread_rwlock_tryrdlock(pthread_rwlock_t* _Nonnull);
229int pthread_rwlock_trywrlock(pthread_rwlock_t* _Nonnull);
230int pthread_rwlock_unlock(pthread_rwlock_t* _Nonnull);
231int pthread_rwlock_wrlock(pthread_rwlock_t* _Nonnull);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800232
Dan Albertbaa2a972015-08-13 16:58:50 -0700233int pthread_barrierattr_init(pthread_barrierattr_t* _Nonnull attr) __INTRODUCED_IN(24);
234int pthread_barrierattr_destroy(pthread_barrierattr_t* _Nonnull attr) __INTRODUCED_IN(24);
Elliott Hughes02ac0282016-08-11 23:18:13 -0700235int pthread_barrierattr_getpshared(const pthread_barrierattr_t* _Nonnull attr,
236 int* _Nonnull pshared) __INTRODUCED_IN(24);
Dan Albertbaa2a972015-08-13 16:58:50 -0700237int pthread_barrierattr_setpshared(pthread_barrierattr_t* _Nonnull attr, int pshared)
Josh Gao14adff12016-04-29 12:00:55 -0700238 __INTRODUCED_IN(24);
Yabin Cuie7c2fff2015-11-05 22:06:09 -0800239
Dan Albertbaa2a972015-08-13 16:58:50 -0700240int pthread_barrier_init(pthread_barrier_t* _Nonnull, const pthread_barrierattr_t*, unsigned)
Josh Gao14adff12016-04-29 12:00:55 -0700241 __INTRODUCED_IN(24);
Dan Albertbaa2a972015-08-13 16:58:50 -0700242int pthread_barrier_destroy(pthread_barrier_t* _Nonnull) __INTRODUCED_IN(24);
243int pthread_barrier_wait(pthread_barrier_t* _Nonnull) __INTRODUCED_IN(24);
Yabin Cuie7c2fff2015-11-05 22:06:09 -0800244
Dan Albertbaa2a972015-08-13 16:58:50 -0700245int pthread_spin_destroy(pthread_spinlock_t* _Nonnull) __INTRODUCED_IN(24);
246int pthread_spin_init(pthread_spinlock_t* _Nonnull, int) __INTRODUCED_IN(24);
247int pthread_spin_lock(pthread_spinlock_t* _Nonnull) __INTRODUCED_IN(24);
248int pthread_spin_trylock(pthread_spinlock_t* _Nonnull) __INTRODUCED_IN(24);
249int pthread_spin_unlock(pthread_spinlock_t* _Nonnull) __INTRODUCED_IN(24);
Yabin Cuife3a83a2015-11-17 16:03:18 -0800250
Elliott Hughesb27a8402014-06-10 20:47:49 -0700251pthread_t pthread_self(void) __pure2;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800252
Elliott Hughes725b2a92016-03-23 11:20:47 -0700253#if defined(__USE_GNU)
Dan Albertbaa2a972015-08-13 16:58:50 -0700254int pthread_getname_np(pthread_t, char* _Nonnull, size_t) __INTRODUCED_IN_FUTURE;
Elliott Hughes725b2a92016-03-23 11:20:47 -0700255#endif
Elliott Hughescc78b252016-03-23 20:26:35 -0700256/* TODO: this should be __USE_GNU too. */
Dan Albertbaa2a972015-08-13 16:58:50 -0700257int pthread_setname_np(pthread_t, const char* _Nonnull);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800258
Dan Albertbaa2a972015-08-13 16:58:50 -0700259int pthread_setschedparam(pthread_t, int, const struct sched_param* _Nonnull);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800260
Elliott Hughesc3f11402013-10-30 14:40:09 -0700261int pthread_setspecific(pthread_key_t, const void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800262
Elliott Hughesc3f11402013-10-30 14:40:09 -0700263typedef void (*__pthread_cleanup_func_t)(void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800264
265typedef struct __pthread_cleanup_t {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700266 struct __pthread_cleanup_t* __cleanup_prev;
267 __pthread_cleanup_func_t __cleanup_routine;
268 void* __cleanup_arg;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800269} __pthread_cleanup_t;
270
Elliott Hughes5470c182016-07-22 11:36:17 -0700271void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t, void*);
272void __pthread_cleanup_pop(__pthread_cleanup_t*, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800273
274/* Believe or not, the definitions of pthread_cleanup_push and
275 * pthread_cleanup_pop below are correct. Posix states that these
276 * can be implemented as macros that might introduce opening and
277 * closing braces, and that using setjmp/longjmp/return/break/continue
Elliott Hughesc3f11402013-10-30 14:40:09 -0700278 * between them results in undefined behavior.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800279 */
280#define pthread_cleanup_push(routine, arg) \
281 do { \
282 __pthread_cleanup_t __cleanup; \
283 __pthread_cleanup_push( &__cleanup, (routine), (arg) ); \
284
285#define pthread_cleanup_pop(execute) \
286 __pthread_cleanup_pop( &__cleanup, (execute)); \
Elliott Hughesc3f11402013-10-30 14:40:09 -0700287 } while (0); \
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800288
Elliott Hughes5470c182016-07-22 11:36:17 -0700289#if defined(__clang__)
290#pragma clang diagnostic pop
291#endif
292
Elliott Hughesc3f11402013-10-30 14:40:09 -0700293__END_DECLS
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800294
David 'Digit' Turnerbd8d9872010-09-26 23:00:36 +0200295#endif /* _PTHREAD_H_ */