blob: 234a43d517798678ff1eba7c4ec0f07dcfee27eb [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 {
Yabin Cui86fc96f2015-01-29 21:50:48 -080046 int 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
Elliott Hughes212e0e32014-12-01 16:43:51 -080056#define PTHREAD_MUTEX_INITIALIZER {__PTHREAD_MUTEX_INIT_VALUE __RESERVED_INITIALIZER}
57#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP {__PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE __RESERVED_INITIALIZER}
58#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP {__PTHREAD_RECURSIVE_MUTEX_INIT_VALUE __RESERVED_INITIALIZER}
59
60/* TODO: remove this namespace pollution. */
61#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
62#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063
64enum {
65 PTHREAD_MUTEX_NORMAL = 0,
66 PTHREAD_MUTEX_RECURSIVE = 1,
67 PTHREAD_MUTEX_ERRORCHECK = 2,
68
69 PTHREAD_MUTEX_ERRORCHECK_NP = PTHREAD_MUTEX_ERRORCHECK,
70 PTHREAD_MUTEX_RECURSIVE_NP = PTHREAD_MUTEX_RECURSIVE,
71
72 PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
73};
74
Elliott Hughesc3f11402013-10-30 14:40:09 -070075typedef struct {
Yabin Cui32651b82015-03-13 20:30:00 -070076#if defined(__LP64__)
77 char __private[48];
78#else
79 char __private[4];
Calin Juravleb1cacd42014-04-23 17:35:04 +010080#endif
Yabin Cuib5845722015-03-16 22:46:42 -070081} pthread_cond_t __attribute__((aligned(4)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080082
Yabin Cui32651b82015-03-13 20:30:00 -070083#define PTHREAD_COND_INITIALIZER { { 0 } }
Elliott Hughesc3f11402013-10-30 14:40:09 -070084
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080085typedef long pthread_mutexattr_t;
86typedef long pthread_condattr_t;
87
Calin Juravle7c435f22014-05-06 13:39:47 +010088typedef long pthread_rwlockattr_t;
Elliott Hughesc3f11402013-10-30 14:40:09 -070089
90typedef struct {
Yabin Cui2fabea42015-03-13 14:22:05 -070091#if defined(__LP64__)
92 char __private[56];
Calin Juravle92687e42014-05-22 19:21:22 +010093#else
Yabin Cui2fabea42015-03-13 14:22:05 -070094 char __private[40];
Calin Juravle92687e42014-05-22 19:21:22 +010095#endif
Yabin Cuib5845722015-03-16 22:46:42 -070096} pthread_rwlock_t __attribute__((aligned(4)));
Elliott Hughesc3f11402013-10-30 14:40:09 -070097
Yabin Cui2fabea42015-03-13 14:22:05 -070098#define PTHREAD_RWLOCK_INITIALIZER { { 0 } }
Elliott Hughesc3f11402013-10-30 14:40:09 -070099
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
Christopher Ferrisea5248e2014-05-14 17:58:29 -0700106#if defined(__LP64__)
107#define PTHREAD_STACK_MIN (4 * PAGE_SIZE)
108#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109#define PTHREAD_STACK_MIN (2 * PAGE_SIZE)
Christopher Ferrisea5248e2014-05-14 17:58:29 -0700110#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111
112#define PTHREAD_CREATE_DETACHED 0x00000001
113#define PTHREAD_CREATE_JOINABLE 0x00000000
114
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800115#define PTHREAD_PROCESS_PRIVATE 0
116#define PTHREAD_PROCESS_SHARED 1
117
118#define PTHREAD_SCOPE_SYSTEM 0
119#define PTHREAD_SCOPE_PROCESS 1
120
Elliott Hughesc3f11402013-10-30 14:40:09 -0700121__BEGIN_DECLS
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122
Elliott Hughesc3f11402013-10-30 14:40:09 -0700123int pthread_atfork(void (*)(void), void (*)(void), void(*)(void));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124
Elliott Hughesc3f11402013-10-30 14:40:09 -0700125int pthread_attr_destroy(pthread_attr_t*) __nonnull((1));
126int pthread_attr_getdetachstate(const pthread_attr_t*, int*) __nonnull((1, 2));
127int pthread_attr_getguardsize(const pthread_attr_t*, size_t*) __nonnull((1, 2));
128int pthread_attr_getschedparam(const pthread_attr_t*, struct sched_param*) __nonnull((1, 2));
129int pthread_attr_getschedpolicy(const pthread_attr_t*, int*) __nonnull((1, 2));
130int pthread_attr_getscope(const pthread_attr_t*, int*) __nonnull((1, 2));
131int pthread_attr_getstack(const pthread_attr_t*, void**, size_t*) __nonnull((1, 2, 3));
132int pthread_attr_getstacksize(const pthread_attr_t*, size_t*) __nonnull((1, 2));
133int pthread_attr_init(pthread_attr_t*) __nonnull((1));
134int pthread_attr_setdetachstate(pthread_attr_t*, int) __nonnull((1));
135int pthread_attr_setguardsize(pthread_attr_t*, size_t) __nonnull((1));
136int pthread_attr_setschedparam(pthread_attr_t*, const struct sched_param*) __nonnull((1, 2));
137int pthread_attr_setschedpolicy(pthread_attr_t*, int) __nonnull((1));
138int pthread_attr_setscope(pthread_attr_t*, int) __nonnull((1));
139int pthread_attr_setstack(pthread_attr_t*, void*, size_t) __nonnull((1));
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000140int pthread_attr_setstacksize(pthread_attr_t*, size_t stack_size) __nonnull((1));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141
Elliott Hughesc3f11402013-10-30 14:40:09 -0700142int pthread_condattr_destroy(pthread_condattr_t*) __nonnull((1));
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000143int pthread_condattr_getclock(const pthread_condattr_t*, clockid_t*) __nonnull((1, 2));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700144int pthread_condattr_getpshared(const pthread_condattr_t*, int*) __nonnull((1, 2));
145int pthread_condattr_init(pthread_condattr_t*) __nonnull((1));
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000146int pthread_condattr_setclock(pthread_condattr_t*, clockid_t) __nonnull((1));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700147int pthread_condattr_setpshared(pthread_condattr_t*, int) __nonnull((1));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800148
Elliott Hughesc3f11402013-10-30 14:40:09 -0700149int pthread_cond_broadcast(pthread_cond_t*) __nonnull((1));
150int pthread_cond_destroy(pthread_cond_t*) __nonnull((1));
151int pthread_cond_init(pthread_cond_t*, const pthread_condattr_t*) __nonnull((1));
152int pthread_cond_signal(pthread_cond_t*) __nonnull((1));
153int pthread_cond_timedwait(pthread_cond_t*, pthread_mutex_t*, const struct timespec*) __nonnull((1, 2, 3));
154int pthread_cond_wait(pthread_cond_t*, pthread_mutex_t*) __nonnull((1, 2));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155
Elliott Hughesc3f11402013-10-30 14:40:09 -0700156int pthread_create(pthread_t*, pthread_attr_t const*, void *(*)(void*), void*) __nonnull((1, 3));
157int pthread_detach(pthread_t);
158void pthread_exit(void*) __noreturn;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159
Elliott Hughesc3f11402013-10-30 14:40:09 -0700160int pthread_equal(pthread_t, pthread_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800161
Elliott Hughesc3f11402013-10-30 14:40:09 -0700162int pthread_getattr_np(pthread_t, pthread_attr_t*) __nonnull((2));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800163
Elliott Hughesc3f11402013-10-30 14:40:09 -0700164int pthread_getcpuclockid(pthread_t, clockid_t*) __nonnull((2));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800165
Elliott Hughesc3f11402013-10-30 14:40:09 -0700166int pthread_getschedparam(pthread_t, int*, struct sched_param*) __nonnull((2, 3));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800167
Elliott Hughesc3f11402013-10-30 14:40:09 -0700168void* pthread_getspecific(pthread_key_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800169
Elliott Hughesa4831cb2014-09-11 16:11:43 -0700170pid_t pthread_gettid_np(pthread_t);
171
Elliott Hughesc3f11402013-10-30 14:40:09 -0700172int pthread_join(pthread_t, void**);
173
174int pthread_key_create(pthread_key_t*, void (*)(void*)) __nonnull((1));
175int pthread_key_delete(pthread_key_t);
176
Elliott Hughesc3f11402013-10-30 14:40:09 -0700177int pthread_mutexattr_destroy(pthread_mutexattr_t*) __nonnull((1));
178int pthread_mutexattr_getpshared(const pthread_mutexattr_t*, int*) __nonnull((1, 2));
179int pthread_mutexattr_gettype(const pthread_mutexattr_t*, int*) __nonnull((1, 2));
180int pthread_mutexattr_init(pthread_mutexattr_t*) __nonnull((1));
181int pthread_mutexattr_setpshared(pthread_mutexattr_t*, int) __nonnull((1));
182int pthread_mutexattr_settype(pthread_mutexattr_t*, int) __nonnull((1));
183
184int pthread_mutex_destroy(pthread_mutex_t*) __nonnull((1));
185int pthread_mutex_init(pthread_mutex_t*, const pthread_mutexattr_t*) __nonnull((1));
Elliott Hughes3d773272014-10-07 23:31:06 +0000186int pthread_mutex_lock(pthread_mutex_t*) __nonnull((1));
Elliott Hughes0e714a52014-03-03 16:42:47 -0800187int pthread_mutex_timedlock(pthread_mutex_t*, const struct timespec*) __nonnull((1, 2));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700188int pthread_mutex_trylock(pthread_mutex_t*) __nonnull((1));
Elliott Hughes3d773272014-10-07 23:31:06 +0000189int pthread_mutex_unlock(pthread_mutex_t*) __nonnull((1));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700190
191int pthread_once(pthread_once_t*, void (*)(void)) __nonnull((1, 2));
192
193int pthread_rwlockattr_destroy(pthread_rwlockattr_t*) __nonnull((1));
194int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t*, int*) __nonnull((1, 2));
195int pthread_rwlockattr_init(pthread_rwlockattr_t*) __nonnull((1));
196int pthread_rwlockattr_setpshared(pthread_rwlockattr_t*, int) __nonnull((1));
197
198int pthread_rwlock_destroy(pthread_rwlock_t*) __nonnull((1));
199int pthread_rwlock_init(pthread_rwlock_t*, const pthread_rwlockattr_t*) __nonnull((1));
200int pthread_rwlock_rdlock(pthread_rwlock_t*) __nonnull((1));
201int pthread_rwlock_timedrdlock(pthread_rwlock_t*, const struct timespec*) __nonnull((1, 2));
202int pthread_rwlock_timedwrlock(pthread_rwlock_t*, const struct timespec*) __nonnull((1, 2));
203int pthread_rwlock_tryrdlock(pthread_rwlock_t*) __nonnull((1));
204int pthread_rwlock_trywrlock(pthread_rwlock_t*) __nonnull((1));
205int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) __nonnull((1));
206int pthread_rwlock_wrlock(pthread_rwlock_t*) __nonnull((1));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800207
Elliott Hughesb27a8402014-06-10 20:47:49 -0700208pthread_t pthread_self(void) __pure2;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800209
Elliott Hughesc3f11402013-10-30 14:40:09 -0700210int pthread_setname_np(pthread_t, const char*) __nonnull((2));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800211
Elliott Hughesc3f11402013-10-30 14:40:09 -0700212int pthread_setschedparam(pthread_t, int, const struct sched_param*) __nonnull((3));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800213
Elliott Hughesc3f11402013-10-30 14:40:09 -0700214int pthread_setspecific(pthread_key_t, const void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800215
Elliott Hughesc3f11402013-10-30 14:40:09 -0700216typedef void (*__pthread_cleanup_func_t)(void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800217
218typedef struct __pthread_cleanup_t {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700219 struct __pthread_cleanup_t* __cleanup_prev;
220 __pthread_cleanup_func_t __cleanup_routine;
221 void* __cleanup_arg;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800222} __pthread_cleanup_t;
223
Elliott Hughesc3f11402013-10-30 14:40:09 -0700224extern void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t, void*);
225extern void __pthread_cleanup_pop(__pthread_cleanup_t*, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226
227/* Believe or not, the definitions of pthread_cleanup_push and
228 * pthread_cleanup_pop below are correct. Posix states that these
229 * can be implemented as macros that might introduce opening and
230 * closing braces, and that using setjmp/longjmp/return/break/continue
Elliott Hughesc3f11402013-10-30 14:40:09 -0700231 * between them results in undefined behavior.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800232 */
233#define pthread_cleanup_push(routine, arg) \
234 do { \
235 __pthread_cleanup_t __cleanup; \
236 __pthread_cleanup_push( &__cleanup, (routine), (arg) ); \
237
238#define pthread_cleanup_pop(execute) \
239 __pthread_cleanup_pop( &__cleanup, (execute)); \
Elliott Hughesc3f11402013-10-30 14:40:09 -0700240 } while (0); \
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800241
Elliott Hughesc3f11402013-10-30 14:40:09 -0700242
243#if !defined(__LP64__)
244
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000245// Bionic additions that are deprecated even in the 32-bit ABI.
246//
247// TODO: Remove them once chromium_org / NFC have switched over.
Elliott Hughesc3f11402013-10-30 14:40:09 -0700248int pthread_cond_timedwait_monotonic_np(pthread_cond_t*, pthread_mutex_t*, const struct timespec*);
249int pthread_cond_timedwait_monotonic(pthread_cond_t*, pthread_mutex_t*, const struct timespec*);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700250
Elliott Hughes0e714a52014-03-03 16:42:47 -0800251int pthread_cond_timedwait_relative_np(pthread_cond_t*, pthread_mutex_t*, const struct timespec*) /* TODO: __attribute__((deprecated("use pthread_cond_timedwait instead")))*/;
252#define HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE 1 /* TODO: stop defining this to push LP32 off this API sooner. */
253int 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 -0700254
Elliott Hughes0e714a52014-03-03 16:42:47 -0800255int pthread_mutex_lock_timeout_np(pthread_mutex_t*, unsigned) __attribute__((deprecated("use pthread_mutex_timedlock instead")));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700256
257#endif /* !defined(__LP64__) */
258
259__END_DECLS
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800260
David 'Digit' Turnerbd8d9872010-09-26 23:00:36 +0200261#endif /* _PTHREAD_H_ */