pthread.h revision c2ce12046c74613da2c41e1cce876db6ce81d4a9
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__BEGIN_DECLS 40 41#if defined(__clang__) 42#pragma clang diagnostic push 43#pragma clang diagnostic ignored "-Wnullability-completeness" 44#endif 45 46enum { 47 PTHREAD_MUTEX_NORMAL = 0, 48 PTHREAD_MUTEX_RECURSIVE = 1, 49 PTHREAD_MUTEX_ERRORCHECK = 2, 50 51 PTHREAD_MUTEX_ERRORCHECK_NP = PTHREAD_MUTEX_ERRORCHECK, 52 PTHREAD_MUTEX_RECURSIVE_NP = PTHREAD_MUTEX_RECURSIVE, 53 54 PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL 55}; 56 57#define PTHREAD_MUTEX_INITIALIZER { { ((PTHREAD_MUTEX_NORMAL & 3) << 14) } } 58#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP { { ((PTHREAD_MUTEX_RECURSIVE & 3) << 14) } } 59#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP { { ((PTHREAD_MUTEX_ERRORCHECK & 3) << 14) } } 60 61#define PTHREAD_COND_INITIALIZER { { 0 } } 62 63#define PTHREAD_RWLOCK_INITIALIZER { { 0 } } 64 65enum { 66 PTHREAD_RWLOCK_PREFER_READER_NP = 0, 67 PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP = 1, 68}; 69 70#define PTHREAD_ONCE_INIT 0 71 72#if __ANDROID_API__ >= __ANDROID_API_N__ 73#define PTHREAD_BARRIER_SERIAL_THREAD -1 74#endif 75 76#if defined(__LP64__) 77#define PTHREAD_STACK_MIN (4 * PAGE_SIZE) 78#else 79#define PTHREAD_STACK_MIN (2 * PAGE_SIZE) 80#endif 81 82#define PTHREAD_CREATE_DETACHED 0x00000001 83#define PTHREAD_CREATE_JOINABLE 0x00000000 84 85#define PTHREAD_PROCESS_PRIVATE 0 86#define PTHREAD_PROCESS_SHARED 1 87 88#define PTHREAD_SCOPE_SYSTEM 0 89#define PTHREAD_SCOPE_PROCESS 1 90 91int pthread_atfork(void (*)(void), void (*)(void), void (*)(void)) __INTRODUCED_IN(12); 92 93int pthread_attr_destroy(pthread_attr_t* _Nonnull); 94int pthread_attr_getdetachstate(const pthread_attr_t* _Nonnull, int* _Nonnull); 95int pthread_attr_getguardsize(const pthread_attr_t* _Nonnull, size_t* _Nonnull); 96int pthread_attr_getschedparam(const pthread_attr_t* _Nonnull, struct sched_param* _Nonnull); 97int pthread_attr_getschedpolicy(const pthread_attr_t* _Nonnull, int* _Nonnull); 98int pthread_attr_getscope(const pthread_attr_t* _Nonnull, int* _Nonnull); 99int pthread_attr_getstack(const pthread_attr_t* _Nonnull, void** _Nonnull, size_t* _Nonnull); 100int pthread_attr_getstacksize(const pthread_attr_t* _Nonnull, size_t* _Nonnull); 101int pthread_attr_init(pthread_attr_t* _Nonnull); 102int pthread_attr_setdetachstate(pthread_attr_t* _Nonnull, int); 103int pthread_attr_setguardsize(pthread_attr_t* _Nonnull, size_t); 104int pthread_attr_setschedparam(pthread_attr_t* _Nonnull, const struct sched_param* _Nonnull); 105int pthread_attr_setschedpolicy(pthread_attr_t* _Nonnull, int); 106int pthread_attr_setscope(pthread_attr_t* _Nonnull, int); 107int pthread_attr_setstack(pthread_attr_t* _Nonnull, void*, size_t); 108int pthread_attr_setstacksize(pthread_attr_t* _Nonnull, size_t); 109 110int pthread_condattr_destroy(pthread_condattr_t* _Nonnull); 111int pthread_condattr_getclock(const pthread_condattr_t* _Nonnull, clockid_t* _Nonnull) 112 __INTRODUCED_IN(21); 113int pthread_condattr_getpshared(const pthread_condattr_t* _Nonnull, int* _Nonnull); 114int pthread_condattr_init(pthread_condattr_t* _Nonnull); 115int pthread_condattr_setclock(pthread_condattr_t* _Nonnull, clockid_t) __INTRODUCED_IN(21); 116int pthread_condattr_setpshared(pthread_condattr_t* _Nonnull, int); 117 118int pthread_cond_broadcast(pthread_cond_t* _Nonnull); 119int pthread_cond_destroy(pthread_cond_t* _Nonnull); 120int pthread_cond_init(pthread_cond_t* _Nonnull, const pthread_condattr_t*); 121int pthread_cond_signal(pthread_cond_t* _Nonnull); 122int pthread_cond_timedwait(pthread_cond_t* _Nonnull, pthread_mutex_t* _Nonnull, 123 const struct timespec* _Nonnull); 124int pthread_cond_wait(pthread_cond_t* _Nonnull, pthread_mutex_t* _Nonnull); 125 126int pthread_create(pthread_t* _Nonnull, pthread_attr_t const*, 127 void* (* _Nonnull start_routine)(void*), void*); 128int pthread_detach(pthread_t); 129void pthread_exit(void*) __noreturn; 130 131int pthread_equal(pthread_t, pthread_t); 132 133int pthread_getattr_np(pthread_t, pthread_attr_t* _Nonnull); 134 135int pthread_getcpuclockid(pthread_t, clockid_t* _Nonnull); 136 137int pthread_getschedparam(pthread_t, int* _Nonnull, struct sched_param* _Nonnull); 138 139void* pthread_getspecific(pthread_key_t); 140 141pid_t pthread_gettid_np(pthread_t) __INTRODUCED_IN(21); 142 143int pthread_join(pthread_t, void**); 144 145int pthread_key_create(pthread_key_t* _Nonnull, void (*)(void*)); 146int pthread_key_delete(pthread_key_t); 147 148int pthread_mutexattr_destroy(pthread_mutexattr_t* _Nonnull); 149int pthread_mutexattr_getpshared(const pthread_mutexattr_t* _Nonnull, int* _Nonnull); 150int pthread_mutexattr_gettype(const pthread_mutexattr_t* _Nonnull, int* _Nonnull); 151int pthread_mutexattr_init(pthread_mutexattr_t* _Nonnull); 152int pthread_mutexattr_setpshared(pthread_mutexattr_t* _Nonnull, int); 153int pthread_mutexattr_settype(pthread_mutexattr_t* _Nonnull, int); 154 155int pthread_mutex_destroy(pthread_mutex_t* _Nonnull); 156int pthread_mutex_init(pthread_mutex_t* _Nonnull, const pthread_mutexattr_t*); 157int pthread_mutex_lock(pthread_mutex_t* _Nonnull); 158int pthread_mutex_timedlock(pthread_mutex_t* _Nonnull, const struct timespec* _Nonnull) 159 __INTRODUCED_IN(21); 160int pthread_mutex_trylock(pthread_mutex_t* _Nonnull); 161int pthread_mutex_unlock(pthread_mutex_t* _Nonnull); 162 163#if defined(__LP32__) && __ANDROID_API__ < 21 164/* 165 * Cruft for supporting old API levels. Pre-L we didn't have the proper POSIX 166 * APIs for things, but instead had some locally grown, artisan equivalents. 167 * Keep exposing the old prototypes on old API levels so we don't regress 168 * functionality. 169 * 170 * See the following bugs: 171 * * https://github.com/android-ndk/ndk/issues/420 172 * * https://github.com/android-ndk/ndk/issues/423 173 * * https://stackoverflow.com/q/44580542/632035 174 */ 175 176int pthread_mutex_lock_timeout_np(pthread_mutex_t* mutex, unsigned msecs); 177int pthread_cond_timeout_np(pthread_cond_t* cond, pthread_mutex_t* mutex, unsigned msecs); 178int pthread_cond_timedwait_monotonic_np(pthread_cond_t*, pthread_mutex_t*, const struct timespec*); 179int pthread_cond_timedwait_relative_np(pthread_cond_t* cond, pthread_mutex_t* mutex, 180 const struct timespec* reltime); 181#endif 182 183int pthread_once(pthread_once_t* _Nonnull, void (* _Nonnull init_routine)(void)); 184 185int pthread_rwlockattr_init(pthread_rwlockattr_t* _Nonnull); 186int pthread_rwlockattr_destroy(pthread_rwlockattr_t* _Nonnull); 187int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t* _Nonnull, int* _Nonnull); 188int pthread_rwlockattr_setpshared(pthread_rwlockattr_t* _Nonnull, int); 189int pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t* _Nonnull, int* _Nonnull) 190 __INTRODUCED_IN(23); 191int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t* _Nonnull, int) __INTRODUCED_IN(23); 192 193int pthread_rwlock_destroy(pthread_rwlock_t* _Nonnull); 194int pthread_rwlock_init(pthread_rwlock_t* _Nonnull, const pthread_rwlockattr_t*); 195int pthread_rwlock_rdlock(pthread_rwlock_t* _Nonnull); 196int pthread_rwlock_timedrdlock(pthread_rwlock_t* _Nonnull, const struct timespec* _Nonnull); 197int pthread_rwlock_timedwrlock(pthread_rwlock_t* _Nonnull, const struct timespec* _Nonnull); 198int pthread_rwlock_tryrdlock(pthread_rwlock_t* _Nonnull); 199int pthread_rwlock_trywrlock(pthread_rwlock_t* _Nonnull); 200int pthread_rwlock_unlock(pthread_rwlock_t* _Nonnull); 201int pthread_rwlock_wrlock(pthread_rwlock_t* _Nonnull); 202 203#if __ANDROID_API__ >= __ANDROID_API_N__ 204int pthread_barrierattr_init(pthread_barrierattr_t* _Nonnull attr) __INTRODUCED_IN(24); 205int pthread_barrierattr_destroy(pthread_barrierattr_t* _Nonnull attr) __INTRODUCED_IN(24); 206int pthread_barrierattr_getpshared(const pthread_barrierattr_t* _Nonnull attr, 207 int* _Nonnull pshared) __INTRODUCED_IN(24); 208int pthread_barrierattr_setpshared(pthread_barrierattr_t* _Nonnull attr, int pshared) 209 __INTRODUCED_IN(24); 210#endif 211 212#if __ANDROID_API__ >= __ANDROID_API_N__ 213int pthread_barrier_init(pthread_barrier_t* _Nonnull, const pthread_barrierattr_t*, unsigned) 214 __INTRODUCED_IN(24); 215int pthread_barrier_destroy(pthread_barrier_t* _Nonnull) __INTRODUCED_IN(24); 216int pthread_barrier_wait(pthread_barrier_t* _Nonnull) __INTRODUCED_IN(24); 217#endif 218 219#if __ANDROID_API__ >= __ANDROID_API_N__ 220int pthread_spin_destroy(pthread_spinlock_t* _Nonnull) __INTRODUCED_IN(24); 221int pthread_spin_init(pthread_spinlock_t* _Nonnull, int) __INTRODUCED_IN(24); 222int pthread_spin_lock(pthread_spinlock_t* _Nonnull) __INTRODUCED_IN(24); 223int pthread_spin_trylock(pthread_spinlock_t* _Nonnull) __INTRODUCED_IN(24); 224int pthread_spin_unlock(pthread_spinlock_t* _Nonnull) __INTRODUCED_IN(24); 225#endif 226 227pthread_t pthread_self(void) __attribute_const__; 228 229#if defined(__USE_GNU) 230int pthread_getname_np(pthread_t, char* _Nonnull, size_t) __INTRODUCED_IN(26); 231#endif 232/* TODO: this should be __USE_GNU too. */ 233int pthread_setname_np(pthread_t, const char* _Nonnull); 234 235int pthread_setschedparam(pthread_t, int, const struct sched_param* _Nonnull); 236 237int pthread_setspecific(pthread_key_t, const void*); 238 239typedef void (*__pthread_cleanup_func_t)(void*); 240 241typedef struct __pthread_cleanup_t { 242 struct __pthread_cleanup_t* __cleanup_prev; 243 __pthread_cleanup_func_t __cleanup_routine; 244 void* __cleanup_arg; 245} __pthread_cleanup_t; 246 247void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t, void*); 248void __pthread_cleanup_pop(__pthread_cleanup_t*, int); 249 250/* Believe or not, the definitions of pthread_cleanup_push and 251 * pthread_cleanup_pop below are correct. Posix states that these 252 * can be implemented as macros that might introduce opening and 253 * closing braces, and that using setjmp/longjmp/return/break/continue 254 * between them results in undefined behavior. 255 */ 256#define pthread_cleanup_push(routine, arg) \ 257 do { \ 258 __pthread_cleanup_t __cleanup; \ 259 __pthread_cleanup_push( &__cleanup, (routine), (arg) ); \ 260 261#define pthread_cleanup_pop(execute) \ 262 __pthread_cleanup_pop( &__cleanup, (execute)); \ 263 } while (0); \ 264 265#if defined(__clang__) 266#pragma clang diagnostic pop 267#endif 268 269__END_DECLS 270 271#endif /* _PTHREAD_H_ */ 272