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 
29 #ifndef _PTHREAD_H_
30 #define _PTHREAD_H_
31 
32 #include <limits.h>
33 #include <bits/pthread_types.h>
34 #include <sched.h>
35 #include <sys/cdefs.h>
36 #include <sys/types.h>
37 #include <time.h>
38 
39 typedef struct {
40 #if defined(__LP64__)
41   int32_t __private[10];
42 #else
43   int32_t __private[1];
44 #endif
45 } pthread_mutex_t;
46 
47 typedef long pthread_mutexattr_t;
48 
49 enum {
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 #define PTHREAD_MUTEX_INITIALIZER { { ((PTHREAD_MUTEX_NORMAL & 3) << 14) } }
61 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP { { ((PTHREAD_MUTEX_RECURSIVE & 3) << 14) } }
62 #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP { { ((PTHREAD_MUTEX_ERRORCHECK & 3) << 14) } }
63 
64 typedef struct {
65 #if defined(__LP64__)
66   int32_t __private[12];
67 #else
68   int32_t __private[1];
69 #endif
70 } pthread_cond_t;
71 
72 typedef long pthread_condattr_t;
73 
74 #define PTHREAD_COND_INITIALIZER  { { 0 } }
75 
76 typedef struct {
77 #if defined(__LP64__)
78   int32_t __private[14];
79 #else
80   int32_t __private[10];
81 #endif
82 } pthread_rwlock_t;
83 
84 typedef long pthread_rwlockattr_t;
85 
86 #define PTHREAD_RWLOCK_INITIALIZER  { { 0 } }
87 
88 enum {
89   PTHREAD_RWLOCK_PREFER_READER_NP = 0,
90   PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP = 1,
91 };
92 
93 typedef int pthread_key_t;
94 
95 typedef int pthread_once_t;
96 
97 #define PTHREAD_ONCE_INIT 0
98 
99 typedef struct {
100 #if defined(__LP64__)
101   int64_t __private[4];
102 #else
103   int32_t __private[8];
104 #endif
105 } pthread_barrier_t;
106 
107 typedef int pthread_barrierattr_t;
108 
109 #define PTHREAD_BARRIER_SERIAL_THREAD -1
110 
111 typedef struct {
112 #if defined(__LP64__)
113   int64_t __private;
114 #else
115   int32_t __private[2];
116 #endif
117 } pthread_spinlock_t;
118 
119 #if defined(__LP64__)
120 #define PTHREAD_STACK_MIN (4 * PAGE_SIZE)
121 #else
122 #define PTHREAD_STACK_MIN (2 * PAGE_SIZE)
123 #endif
124 
125 #define PTHREAD_CREATE_DETACHED  0x00000001
126 #define PTHREAD_CREATE_JOINABLE  0x00000000
127 
128 #define PTHREAD_PROCESS_PRIVATE  0
129 #define PTHREAD_PROCESS_SHARED   1
130 
131 #define PTHREAD_SCOPE_SYSTEM     0
132 #define PTHREAD_SCOPE_PROCESS    1
133 
134 __BEGIN_DECLS
135 
136 int pthread_atfork(void (*)(void), void (*)(void), void(*)(void));
137 
138 int pthread_attr_destroy(pthread_attr_t*) __nonnull((1));
139 int pthread_attr_getdetachstate(const pthread_attr_t*, int*) __nonnull((1, 2));
140 int pthread_attr_getguardsize(const pthread_attr_t*, size_t*) __nonnull((1, 2));
141 int pthread_attr_getschedparam(const pthread_attr_t*, struct sched_param*) __nonnull((1, 2));
142 int pthread_attr_getschedpolicy(const pthread_attr_t*, int*) __nonnull((1, 2));
143 int pthread_attr_getscope(const pthread_attr_t*, int*) __nonnull((1, 2));
144 int pthread_attr_getstack(const pthread_attr_t*, void**, size_t*) __nonnull((1, 2, 3));
145 int pthread_attr_getstacksize(const pthread_attr_t*, size_t*) __nonnull((1, 2));
146 int pthread_attr_init(pthread_attr_t*) __nonnull((1));
147 int pthread_attr_setdetachstate(pthread_attr_t*, int) __nonnull((1));
148 int pthread_attr_setguardsize(pthread_attr_t*, size_t) __nonnull((1));
149 int pthread_attr_setschedparam(pthread_attr_t*, const struct sched_param*) __nonnull((1, 2));
150 int pthread_attr_setschedpolicy(pthread_attr_t*, int) __nonnull((1));
151 int pthread_attr_setscope(pthread_attr_t*, int) __nonnull((1));
152 int pthread_attr_setstack(pthread_attr_t*, void*, size_t) __nonnull((1));
153 int pthread_attr_setstacksize(pthread_attr_t*, size_t) __nonnull((1));
154 
155 int pthread_condattr_destroy(pthread_condattr_t*) __nonnull((1));
156 int pthread_condattr_getclock(const pthread_condattr_t*, clockid_t*) __nonnull((1, 2));
157 int pthread_condattr_getpshared(const pthread_condattr_t*, int*) __nonnull((1, 2));
158 int pthread_condattr_init(pthread_condattr_t*) __nonnull((1));
159 int pthread_condattr_setclock(pthread_condattr_t*, clockid_t) __nonnull((1));
160 int pthread_condattr_setpshared(pthread_condattr_t*, int) __nonnull((1));
161 
162 int pthread_cond_broadcast(pthread_cond_t*) __nonnull((1));
163 int pthread_cond_destroy(pthread_cond_t*) __nonnull((1));
164 int pthread_cond_init(pthread_cond_t*, const pthread_condattr_t*) __nonnull((1));
165 int pthread_cond_signal(pthread_cond_t*) __nonnull((1));
166 int pthread_cond_timedwait(pthread_cond_t*, pthread_mutex_t*, const struct timespec*) __nonnull((1, 2, 3));
167 int pthread_cond_wait(pthread_cond_t*, pthread_mutex_t*) __nonnull((1, 2));
168 
169 int pthread_create(pthread_t*, pthread_attr_t const*, void *(*)(void*), void*) __nonnull((1, 3));
170 int pthread_detach(pthread_t);
171 void pthread_exit(void*) __noreturn;
172 
173 int pthread_equal(pthread_t, pthread_t);
174 
175 int pthread_getattr_np(pthread_t, pthread_attr_t*) __nonnull((2));
176 
177 int pthread_getcpuclockid(pthread_t, clockid_t*) __nonnull((2));
178 
179 int pthread_getschedparam(pthread_t, int*, struct sched_param*) __nonnull((2, 3));
180 
181 void* pthread_getspecific(pthread_key_t);
182 
183 pid_t pthread_gettid_np(pthread_t);
184 
185 int pthread_join(pthread_t, void**);
186 
187 int pthread_key_create(pthread_key_t*, void (*)(void*)) __nonnull((1));
188 int pthread_key_delete(pthread_key_t);
189 
190 int pthread_mutexattr_destroy(pthread_mutexattr_t*) __nonnull((1));
191 int pthread_mutexattr_getpshared(const pthread_mutexattr_t*, int*) __nonnull((1, 2));
192 int pthread_mutexattr_gettype(const pthread_mutexattr_t*, int*) __nonnull((1, 2));
193 int pthread_mutexattr_init(pthread_mutexattr_t*) __nonnull((1));
194 int pthread_mutexattr_setpshared(pthread_mutexattr_t*, int) __nonnull((1));
195 int pthread_mutexattr_settype(pthread_mutexattr_t*, int) __nonnull((1));
196 
197 int pthread_mutex_destroy(pthread_mutex_t*) __nonnull((1));
198 int pthread_mutex_init(pthread_mutex_t*, const pthread_mutexattr_t*) __nonnull((1));
199 #if !defined(__LP64__)
200 int pthread_mutex_lock(pthread_mutex_t*) /* __nonnull((1)) */;
201 #else
202 int pthread_mutex_lock(pthread_mutex_t*) __nonnull((1));
203 #endif
204 int pthread_mutex_timedlock(pthread_mutex_t*, const struct timespec*) __nonnull((1, 2));
205 int pthread_mutex_trylock(pthread_mutex_t*) __nonnull((1));
206 #if !defined(__LP4__)
207 int pthread_mutex_unlock(pthread_mutex_t*) /* __nonnull((1)) */;
208 #else
209 int pthread_mutex_unlock(pthread_mutex_t*) __nonnull((1));
210 #endif
211 
212 int pthread_once(pthread_once_t*, void (*)(void)) __nonnull((1, 2));
213 
214 int pthread_rwlockattr_init(pthread_rwlockattr_t*) __nonnull((1));
215 int pthread_rwlockattr_destroy(pthread_rwlockattr_t*) __nonnull((1));
216 int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t*, int*) __nonnull((1, 2));
217 int pthread_rwlockattr_setpshared(pthread_rwlockattr_t*, int) __nonnull((1));
218 int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t*, int*) __nonnull((1, 2));
219 int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t*, int) __nonnull((1));
220 
221 int pthread_rwlock_destroy(pthread_rwlock_t*) __nonnull((1));
222 int pthread_rwlock_init(pthread_rwlock_t*, const pthread_rwlockattr_t*) __nonnull((1));
223 int pthread_rwlock_rdlock(pthread_rwlock_t*) __nonnull((1));
224 int pthread_rwlock_timedrdlock(pthread_rwlock_t*, const struct timespec*) __nonnull((1, 2));
225 int pthread_rwlock_timedwrlock(pthread_rwlock_t*, const struct timespec*) __nonnull((1, 2));
226 int pthread_rwlock_tryrdlock(pthread_rwlock_t*) __nonnull((1));
227 int pthread_rwlock_trywrlock(pthread_rwlock_t*) __nonnull((1));
228 int pthread_rwlock_unlock(pthread_rwlock_t *) __nonnull((1));
229 int pthread_rwlock_wrlock(pthread_rwlock_t*) __nonnull((1));
230 
231 int pthread_barrierattr_init(pthread_barrierattr_t* attr) __nonnull((1));
232 int pthread_barrierattr_destroy(pthread_barrierattr_t* attr) __nonnull((1));
233 int pthread_barrierattr_getpshared(pthread_barrierattr_t* attr, int* pshared) __nonnull((1, 2));
234 int pthread_barrierattr_setpshared(pthread_barrierattr_t* attr, int pshared) __nonnull((1));
235 
236 int pthread_barrier_init(pthread_barrier_t*, const pthread_barrierattr_t*, unsigned) __nonnull((1));
237 int pthread_barrier_destroy(pthread_barrier_t*) __nonnull((1));
238 int pthread_barrier_wait(pthread_barrier_t*) __nonnull((1));
239 
240 int pthread_spin_destroy(pthread_spinlock_t*) __nonnull((1));
241 int pthread_spin_init(pthread_spinlock_t*, int) __nonnull((1));
242 int pthread_spin_lock(pthread_spinlock_t*) __nonnull((1));
243 int pthread_spin_trylock(pthread_spinlock_t*) __nonnull((1));
244 int pthread_spin_unlock(pthread_spinlock_t*) __nonnull((1));
245 
246 pthread_t pthread_self(void) __pure2;
247 
248 int pthread_setname_np(pthread_t, const char*) __nonnull((2));
249 
250 int pthread_setschedparam(pthread_t, int, const struct sched_param*) __nonnull((3));
251 
252 int pthread_setspecific(pthread_key_t, const void*);
253 
254 typedef void (*__pthread_cleanup_func_t)(void*);
255 
256 typedef struct __pthread_cleanup_t {
257   struct __pthread_cleanup_t*   __cleanup_prev;
258   __pthread_cleanup_func_t      __cleanup_routine;
259   void*                         __cleanup_arg;
260 } __pthread_cleanup_t;
261 
262 extern void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t, void*);
263 extern void __pthread_cleanup_pop(__pthread_cleanup_t*, int);
264 
265 /* Believe or not, the definitions of pthread_cleanup_push and
266  * pthread_cleanup_pop below are correct. Posix states that these
267  * can be implemented as macros that might introduce opening and
268  * closing braces, and that using setjmp/longjmp/return/break/continue
269  * between them results in undefined behavior.
270  */
271 #define  pthread_cleanup_push(routine, arg)                      \
272     do {                                                         \
273         __pthread_cleanup_t  __cleanup;                          \
274         __pthread_cleanup_push( &__cleanup, (routine), (arg) );  \
275 
276 #define  pthread_cleanup_pop(execute)                  \
277         __pthread_cleanup_pop( &__cleanup, (execute)); \
278     } while (0);                                       \
279 
280 
281 #if !defined(__LP64__)
282 
283 // Bionic additions that are deprecated even in the 32-bit ABI.
284 //
285 // TODO: Remove them once chromium_org / NFC have switched over.
286 int pthread_cond_timedwait_monotonic_np(pthread_cond_t*, pthread_mutex_t*, const struct timespec*);
287 int pthread_cond_timedwait_monotonic(pthread_cond_t*, pthread_mutex_t*, const struct timespec*);
288 
289 int pthread_cond_timedwait_relative_np(pthread_cond_t*, pthread_mutex_t*, const struct timespec*) /* TODO: __attribute__((deprecated("use pthread_cond_timedwait instead")))*/;
290 #define HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE 1 /* TODO: stop defining this to push LP32 off this API sooner. */
291 int pthread_cond_timeout_np(pthread_cond_t*, pthread_mutex_t*, unsigned) /* TODO: __attribute__((deprecated("use pthread_cond_timedwait instead")))*/;
292 
293 int pthread_mutex_lock_timeout_np(pthread_mutex_t*, unsigned) __attribute__((deprecated("use pthread_mutex_timedlock instead")));
294 
295 #endif /* !defined(__LP64__) */
296 
297 __END_DECLS
298 
299 #endif /* _PTHREAD_H_ */
300