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