blob: 7b8de81cdac44648128f112f7c3505aceadcf077 [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
32#include <time.h>
33#include <signal.h>
34#include <sched.h>
35#include <limits.h>
36#include <sys/types.h>
37
Calin Juravleb1cacd42014-04-23 17:35:04 +010038#ifdef __LP64__
39 #define __RESERVED_INITIALIZER , {0}
40#else
41 #define __RESERVED_INITIALIZER
42#endif
43
Elliott Hughesc3f11402013-10-30 14:40:09 -070044typedef struct {
45 int volatile value;
Calin Juravleb1cacd42014-04-23 17:35:04 +010046#ifdef __LP64__
47 char __reserved[36];
48#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080049} pthread_mutex_t;
50
David 'Digit' Turner9831ad32011-08-29 21:43:46 +020051#define __PTHREAD_MUTEX_INIT_VALUE 0
52#define __PTHREAD_RECURSIVE_MUTEX_INIT_VALUE 0x4000
53#define __PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE 0x8000
54
Calin Juravleb1cacd42014-04-23 17:35:04 +010055#define PTHREAD_MUTEX_INITIALIZER {__PTHREAD_MUTEX_INIT_VALUE __RESERVED_INITIALIZER}
56#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER {__PTHREAD_RECURSIVE_MUTEX_INIT_VALUE __RESERVED_INITIALIZER}
57#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER {__PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE __RESERVED_INITIALIZER}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080058
59enum {
60 PTHREAD_MUTEX_NORMAL = 0,
61 PTHREAD_MUTEX_RECURSIVE = 1,
62 PTHREAD_MUTEX_ERRORCHECK = 2,
63
64 PTHREAD_MUTEX_ERRORCHECK_NP = PTHREAD_MUTEX_ERRORCHECK,
65 PTHREAD_MUTEX_RECURSIVE_NP = PTHREAD_MUTEX_RECURSIVE,
66
67 PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
68};
69
Elliott Hughesc3f11402013-10-30 14:40:09 -070070typedef struct {
71 int volatile value;
Calin Juravleb1cacd42014-04-23 17:35:04 +010072#ifdef __LP64__
73 char __reserved[44];
74#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075} pthread_cond_t;
76
Calin Juravleb1cacd42014-04-23 17:35:04 +010077#define PTHREAD_COND_INITIALIZER {0 __RESERVED_INITIALIZER}
Elliott Hughesc3f11402013-10-30 14:40:09 -070078
79typedef struct {
80 uint32_t flags;
81 void* stack_base;
82 size_t stack_size;
83 size_t guard_size;
84 int32_t sched_policy;
85 int32_t sched_priority;
Calin Juravleb1cacd42014-04-23 17:35:04 +010086#ifdef __LP64__
87 char __reserved[16];
88#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080089} pthread_attr_t;
90
91typedef long pthread_mutexattr_t;
92typedef long pthread_condattr_t;
93
Calin Juravle7c435f22014-05-06 13:39:47 +010094typedef long pthread_rwlockattr_t;
Elliott Hughesc3f11402013-10-30 14:40:09 -070095
96typedef struct {
Calin Juravleb1cacd42014-04-23 17:35:04 +010097 pthread_mutex_t lock;
98 pthread_cond_t cond;
99 int numLocks;
100 int writerThreadId;
101 int pendingReaders;
102 int pendingWriters;
103 void* __reserved[4];
Elliott Hughesc3f11402013-10-30 14:40:09 -0700104} pthread_rwlock_t;
105
106#define PTHREAD_RWLOCK_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0, 0, 0, { NULL, NULL, NULL, NULL } }
107
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108typedef int pthread_key_t;
109typedef long pthread_t;
110
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
115#define PTHREAD_STACK_MIN (2 * PAGE_SIZE)
116
117#define PTHREAD_CREATE_DETACHED 0x00000001
118#define PTHREAD_CREATE_JOINABLE 0x00000000
119
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120#define PTHREAD_PROCESS_PRIVATE 0
121#define PTHREAD_PROCESS_SHARED 1
122
123#define PTHREAD_SCOPE_SYSTEM 0
124#define PTHREAD_SCOPE_PROCESS 1
125
Elliott Hughesc3f11402013-10-30 14:40:09 -0700126__BEGIN_DECLS
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127
Elliott Hughesc3f11402013-10-30 14:40:09 -0700128int pthread_atfork(void (*)(void), void (*)(void), void(*)(void));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129
Elliott Hughesc3f11402013-10-30 14:40:09 -0700130int pthread_attr_destroy(pthread_attr_t*) __nonnull((1));
131int pthread_attr_getdetachstate(const pthread_attr_t*, int*) __nonnull((1, 2));
132int pthread_attr_getguardsize(const pthread_attr_t*, size_t*) __nonnull((1, 2));
133int pthread_attr_getschedparam(const pthread_attr_t*, struct sched_param*) __nonnull((1, 2));
134int pthread_attr_getschedpolicy(const pthread_attr_t*, int*) __nonnull((1, 2));
135int pthread_attr_getscope(const pthread_attr_t*, int*) __nonnull((1, 2));
136int pthread_attr_getstack(const pthread_attr_t*, void**, size_t*) __nonnull((1, 2, 3));
137int pthread_attr_getstacksize(const pthread_attr_t*, size_t*) __nonnull((1, 2));
138int pthread_attr_init(pthread_attr_t*) __nonnull((1));
139int pthread_attr_setdetachstate(pthread_attr_t*, int) __nonnull((1));
140int pthread_attr_setguardsize(pthread_attr_t*, size_t) __nonnull((1));
141int pthread_attr_setschedparam(pthread_attr_t*, const struct sched_param*) __nonnull((1, 2));
142int pthread_attr_setschedpolicy(pthread_attr_t*, int) __nonnull((1));
143int pthread_attr_setscope(pthread_attr_t*, int) __nonnull((1));
144int pthread_attr_setstack(pthread_attr_t*, void*, size_t) __nonnull((1));
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000145int pthread_attr_setstacksize(pthread_attr_t*, size_t stack_size) __nonnull((1));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800146
Elliott Hughesc3f11402013-10-30 14:40:09 -0700147int pthread_condattr_destroy(pthread_condattr_t*) __nonnull((1));
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000148int pthread_condattr_getclock(const pthread_condattr_t*, clockid_t*) __nonnull((1, 2));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700149int pthread_condattr_getpshared(const pthread_condattr_t*, int*) __nonnull((1, 2));
150int pthread_condattr_init(pthread_condattr_t*) __nonnull((1));
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000151int pthread_condattr_setclock(pthread_condattr_t*, clockid_t) __nonnull((1));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700152int pthread_condattr_setpshared(pthread_condattr_t*, int) __nonnull((1));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800153
Elliott Hughesc3f11402013-10-30 14:40:09 -0700154int pthread_cond_broadcast(pthread_cond_t*) __nonnull((1));
155int pthread_cond_destroy(pthread_cond_t*) __nonnull((1));
156int pthread_cond_init(pthread_cond_t*, const pthread_condattr_t*) __nonnull((1));
157int pthread_cond_signal(pthread_cond_t*) __nonnull((1));
158int pthread_cond_timedwait(pthread_cond_t*, pthread_mutex_t*, const struct timespec*) __nonnull((1, 2, 3));
159int pthread_cond_wait(pthread_cond_t*, pthread_mutex_t*) __nonnull((1, 2));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800160
Elliott Hughesc3f11402013-10-30 14:40:09 -0700161int pthread_create(pthread_t*, pthread_attr_t const*, void *(*)(void*), void*) __nonnull((1, 3));
162int pthread_detach(pthread_t);
163void pthread_exit(void*) __noreturn;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800164
Elliott Hughesc3f11402013-10-30 14:40:09 -0700165int pthread_equal(pthread_t, pthread_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800166
Elliott Hughesc3f11402013-10-30 14:40:09 -0700167int pthread_getattr_np(pthread_t, pthread_attr_t*) __nonnull((2));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800168
Elliott Hughesc3f11402013-10-30 14:40:09 -0700169int pthread_getcpuclockid(pthread_t, clockid_t*) __nonnull((2));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800170
Elliott Hughesc3f11402013-10-30 14:40:09 -0700171int pthread_getschedparam(pthread_t, int*, struct sched_param*) __nonnull((2, 3));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800172
Elliott Hughesc3f11402013-10-30 14:40:09 -0700173void* pthread_getspecific(pthread_key_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800174
Elliott Hughesc3f11402013-10-30 14:40:09 -0700175int pthread_join(pthread_t, void**);
176
177int pthread_key_create(pthread_key_t*, void (*)(void*)) __nonnull((1));
178int pthread_key_delete(pthread_key_t);
179
180int pthread_kill(pthread_t, int);
181
182int pthread_mutexattr_destroy(pthread_mutexattr_t*) __nonnull((1));
183int pthread_mutexattr_getpshared(const pthread_mutexattr_t*, int*) __nonnull((1, 2));
184int pthread_mutexattr_gettype(const pthread_mutexattr_t*, int*) __nonnull((1, 2));
185int pthread_mutexattr_init(pthread_mutexattr_t*) __nonnull((1));
186int pthread_mutexattr_setpshared(pthread_mutexattr_t*, int) __nonnull((1));
187int pthread_mutexattr_settype(pthread_mutexattr_t*, int) __nonnull((1));
188
189int pthread_mutex_destroy(pthread_mutex_t*) __nonnull((1));
190int pthread_mutex_init(pthread_mutex_t*, const pthread_mutexattr_t*) __nonnull((1));
191int pthread_mutex_lock(pthread_mutex_t*) __nonnull((1));
Elliott Hughes0e714a52014-03-03 16:42:47 -0800192int pthread_mutex_timedlock(pthread_mutex_t*, const struct timespec*) __nonnull((1, 2));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700193int pthread_mutex_trylock(pthread_mutex_t*) __nonnull((1));
194int pthread_mutex_unlock(pthread_mutex_t*) __nonnull((1));
195
196int pthread_once(pthread_once_t*, void (*)(void)) __nonnull((1, 2));
197
198int pthread_rwlockattr_destroy(pthread_rwlockattr_t*) __nonnull((1));
199int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t*, int*) __nonnull((1, 2));
200int pthread_rwlockattr_init(pthread_rwlockattr_t*) __nonnull((1));
201int pthread_rwlockattr_setpshared(pthread_rwlockattr_t*, int) __nonnull((1));
202
203int pthread_rwlock_destroy(pthread_rwlock_t*) __nonnull((1));
204int pthread_rwlock_init(pthread_rwlock_t*, const pthread_rwlockattr_t*) __nonnull((1));
205int pthread_rwlock_rdlock(pthread_rwlock_t*) __nonnull((1));
206int pthread_rwlock_timedrdlock(pthread_rwlock_t*, const struct timespec*) __nonnull((1, 2));
207int pthread_rwlock_timedwrlock(pthread_rwlock_t*, const struct timespec*) __nonnull((1, 2));
208int pthread_rwlock_tryrdlock(pthread_rwlock_t*) __nonnull((1));
209int pthread_rwlock_trywrlock(pthread_rwlock_t*) __nonnull((1));
210int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) __nonnull((1));
211int pthread_rwlock_wrlock(pthread_rwlock_t*) __nonnull((1));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800212
213pthread_t pthread_self(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800214
Elliott Hughesc3f11402013-10-30 14:40:09 -0700215int pthread_setname_np(pthread_t, const char*) __nonnull((2));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800216
Elliott Hughesc3f11402013-10-30 14:40:09 -0700217int pthread_setschedparam(pthread_t, int, const struct sched_param*) __nonnull((3));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800218
Elliott Hughesc3f11402013-10-30 14:40:09 -0700219int pthread_setspecific(pthread_key_t, const void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800220
Elliott Hughesc3f11402013-10-30 14:40:09 -0700221int pthread_sigmask(int, const sigset_t*, sigset_t*);
David 'Digit' Turneree7b0772010-03-18 14:07:42 -0700222
Elliott Hughesc3f11402013-10-30 14:40:09 -0700223typedef void (*__pthread_cleanup_func_t)(void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800224
225typedef struct __pthread_cleanup_t {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700226 struct __pthread_cleanup_t* __cleanup_prev;
227 __pthread_cleanup_func_t __cleanup_routine;
228 void* __cleanup_arg;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800229} __pthread_cleanup_t;
230
Elliott Hughesc3f11402013-10-30 14:40:09 -0700231extern void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t, void*);
232extern void __pthread_cleanup_pop(__pthread_cleanup_t*, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800233
234/* Believe or not, the definitions of pthread_cleanup_push and
235 * pthread_cleanup_pop below are correct. Posix states that these
236 * can be implemented as macros that might introduce opening and
237 * closing braces, and that using setjmp/longjmp/return/break/continue
Elliott Hughesc3f11402013-10-30 14:40:09 -0700238 * between them results in undefined behavior.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800239 */
240#define pthread_cleanup_push(routine, arg) \
241 do { \
242 __pthread_cleanup_t __cleanup; \
243 __pthread_cleanup_push( &__cleanup, (routine), (arg) ); \
244
245#define pthread_cleanup_pop(execute) \
246 __pthread_cleanup_pop( &__cleanup, (execute)); \
Elliott Hughesc3f11402013-10-30 14:40:09 -0700247 } while (0); \
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800248
Elliott Hughesc3f11402013-10-30 14:40:09 -0700249
250#if !defined(__LP64__)
251
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000252// Bionic additions that are deprecated even in the 32-bit ABI.
253//
254// TODO: Remove them once chromium_org / NFC have switched over.
Elliott Hughesc3f11402013-10-30 14:40:09 -0700255int pthread_cond_timedwait_monotonic_np(pthread_cond_t*, pthread_mutex_t*, const struct timespec*);
256int pthread_cond_timedwait_monotonic(pthread_cond_t*, pthread_mutex_t*, const struct timespec*);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700257
Elliott Hughes0e714a52014-03-03 16:42:47 -0800258int pthread_cond_timedwait_relative_np(pthread_cond_t*, pthread_mutex_t*, const struct timespec*) /* TODO: __attribute__((deprecated("use pthread_cond_timedwait instead")))*/;
259#define HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE 1 /* TODO: stop defining this to push LP32 off this API sooner. */
260int 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 -0700261
Elliott Hughes0e714a52014-03-03 16:42:47 -0800262int pthread_mutex_lock_timeout_np(pthread_mutex_t*, unsigned) __attribute__((deprecated("use pthread_mutex_timedlock instead")));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700263
264#endif /* !defined(__LP64__) */
265
266__END_DECLS
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800267
David 'Digit' Turnerbd8d9872010-09-26 23:00:36 +0200268#endif /* _PTHREAD_H_ */