blob: 24dba1bcfd9b0331495c0501946b3a8845f15bd2 [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>
Dan Albert75ef63d2014-11-21 00:18:07 -080033#include <machine/pthread_types.h>
34#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
Christopher Ferrisea5248e2014-05-14 17:58:29 -070039#if defined(__LP64__)
Calin Juravleb1cacd42014-04-23 17:35:04 +010040 #define __RESERVED_INITIALIZER , {0}
41#else
42 #define __RESERVED_INITIALIZER
43#endif
44
Elliott Hughesc3f11402013-10-30 14:40:09 -070045typedef struct {
46 int volatile value;
Calin Juravleb1cacd42014-04-23 17:35:04 +010047#ifdef __LP64__
48 char __reserved[36];
49#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050} pthread_mutex_t;
51
David 'Digit' Turner9831ad32011-08-29 21:43:46 +020052#define __PTHREAD_MUTEX_INIT_VALUE 0
53#define __PTHREAD_RECURSIVE_MUTEX_INIT_VALUE 0x4000
54#define __PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE 0x8000
55
Calin Juravleb1cacd42014-04-23 17:35:04 +010056#define PTHREAD_MUTEX_INITIALIZER {__PTHREAD_MUTEX_INIT_VALUE __RESERVED_INITIALIZER}
57#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER {__PTHREAD_RECURSIVE_MUTEX_INIT_VALUE __RESERVED_INITIALIZER}
58#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER {__PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE __RESERVED_INITIALIZER}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059
60enum {
61 PTHREAD_MUTEX_NORMAL = 0,
62 PTHREAD_MUTEX_RECURSIVE = 1,
63 PTHREAD_MUTEX_ERRORCHECK = 2,
64
65 PTHREAD_MUTEX_ERRORCHECK_NP = PTHREAD_MUTEX_ERRORCHECK,
66 PTHREAD_MUTEX_RECURSIVE_NP = PTHREAD_MUTEX_RECURSIVE,
67
68 PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
69};
70
Elliott Hughesc3f11402013-10-30 14:40:09 -070071typedef struct {
72 int volatile value;
Calin Juravleb1cacd42014-04-23 17:35:04 +010073#ifdef __LP64__
74 char __reserved[44];
75#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080076} pthread_cond_t;
77
Calin Juravleb1cacd42014-04-23 17:35:04 +010078#define PTHREAD_COND_INITIALIZER {0 __RESERVED_INITIALIZER}
Elliott Hughesc3f11402013-10-30 14:40:09 -070079
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080typedef long pthread_mutexattr_t;
81typedef long pthread_condattr_t;
82
Calin Juravle7c435f22014-05-06 13:39:47 +010083typedef long pthread_rwlockattr_t;
Elliott Hughesc3f11402013-10-30 14:40:09 -070084
85typedef struct {
Calin Juravle92687e42014-05-22 19:21:22 +010086#if !defined(__LP64__)
Calin Juravle76f352e2014-05-19 13:41:10 +010087 pthread_mutex_t __unused_lock;
88 pthread_cond_t __unused_cond;
Calin Juravle92687e42014-05-22 19:21:22 +010089#endif
Calin Juravle76f352e2014-05-19 13:41:10 +010090 volatile int32_t state; // 0=unlock, -1=writer lock, +n=reader lock
Calin Juravle92687e42014-05-22 19:21:22 +010091 volatile int32_t writer_thread_id;
92 volatile int32_t pending_readers;
93 volatile int32_t pending_writers;
Calin Juravle76f352e2014-05-19 13:41:10 +010094 int32_t attr;
Calin Juravle92687e42014-05-22 19:21:22 +010095#ifdef __LP64__
96 char __reserved[36];
97#else
98 char __reserved[12];
99#endif
100
Elliott Hughesc3f11402013-10-30 14:40:09 -0700101} pthread_rwlock_t;
102
Calin Juravle92687e42014-05-22 19:21:22 +0100103#ifdef __LP64__
104 #define PTHREAD_RWLOCK_INITIALIZER { 0, 0, 0, 0, 0, { 0 } }
105#else
106 #define PTHREAD_RWLOCK_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0, 0, 0, 0, { 0 } }
107#endif
Elliott Hughesc3f11402013-10-30 14:40:09 -0700108
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109typedef int pthread_key_t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110
Elliott Hughesc3f11402013-10-30 14:40:09 -0700111typedef volatile int pthread_once_t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112
Calin Juravleb1cacd42014-04-23 17:35:04 +0100113#define PTHREAD_ONCE_INIT 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114
Christopher Ferrisea5248e2014-05-14 17:58:29 -0700115#if defined(__LP64__)
116#define PTHREAD_STACK_MIN (4 * PAGE_SIZE)
117#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118#define PTHREAD_STACK_MIN (2 * PAGE_SIZE)
Christopher Ferrisea5248e2014-05-14 17:58:29 -0700119#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120
121#define PTHREAD_CREATE_DETACHED 0x00000001
122#define PTHREAD_CREATE_JOINABLE 0x00000000
123
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124#define PTHREAD_PROCESS_PRIVATE 0
125#define PTHREAD_PROCESS_SHARED 1
126
127#define PTHREAD_SCOPE_SYSTEM 0
128#define PTHREAD_SCOPE_PROCESS 1
129
Elliott Hughesc3f11402013-10-30 14:40:09 -0700130__BEGIN_DECLS
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131
Elliott Hughesc3f11402013-10-30 14:40:09 -0700132int pthread_atfork(void (*)(void), void (*)(void), void(*)(void));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800133
Elliott Hughesc3f11402013-10-30 14:40:09 -0700134int pthread_attr_destroy(pthread_attr_t*) __nonnull((1));
135int pthread_attr_getdetachstate(const pthread_attr_t*, int*) __nonnull((1, 2));
136int pthread_attr_getguardsize(const pthread_attr_t*, size_t*) __nonnull((1, 2));
137int pthread_attr_getschedparam(const pthread_attr_t*, struct sched_param*) __nonnull((1, 2));
138int pthread_attr_getschedpolicy(const pthread_attr_t*, int*) __nonnull((1, 2));
139int pthread_attr_getscope(const pthread_attr_t*, int*) __nonnull((1, 2));
140int pthread_attr_getstack(const pthread_attr_t*, void**, size_t*) __nonnull((1, 2, 3));
141int pthread_attr_getstacksize(const pthread_attr_t*, size_t*) __nonnull((1, 2));
142int pthread_attr_init(pthread_attr_t*) __nonnull((1));
143int pthread_attr_setdetachstate(pthread_attr_t*, int) __nonnull((1));
144int pthread_attr_setguardsize(pthread_attr_t*, size_t) __nonnull((1));
145int pthread_attr_setschedparam(pthread_attr_t*, const struct sched_param*) __nonnull((1, 2));
146int pthread_attr_setschedpolicy(pthread_attr_t*, int) __nonnull((1));
147int pthread_attr_setscope(pthread_attr_t*, int) __nonnull((1));
148int pthread_attr_setstack(pthread_attr_t*, void*, size_t) __nonnull((1));
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000149int pthread_attr_setstacksize(pthread_attr_t*, size_t stack_size) __nonnull((1));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800150
Elliott Hughesc3f11402013-10-30 14:40:09 -0700151int pthread_condattr_destroy(pthread_condattr_t*) __nonnull((1));
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000152int pthread_condattr_getclock(const pthread_condattr_t*, clockid_t*) __nonnull((1, 2));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700153int pthread_condattr_getpshared(const pthread_condattr_t*, int*) __nonnull((1, 2));
154int pthread_condattr_init(pthread_condattr_t*) __nonnull((1));
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000155int pthread_condattr_setclock(pthread_condattr_t*, clockid_t) __nonnull((1));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700156int pthread_condattr_setpshared(pthread_condattr_t*, int) __nonnull((1));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157
Elliott Hughesc3f11402013-10-30 14:40:09 -0700158int pthread_cond_broadcast(pthread_cond_t*) __nonnull((1));
159int pthread_cond_destroy(pthread_cond_t*) __nonnull((1));
160int pthread_cond_init(pthread_cond_t*, const pthread_condattr_t*) __nonnull((1));
161int pthread_cond_signal(pthread_cond_t*) __nonnull((1));
162int pthread_cond_timedwait(pthread_cond_t*, pthread_mutex_t*, const struct timespec*) __nonnull((1, 2, 3));
163int pthread_cond_wait(pthread_cond_t*, pthread_mutex_t*) __nonnull((1, 2));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800164
Elliott Hughesc3f11402013-10-30 14:40:09 -0700165int pthread_create(pthread_t*, pthread_attr_t const*, void *(*)(void*), void*) __nonnull((1, 3));
166int pthread_detach(pthread_t);
167void pthread_exit(void*) __noreturn;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800168
Elliott Hughesc3f11402013-10-30 14:40:09 -0700169int pthread_equal(pthread_t, pthread_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800170
Elliott Hughesc3f11402013-10-30 14:40:09 -0700171int pthread_getattr_np(pthread_t, pthread_attr_t*) __nonnull((2));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800172
Elliott Hughesc3f11402013-10-30 14:40:09 -0700173int pthread_getcpuclockid(pthread_t, clockid_t*) __nonnull((2));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800174
Elliott Hughesc3f11402013-10-30 14:40:09 -0700175int pthread_getschedparam(pthread_t, int*, struct sched_param*) __nonnull((2, 3));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176
Elliott Hughesc3f11402013-10-30 14:40:09 -0700177void* pthread_getspecific(pthread_key_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800178
Elliott Hughesa4831cb2014-09-11 16:11:43 -0700179pid_t pthread_gettid_np(pthread_t);
180
Elliott Hughesc3f11402013-10-30 14:40:09 -0700181int pthread_join(pthread_t, void**);
182
183int pthread_key_create(pthread_key_t*, void (*)(void*)) __nonnull((1));
184int pthread_key_delete(pthread_key_t);
185
Elliott Hughesc3f11402013-10-30 14:40:09 -0700186int pthread_mutexattr_destroy(pthread_mutexattr_t*) __nonnull((1));
187int pthread_mutexattr_getpshared(const pthread_mutexattr_t*, int*) __nonnull((1, 2));
188int pthread_mutexattr_gettype(const pthread_mutexattr_t*, int*) __nonnull((1, 2));
189int pthread_mutexattr_init(pthread_mutexattr_t*) __nonnull((1));
190int pthread_mutexattr_setpshared(pthread_mutexattr_t*, int) __nonnull((1));
191int pthread_mutexattr_settype(pthread_mutexattr_t*, int) __nonnull((1));
192
193int pthread_mutex_destroy(pthread_mutex_t*) __nonnull((1));
194int pthread_mutex_init(pthread_mutex_t*, const pthread_mutexattr_t*) __nonnull((1));
Elliott Hughes3d773272014-10-07 23:31:06 +0000195int pthread_mutex_lock(pthread_mutex_t*) __nonnull((1));
Elliott Hughes0e714a52014-03-03 16:42:47 -0800196int pthread_mutex_timedlock(pthread_mutex_t*, const struct timespec*) __nonnull((1, 2));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700197int pthread_mutex_trylock(pthread_mutex_t*) __nonnull((1));
Elliott Hughes3d773272014-10-07 23:31:06 +0000198int pthread_mutex_unlock(pthread_mutex_t*) __nonnull((1));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700199
200int pthread_once(pthread_once_t*, void (*)(void)) __nonnull((1, 2));
201
202int pthread_rwlockattr_destroy(pthread_rwlockattr_t*) __nonnull((1));
203int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t*, int*) __nonnull((1, 2));
204int pthread_rwlockattr_init(pthread_rwlockattr_t*) __nonnull((1));
205int pthread_rwlockattr_setpshared(pthread_rwlockattr_t*, int) __nonnull((1));
206
207int pthread_rwlock_destroy(pthread_rwlock_t*) __nonnull((1));
208int pthread_rwlock_init(pthread_rwlock_t*, const pthread_rwlockattr_t*) __nonnull((1));
209int pthread_rwlock_rdlock(pthread_rwlock_t*) __nonnull((1));
210int pthread_rwlock_timedrdlock(pthread_rwlock_t*, const struct timespec*) __nonnull((1, 2));
211int pthread_rwlock_timedwrlock(pthread_rwlock_t*, const struct timespec*) __nonnull((1, 2));
212int pthread_rwlock_tryrdlock(pthread_rwlock_t*) __nonnull((1));
213int pthread_rwlock_trywrlock(pthread_rwlock_t*) __nonnull((1));
214int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) __nonnull((1));
215int pthread_rwlock_wrlock(pthread_rwlock_t*) __nonnull((1));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800216
Elliott Hughesb27a8402014-06-10 20:47:49 -0700217pthread_t pthread_self(void) __pure2;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800218
Elliott Hughesc3f11402013-10-30 14:40:09 -0700219int pthread_setname_np(pthread_t, const char*) __nonnull((2));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800220
Elliott Hughesc3f11402013-10-30 14:40:09 -0700221int pthread_setschedparam(pthread_t, int, const struct sched_param*) __nonnull((3));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800222
Elliott Hughesc3f11402013-10-30 14:40:09 -0700223int pthread_setspecific(pthread_key_t, const void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800224
Elliott Hughesc3f11402013-10-30 14:40:09 -0700225typedef void (*__pthread_cleanup_func_t)(void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226
227typedef struct __pthread_cleanup_t {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700228 struct __pthread_cleanup_t* __cleanup_prev;
229 __pthread_cleanup_func_t __cleanup_routine;
230 void* __cleanup_arg;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800231} __pthread_cleanup_t;
232
Elliott Hughesc3f11402013-10-30 14:40:09 -0700233extern void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t, void*);
234extern void __pthread_cleanup_pop(__pthread_cleanup_t*, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800235
236/* Believe or not, the definitions of pthread_cleanup_push and
237 * pthread_cleanup_pop below are correct. Posix states that these
238 * can be implemented as macros that might introduce opening and
239 * closing braces, and that using setjmp/longjmp/return/break/continue
Elliott Hughesc3f11402013-10-30 14:40:09 -0700240 * between them results in undefined behavior.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800241 */
242#define pthread_cleanup_push(routine, arg) \
243 do { \
244 __pthread_cleanup_t __cleanup; \
245 __pthread_cleanup_push( &__cleanup, (routine), (arg) ); \
246
247#define pthread_cleanup_pop(execute) \
248 __pthread_cleanup_pop( &__cleanup, (execute)); \
Elliott Hughesc3f11402013-10-30 14:40:09 -0700249 } while (0); \
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800250
Elliott Hughesc3f11402013-10-30 14:40:09 -0700251
252#if !defined(__LP64__)
253
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000254// Bionic additions that are deprecated even in the 32-bit ABI.
255//
256// TODO: Remove them once chromium_org / NFC have switched over.
Elliott Hughesc3f11402013-10-30 14:40:09 -0700257int pthread_cond_timedwait_monotonic_np(pthread_cond_t*, pthread_mutex_t*, const struct timespec*);
258int pthread_cond_timedwait_monotonic(pthread_cond_t*, pthread_mutex_t*, const struct timespec*);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700259
Elliott Hughes0e714a52014-03-03 16:42:47 -0800260int pthread_cond_timedwait_relative_np(pthread_cond_t*, pthread_mutex_t*, const struct timespec*) /* TODO: __attribute__((deprecated("use pthread_cond_timedwait instead")))*/;
261#define HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE 1 /* TODO: stop defining this to push LP32 off this API sooner. */
262int pthread_cond_timeout_np(pthread_cond_t*, pthread_mutex_t*, unsigned) /* TODO: __attribute__((deprecated("use pthread_cond_timedwait instead")))*/;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700263
Elliott Hughes0e714a52014-03-03 16:42:47 -0800264int pthread_mutex_lock_timeout_np(pthread_mutex_t*, unsigned) __attribute__((deprecated("use pthread_mutex_timedlock instead")));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700265
266#endif /* !defined(__LP64__) */
267
268__END_DECLS
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800269
David 'Digit' Turnerbd8d9872010-09-26 23:00:36 +0200270#endif /* _PTHREAD_H_ */