blob: 461bb44cfb4efa566134dcae21d493bf08680c50 [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
Elliott Hughesc3f11402013-10-30 14:40:09 -070038typedef struct {
39 int volatile value;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040} pthread_mutex_t;
41
David 'Digit' Turner9831ad32011-08-29 21:43:46 +020042#define __PTHREAD_MUTEX_INIT_VALUE 0
43#define __PTHREAD_RECURSIVE_MUTEX_INIT_VALUE 0x4000
44#define __PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE 0x8000
45
46#define PTHREAD_MUTEX_INITIALIZER {__PTHREAD_MUTEX_INIT_VALUE}
47#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER {__PTHREAD_RECURSIVE_MUTEX_INIT_VALUE}
48#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER {__PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080049
50enum {
51 PTHREAD_MUTEX_NORMAL = 0,
52 PTHREAD_MUTEX_RECURSIVE = 1,
53 PTHREAD_MUTEX_ERRORCHECK = 2,
54
55 PTHREAD_MUTEX_ERRORCHECK_NP = PTHREAD_MUTEX_ERRORCHECK,
56 PTHREAD_MUTEX_RECURSIVE_NP = PTHREAD_MUTEX_RECURSIVE,
57
58 PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
59};
60
Elliott Hughesc3f11402013-10-30 14:40:09 -070061typedef struct {
62 int volatile value;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063} pthread_cond_t;
64
Elliott Hughesc3f11402013-10-30 14:40:09 -070065#define PTHREAD_COND_INITIALIZER {0}
66
67typedef struct {
68 uint32_t flags;
69 void* stack_base;
70 size_t stack_size;
71 size_t guard_size;
72 int32_t sched_policy;
73 int32_t sched_priority;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074} pthread_attr_t;
75
76typedef long pthread_mutexattr_t;
77typedef long pthread_condattr_t;
78
Elliott Hughesc3f11402013-10-30 14:40:09 -070079typedef int pthread_rwlockattr_t;
80
81typedef struct {
82 pthread_mutex_t lock;
83 pthread_cond_t cond;
84 int numLocks;
85 int writerThreadId;
86 int pendingReaders;
87 int pendingWriters;
88 void* reserved[4]; /* for future extensibility */
89} pthread_rwlock_t;
90
91#define PTHREAD_RWLOCK_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0, 0, 0, { NULL, NULL, NULL, NULL } }
92
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093typedef int pthread_key_t;
94typedef long pthread_t;
95
Elliott Hughesc3f11402013-10-30 14:40:09 -070096typedef volatile int pthread_once_t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080097
Elliott Hughesc3f11402013-10-30 14:40:09 -070098#define PTHREAD_ONCE_INIT 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099
100#define PTHREAD_STACK_MIN (2 * PAGE_SIZE)
101
102#define PTHREAD_CREATE_DETACHED 0x00000001
103#define PTHREAD_CREATE_JOINABLE 0x00000000
104
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105#define PTHREAD_PROCESS_PRIVATE 0
106#define PTHREAD_PROCESS_SHARED 1
107
108#define PTHREAD_SCOPE_SYSTEM 0
109#define PTHREAD_SCOPE_PROCESS 1
110
Elliott Hughesc3f11402013-10-30 14:40:09 -0700111__BEGIN_DECLS
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112
Elliott Hughesc3f11402013-10-30 14:40:09 -0700113int pthread_atfork(void (*)(void), void (*)(void), void(*)(void));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114
Elliott Hughesc3f11402013-10-30 14:40:09 -0700115int pthread_attr_destroy(pthread_attr_t*) __nonnull((1));
116int pthread_attr_getdetachstate(const pthread_attr_t*, int*) __nonnull((1, 2));
117int pthread_attr_getguardsize(const pthread_attr_t*, size_t*) __nonnull((1, 2));
118int pthread_attr_getschedparam(const pthread_attr_t*, struct sched_param*) __nonnull((1, 2));
119int pthread_attr_getschedpolicy(const pthread_attr_t*, int*) __nonnull((1, 2));
120int pthread_attr_getscope(const pthread_attr_t*, int*) __nonnull((1, 2));
121int pthread_attr_getstack(const pthread_attr_t*, void**, size_t*) __nonnull((1, 2, 3));
122int pthread_attr_getstacksize(const pthread_attr_t*, size_t*) __nonnull((1, 2));
123int pthread_attr_init(pthread_attr_t*) __nonnull((1));
124int pthread_attr_setdetachstate(pthread_attr_t*, int) __nonnull((1));
125int pthread_attr_setguardsize(pthread_attr_t*, size_t) __nonnull((1));
126int pthread_attr_setschedparam(pthread_attr_t*, const struct sched_param*) __nonnull((1, 2));
127int pthread_attr_setschedpolicy(pthread_attr_t*, int) __nonnull((1));
128int pthread_attr_setscope(pthread_attr_t*, int) __nonnull((1));
129int pthread_attr_setstack(pthread_attr_t*, void*, size_t) __nonnull((1));
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000130int pthread_attr_setstacksize(pthread_attr_t*, size_t stack_size) __nonnull((1));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131
Elliott Hughesc3f11402013-10-30 14:40:09 -0700132int pthread_condattr_destroy(pthread_condattr_t*) __nonnull((1));
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000133int pthread_condattr_getclock(const pthread_condattr_t*, clockid_t*) __nonnull((1, 2));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700134int pthread_condattr_getpshared(const pthread_condattr_t*, int*) __nonnull((1, 2));
135int pthread_condattr_init(pthread_condattr_t*) __nonnull((1));
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000136int pthread_condattr_setclock(pthread_condattr_t*, clockid_t) __nonnull((1));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700137int pthread_condattr_setpshared(pthread_condattr_t*, int) __nonnull((1));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138
Elliott Hughesc3f11402013-10-30 14:40:09 -0700139int pthread_cond_broadcast(pthread_cond_t*) __nonnull((1));
140int pthread_cond_destroy(pthread_cond_t*) __nonnull((1));
141int pthread_cond_init(pthread_cond_t*, const pthread_condattr_t*) __nonnull((1));
142int pthread_cond_signal(pthread_cond_t*) __nonnull((1));
143int pthread_cond_timedwait(pthread_cond_t*, pthread_mutex_t*, const struct timespec*) __nonnull((1, 2, 3));
144int pthread_cond_wait(pthread_cond_t*, pthread_mutex_t*) __nonnull((1, 2));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800145
Elliott Hughesc3f11402013-10-30 14:40:09 -0700146int pthread_create(pthread_t*, pthread_attr_t const*, void *(*)(void*), void*) __nonnull((1, 3));
147int pthread_detach(pthread_t);
148void pthread_exit(void*) __noreturn;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800149
Elliott Hughesc3f11402013-10-30 14:40:09 -0700150int pthread_equal(pthread_t, pthread_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800151
Elliott Hughesc3f11402013-10-30 14:40:09 -0700152int pthread_getattr_np(pthread_t, pthread_attr_t*) __nonnull((2));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800153
Elliott Hughesc3f11402013-10-30 14:40:09 -0700154int pthread_getcpuclockid(pthread_t, clockid_t*) __nonnull((2));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155
Elliott Hughesc3f11402013-10-30 14:40:09 -0700156int pthread_getschedparam(pthread_t, int*, struct sched_param*) __nonnull((2, 3));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157
Elliott Hughesc3f11402013-10-30 14:40:09 -0700158void* pthread_getspecific(pthread_key_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159
Elliott Hughesc3f11402013-10-30 14:40:09 -0700160int pthread_join(pthread_t, void**);
161
162int pthread_key_create(pthread_key_t*, void (*)(void*)) __nonnull((1));
163int pthread_key_delete(pthread_key_t);
164
165int pthread_kill(pthread_t, int);
166
167int pthread_mutexattr_destroy(pthread_mutexattr_t*) __nonnull((1));
168int pthread_mutexattr_getpshared(const pthread_mutexattr_t*, int*) __nonnull((1, 2));
169int pthread_mutexattr_gettype(const pthread_mutexattr_t*, int*) __nonnull((1, 2));
170int pthread_mutexattr_init(pthread_mutexattr_t*) __nonnull((1));
171int pthread_mutexattr_setpshared(pthread_mutexattr_t*, int) __nonnull((1));
172int pthread_mutexattr_settype(pthread_mutexattr_t*, int) __nonnull((1));
173
174int pthread_mutex_destroy(pthread_mutex_t*) __nonnull((1));
175int pthread_mutex_init(pthread_mutex_t*, const pthread_mutexattr_t*) __nonnull((1));
176int pthread_mutex_lock(pthread_mutex_t*) __nonnull((1));
Elliott Hughes0e714a52014-03-03 16:42:47 -0800177int pthread_mutex_timedlock(pthread_mutex_t*, const struct timespec*) __nonnull((1, 2));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700178int pthread_mutex_trylock(pthread_mutex_t*) __nonnull((1));
179int pthread_mutex_unlock(pthread_mutex_t*) __nonnull((1));
180
181int pthread_once(pthread_once_t*, void (*)(void)) __nonnull((1, 2));
182
183int pthread_rwlockattr_destroy(pthread_rwlockattr_t*) __nonnull((1));
184int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t*, int*) __nonnull((1, 2));
185int pthread_rwlockattr_init(pthread_rwlockattr_t*) __nonnull((1));
186int pthread_rwlockattr_setpshared(pthread_rwlockattr_t*, int) __nonnull((1));
187
188int pthread_rwlock_destroy(pthread_rwlock_t*) __nonnull((1));
189int pthread_rwlock_init(pthread_rwlock_t*, const pthread_rwlockattr_t*) __nonnull((1));
190int pthread_rwlock_rdlock(pthread_rwlock_t*) __nonnull((1));
191int pthread_rwlock_timedrdlock(pthread_rwlock_t*, const struct timespec*) __nonnull((1, 2));
192int pthread_rwlock_timedwrlock(pthread_rwlock_t*, const struct timespec*) __nonnull((1, 2));
193int pthread_rwlock_tryrdlock(pthread_rwlock_t*) __nonnull((1));
194int pthread_rwlock_trywrlock(pthread_rwlock_t*) __nonnull((1));
195int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) __nonnull((1));
196int pthread_rwlock_wrlock(pthread_rwlock_t*) __nonnull((1));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800197
198pthread_t pthread_self(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800199
Elliott Hughesc3f11402013-10-30 14:40:09 -0700200int pthread_setname_np(pthread_t, const char*) __nonnull((2));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201
Elliott Hughesc3f11402013-10-30 14:40:09 -0700202int pthread_setschedparam(pthread_t, int, const struct sched_param*) __nonnull((3));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800203
Elliott Hughesc3f11402013-10-30 14:40:09 -0700204int pthread_setspecific(pthread_key_t, const void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800205
Elliott Hughesc3f11402013-10-30 14:40:09 -0700206int pthread_sigmask(int, const sigset_t*, sigset_t*);
David 'Digit' Turneree7b0772010-03-18 14:07:42 -0700207
Elliott Hughesc3f11402013-10-30 14:40:09 -0700208typedef void (*__pthread_cleanup_func_t)(void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800209
210typedef struct __pthread_cleanup_t {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700211 struct __pthread_cleanup_t* __cleanup_prev;
212 __pthread_cleanup_func_t __cleanup_routine;
213 void* __cleanup_arg;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800214} __pthread_cleanup_t;
215
Elliott Hughesc3f11402013-10-30 14:40:09 -0700216extern void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t, void*);
217extern void __pthread_cleanup_pop(__pthread_cleanup_t*, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800218
219/* Believe or not, the definitions of pthread_cleanup_push and
220 * pthread_cleanup_pop below are correct. Posix states that these
221 * can be implemented as macros that might introduce opening and
222 * closing braces, and that using setjmp/longjmp/return/break/continue
Elliott Hughesc3f11402013-10-30 14:40:09 -0700223 * between them results in undefined behavior.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800224 */
225#define pthread_cleanup_push(routine, arg) \
226 do { \
227 __pthread_cleanup_t __cleanup; \
228 __pthread_cleanup_push( &__cleanup, (routine), (arg) ); \
229
230#define pthread_cleanup_pop(execute) \
231 __pthread_cleanup_pop( &__cleanup, (execute)); \
Elliott Hughesc3f11402013-10-30 14:40:09 -0700232 } while (0); \
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800233
Elliott Hughesc3f11402013-10-30 14:40:09 -0700234
235#if !defined(__LP64__)
236
237/* Deprecated by POSIX. TODO: support for LP64 but add deprecated attribute instead? */
238int pthread_attr_getstackaddr(const pthread_attr_t*, void**) __nonnull((1, 2)); /* deprecated */
239int pthread_attr_setstackaddr(pthread_attr_t*, void*) __nonnull((1)); /* deprecated */
240
Narayan Kamath51e6cb32014-03-03 15:38:51 +0000241// Bionic additions that are deprecated even in the 32-bit ABI.
242//
243// TODO: Remove them once chromium_org / NFC have switched over.
Elliott Hughesc3f11402013-10-30 14:40:09 -0700244int pthread_cond_timedwait_monotonic_np(pthread_cond_t*, pthread_mutex_t*, const struct timespec*);
245int pthread_cond_timedwait_monotonic(pthread_cond_t*, pthread_mutex_t*, const struct timespec*);
246#define HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC 1
247
Elliott Hughes0e714a52014-03-03 16:42:47 -0800248int pthread_cond_timedwait_relative_np(pthread_cond_t*, pthread_mutex_t*, const struct timespec*) /* TODO: __attribute__((deprecated("use pthread_cond_timedwait instead")))*/;
249#define HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE 1 /* TODO: stop defining this to push LP32 off this API sooner. */
250int 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 -0700251
Elliott Hughes0e714a52014-03-03 16:42:47 -0800252int pthread_mutex_lock_timeout_np(pthread_mutex_t*, unsigned) __attribute__((deprecated("use pthread_mutex_timedlock instead")));
Elliott Hughesc3f11402013-10-30 14:40:09 -0700253
254#endif /* !defined(__LP64__) */
255
256__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_ */