blob: 56c48eafca857bd672c6ae2a37a2919304e54329 [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 */
28#ifndef _PTHREAD_H_
29#define _PTHREAD_H_
30
31#include <time.h>
32#include <signal.h>
33#include <sched.h>
34#include <limits.h>
35#include <sys/types.h>
36
37/*
38 * Types
39 */
40typedef struct
41{
42 int volatile value;
43} pthread_mutex_t;
44
45#define PTHREAD_MUTEX_INITIALIZER {0}
46#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER {0x4000}
47#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER {0x8000}
48
49enum {
50 PTHREAD_MUTEX_NORMAL = 0,
51 PTHREAD_MUTEX_RECURSIVE = 1,
52 PTHREAD_MUTEX_ERRORCHECK = 2,
53
54 PTHREAD_MUTEX_ERRORCHECK_NP = PTHREAD_MUTEX_ERRORCHECK,
55 PTHREAD_MUTEX_RECURSIVE_NP = PTHREAD_MUTEX_RECURSIVE,
56
57 PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
58};
59
60
61
62typedef struct
63{
64 int volatile value;
65} pthread_cond_t;
66
67typedef struct
68{
69 uint32_t flags;
70 void * stack_base;
71 size_t stack_size;
72 size_t guard_size;
73 int32_t sched_policy;
74 int32_t sched_priority;
75} pthread_attr_t;
76
77typedef long pthread_mutexattr_t;
78typedef long pthread_condattr_t;
79
80typedef int pthread_key_t;
81typedef long pthread_t;
82
83typedef volatile int pthread_once_t;
84
85/*
86 * Defines
87 */
88#define PTHREAD_COND_INITIALIZER {0}
89
90#define PTHREAD_STACK_MIN (2 * PAGE_SIZE)
91
92#define PTHREAD_CREATE_DETACHED 0x00000001
93#define PTHREAD_CREATE_JOINABLE 0x00000000
94
95#define PTHREAD_ONCE_INIT 0
96
97#define PTHREAD_PROCESS_PRIVATE 0
98#define PTHREAD_PROCESS_SHARED 1
99
100#define PTHREAD_SCOPE_SYSTEM 0
101#define PTHREAD_SCOPE_PROCESS 1
102
103/*
104 * Prototypes
105 */
David 'Digit' Turner847b1832011-03-11 14:51:19 +0100106#ifdef __cplusplus
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800107extern "C" {
108#endif
109
110int pthread_attr_init(pthread_attr_t * attr);
111int pthread_attr_destroy(pthread_attr_t * attr);
112
113int pthread_attr_setdetachstate(pthread_attr_t * attr, int state);
114int pthread_attr_getdetachstate(pthread_attr_t const * attr, int * state);
115
116int pthread_attr_setschedpolicy(pthread_attr_t * attr, int policy);
117int pthread_attr_getschedpolicy(pthread_attr_t const * attr, int * policy);
118
119int pthread_attr_setschedparam(pthread_attr_t * attr, struct sched_param const * param);
120int pthread_attr_getschedparam(pthread_attr_t const * attr, struct sched_param * param);
121
122int pthread_attr_setstacksize(pthread_attr_t * attr, size_t stack_size);
123int pthread_attr_getstacksize(pthread_attr_t const * attr, size_t * stack_size);
124
125int pthread_attr_setstackaddr(pthread_attr_t * attr, void * stackaddr);
126int pthread_attr_getstackaddr(pthread_attr_t const * attr, void ** stackaddr);
127
128int pthread_attr_setstack(pthread_attr_t * attr, void * stackaddr, size_t stack_size);
129int pthread_attr_getstack(pthread_attr_t const * attr, void ** stackaddr, size_t * stack_size);
130
131int pthread_attr_setguardsize(pthread_attr_t * attr, size_t guard_size);
132int pthread_attr_getguardsize(pthread_attr_t const * attr, size_t * guard_size);
133
134int pthread_attr_setscope(pthread_attr_t *attr, int scope);
135int pthread_attr_getscope(pthread_attr_t const *attr);
136
137int pthread_getattr_np(pthread_t thid, pthread_attr_t * attr);
138
139int pthread_create(pthread_t *thread, pthread_attr_t const * attr,
140 void *(*start_routine)(void *), void * arg);
141void pthread_exit(void * retval);
142int pthread_join(pthread_t thid, void ** ret_val);
143int pthread_detach(pthread_t thid);
144
Jim Huang2c8ccf22012-03-09 01:51:14 -0500145__attribute__((const))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800146pthread_t pthread_self(void);
147int pthread_equal(pthread_t one, pthread_t two);
148
149int pthread_getschedparam(pthread_t thid, int * policy,
150 struct sched_param * param);
151int pthread_setschedparam(pthread_t thid, int poilcy,
152 struct sched_param const * param);
153
154int pthread_mutexattr_init(pthread_mutexattr_t *attr);
155int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
156int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type);
157int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
158int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
159int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared);
160
161int pthread_mutex_init(pthread_mutex_t *mutex,
162 const pthread_mutexattr_t *attr);
163int pthread_mutex_destroy(pthread_mutex_t *mutex);
164int pthread_mutex_lock(pthread_mutex_t *mutex);
165int pthread_mutex_unlock(pthread_mutex_t *mutex);
166int pthread_mutex_trylock(pthread_mutex_t *mutex);
167int pthread_mutex_timedlock(pthread_mutex_t *mutex, struct timespec* ts);
168
David 'Digit' Turneree7b0772010-03-18 14:07:42 -0700169int pthread_condattr_init(pthread_condattr_t *attr);
170int pthread_condattr_getpshared(pthread_condattr_t *attr, int *pshared);
171int pthread_condattr_setpshared(pthread_condattr_t* attr, int pshared);
172int pthread_condattr_destroy(pthread_condattr_t *attr);
173
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800174int pthread_cond_init(pthread_cond_t *cond,
175 const pthread_condattr_t *attr);
176int pthread_cond_destroy(pthread_cond_t *cond);
177int pthread_cond_broadcast(pthread_cond_t *cond);
178int pthread_cond_signal(pthread_cond_t *cond);
179int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
180int pthread_cond_timedwait(pthread_cond_t *cond,
181 pthread_mutex_t * mutex,
182 const struct timespec *abstime);
183
184/* BIONIC: same as pthread_cond_timedwait, except the 'abstime' given refers
185 * to the CLOCK_MONOTONIC clock instead, to avoid any problems when
186 * the wall-clock time is changed brutally
187 */
Mathias Agopiana2f5e212009-07-13 15:00:46 -0700188int pthread_cond_timedwait_monotonic_np(pthread_cond_t *cond,
189 pthread_mutex_t *mutex,
190 const struct timespec *abstime);
191
192/* BIONIC: DEPRECATED. same as pthread_cond_timedwait_monotonic_np()
193 * unfortunately pthread_cond_timedwait_monotonic has shipped already
194 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800195int pthread_cond_timedwait_monotonic(pthread_cond_t *cond,
196 pthread_mutex_t *mutex,
197 const struct timespec *abstime);
198
Mathias Agopiana2f5e212009-07-13 15:00:46 -0700199#define HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC 1
200
201/* BIONIC: same as pthread_cond_timedwait, except the 'reltime' given refers
202 * is relative to the current time.
203 */
204int pthread_cond_timedwait_relative_np(pthread_cond_t *cond,
205 pthread_mutex_t *mutex,
206 const struct timespec *reltime);
207
208#define HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE 1
209
210
211
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800212int pthread_cond_timeout_np(pthread_cond_t *cond,
213 pthread_mutex_t * mutex,
214 unsigned msecs);
215
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700216/* same as pthread_mutex_lock(), but will wait up to 'msecs' milli-seconds
217 * before returning. same return values than pthread_mutex_trylock though, i.e.
218 * returns EBUSY if the lock could not be acquired after the timeout
219 * expired.
220 */
221int pthread_mutex_lock_timeout_np(pthread_mutex_t *mutex, unsigned msecs);
222
David 'Digit' Turner8a1d2cf2010-05-11 16:39:22 -0700223/* read-write lock support */
224
225typedef int pthread_rwlockattr_t;
226
227typedef struct {
228 pthread_mutex_t lock;
229 pthread_cond_t cond;
230 int numLocks;
231 int writerThreadId;
232 int pendingReaders;
233 int pendingWriters;
234 void* reserved[4]; /* for future extensibility */
235} pthread_rwlock_t;
236
David 'Digit' Turnerca4462d2011-03-10 10:22:55 +0100237#define PTHREAD_RWLOCK_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0, 0, 0, { NULL, NULL, NULL, NULL } }
David 'Digit' Turner8a1d2cf2010-05-11 16:39:22 -0700238
239int pthread_rwlockattr_init(pthread_rwlockattr_t *attr);
240int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);
241int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared);
242int pthread_rwlockattr_getpshared(pthread_rwlockattr_t *attr, int *pshared);
243
244int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
245int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
246
247int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
248int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
249int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock, const struct timespec *abs_timeout);
250
251int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
252int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
253int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock, const struct timespec *abs_timeout);
254
255int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
256
257
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800258int pthread_key_create(pthread_key_t *key, void (*destructor_function)(void *));
259int pthread_key_delete (pthread_key_t);
260int pthread_setspecific(pthread_key_t key, const void *value);
261void *pthread_getspecific(pthread_key_t key);
262
263int pthread_kill(pthread_t tid, int sig);
264int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset);
265
266int pthread_getcpuclockid(pthread_t tid, clockid_t *clockid);
267
268int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));
269
André Goddard Rosa78c1c042010-05-19 23:17:16 -0300270int pthread_setname_np(pthread_t thid, const char *thname);
271
Matt Fischer4f086ae2010-06-25 14:36:39 -0500272int pthread_atfork(void (*prepare)(void), void (*parent)(void), void(*child)(void));
273
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800274typedef void (*__pthread_cleanup_func_t)(void*);
275
276typedef struct __pthread_cleanup_t {
277 struct __pthread_cleanup_t* __cleanup_prev;
278 __pthread_cleanup_func_t __cleanup_routine;
279 void* __cleanup_arg;
280} __pthread_cleanup_t;
281
282extern void __pthread_cleanup_push(__pthread_cleanup_t* c,
283 __pthread_cleanup_func_t routine,
284 void* arg);
285
286extern void __pthread_cleanup_pop(__pthread_cleanup_t* c,
287 int execute);
288
289/* Believe or not, the definitions of pthread_cleanup_push and
290 * pthread_cleanup_pop below are correct. Posix states that these
291 * can be implemented as macros that might introduce opening and
292 * closing braces, and that using setjmp/longjmp/return/break/continue
293 * between them results in undefined behaviour.
294 *
295 * And indeed, GLibc and other C libraries use a similar definition
296 */
297#define pthread_cleanup_push(routine, arg) \
298 do { \
299 __pthread_cleanup_t __cleanup; \
300 __pthread_cleanup_push( &__cleanup, (routine), (arg) ); \
301
302#define pthread_cleanup_pop(execute) \
303 __pthread_cleanup_pop( &__cleanup, (execute)); \
304 } while (0);
305
David 'Digit' Turner847b1832011-03-11 14:51:19 +0100306#ifdef __cplusplus
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800307} /* extern "C" */
308#endif
309
310/************ TO FIX ************/
311
312#define LONG_LONG_MAX __LONG_LONG_MAX__
313#define LONG_LONG_MIN (-__LONG_LONG_MAX__ - 1)
314
David 'Digit' Turnerbd8d9872010-09-26 23:00:36 +0200315#endif /* _PTHREAD_H_ */