pthread.h revision b27a840f4b520bfa095db99b0a2e5205634b0003
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 <time.h> 33#include <signal.h> 34#include <sched.h> 35#include <limits.h> 36#include <sys/types.h> 37 38#if defined(__LP64__) 39 #define __RESERVED_INITIALIZER , {0} 40#else 41 #define __RESERVED_INITIALIZER 42#endif 43 44typedef struct { 45 int volatile value; 46#ifdef __LP64__ 47 char __reserved[36]; 48#endif 49} pthread_mutex_t; 50 51#define __PTHREAD_MUTEX_INIT_VALUE 0 52#define __PTHREAD_RECURSIVE_MUTEX_INIT_VALUE 0x4000 53#define __PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE 0x8000 54 55#define PTHREAD_MUTEX_INITIALIZER {__PTHREAD_MUTEX_INIT_VALUE __RESERVED_INITIALIZER} 56#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER {__PTHREAD_RECURSIVE_MUTEX_INIT_VALUE __RESERVED_INITIALIZER} 57#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER {__PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE __RESERVED_INITIALIZER} 58 59enum { 60 PTHREAD_MUTEX_NORMAL = 0, 61 PTHREAD_MUTEX_RECURSIVE = 1, 62 PTHREAD_MUTEX_ERRORCHECK = 2, 63 64 PTHREAD_MUTEX_ERRORCHECK_NP = PTHREAD_MUTEX_ERRORCHECK, 65 PTHREAD_MUTEX_RECURSIVE_NP = PTHREAD_MUTEX_RECURSIVE, 66 67 PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL 68}; 69 70typedef struct { 71 int volatile value; 72#ifdef __LP64__ 73 char __reserved[44]; 74#endif 75} pthread_cond_t; 76 77#define PTHREAD_COND_INITIALIZER {0 __RESERVED_INITIALIZER} 78 79typedef struct { 80 uint32_t flags; 81 void* stack_base; 82 size_t stack_size; 83 size_t guard_size; 84 int32_t sched_policy; 85 int32_t sched_priority; 86#ifdef __LP64__ 87 char __reserved[16]; 88#endif 89} pthread_attr_t; 90 91typedef long pthread_mutexattr_t; 92typedef long pthread_condattr_t; 93 94typedef long pthread_rwlockattr_t; 95 96typedef struct { 97#if !defined(__LP64__) 98 pthread_mutex_t __unused_lock; 99 pthread_cond_t __unused_cond; 100#endif 101 volatile int32_t state; // 0=unlock, -1=writer lock, +n=reader lock 102 volatile int32_t writer_thread_id; 103 volatile int32_t pending_readers; 104 volatile int32_t pending_writers; 105 int32_t attr; 106#ifdef __LP64__ 107 char __reserved[36]; 108#else 109 char __reserved[12]; 110#endif 111 112} pthread_rwlock_t; 113 114#ifdef __LP64__ 115 #define PTHREAD_RWLOCK_INITIALIZER { 0, 0, 0, 0, 0, { 0 } } 116#else 117 #define PTHREAD_RWLOCK_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0, 0, 0, 0, { 0 } } 118#endif 119 120typedef int pthread_key_t; 121typedef long pthread_t; 122 123typedef volatile int pthread_once_t; 124 125#define PTHREAD_ONCE_INIT 0 126 127#if defined(__LP64__) 128#define PTHREAD_STACK_MIN (4 * PAGE_SIZE) 129#else 130#define PTHREAD_STACK_MIN (2 * PAGE_SIZE) 131#endif 132 133#define PTHREAD_CREATE_DETACHED 0x00000001 134#define PTHREAD_CREATE_JOINABLE 0x00000000 135 136#define PTHREAD_PROCESS_PRIVATE 0 137#define PTHREAD_PROCESS_SHARED 1 138 139#define PTHREAD_SCOPE_SYSTEM 0 140#define PTHREAD_SCOPE_PROCESS 1 141 142__BEGIN_DECLS 143 144int pthread_atfork(void (*)(void), void (*)(void), void(*)(void)); 145 146int pthread_attr_destroy(pthread_attr_t*) __nonnull((1)); 147int pthread_attr_getdetachstate(const pthread_attr_t*, int*) __nonnull((1, 2)); 148int pthread_attr_getguardsize(const pthread_attr_t*, size_t*) __nonnull((1, 2)); 149int pthread_attr_getschedparam(const pthread_attr_t*, struct sched_param*) __nonnull((1, 2)); 150int pthread_attr_getschedpolicy(const pthread_attr_t*, int*) __nonnull((1, 2)); 151int pthread_attr_getscope(const pthread_attr_t*, int*) __nonnull((1, 2)); 152int pthread_attr_getstack(const pthread_attr_t*, void**, size_t*) __nonnull((1, 2, 3)); 153int pthread_attr_getstacksize(const pthread_attr_t*, size_t*) __nonnull((1, 2)); 154int pthread_attr_init(pthread_attr_t*) __nonnull((1)); 155int pthread_attr_setdetachstate(pthread_attr_t*, int) __nonnull((1)); 156int pthread_attr_setguardsize(pthread_attr_t*, size_t) __nonnull((1)); 157int pthread_attr_setschedparam(pthread_attr_t*, const struct sched_param*) __nonnull((1, 2)); 158int pthread_attr_setschedpolicy(pthread_attr_t*, int) __nonnull((1)); 159int pthread_attr_setscope(pthread_attr_t*, int) __nonnull((1)); 160int pthread_attr_setstack(pthread_attr_t*, void*, size_t) __nonnull((1)); 161int pthread_attr_setstacksize(pthread_attr_t*, size_t stack_size) __nonnull((1)); 162 163int pthread_condattr_destroy(pthread_condattr_t*) __nonnull((1)); 164int pthread_condattr_getclock(const pthread_condattr_t*, clockid_t*) __nonnull((1, 2)); 165int pthread_condattr_getpshared(const pthread_condattr_t*, int*) __nonnull((1, 2)); 166int pthread_condattr_init(pthread_condattr_t*) __nonnull((1)); 167int pthread_condattr_setclock(pthread_condattr_t*, clockid_t) __nonnull((1)); 168int pthread_condattr_setpshared(pthread_condattr_t*, int) __nonnull((1)); 169 170int pthread_cond_broadcast(pthread_cond_t*) __nonnull((1)); 171int pthread_cond_destroy(pthread_cond_t*) __nonnull((1)); 172int pthread_cond_init(pthread_cond_t*, const pthread_condattr_t*) __nonnull((1)); 173int pthread_cond_signal(pthread_cond_t*) __nonnull((1)); 174int pthread_cond_timedwait(pthread_cond_t*, pthread_mutex_t*, const struct timespec*) __nonnull((1, 2, 3)); 175int pthread_cond_wait(pthread_cond_t*, pthread_mutex_t*) __nonnull((1, 2)); 176 177int pthread_create(pthread_t*, pthread_attr_t const*, void *(*)(void*), void*) __nonnull((1, 3)); 178int pthread_detach(pthread_t); 179void pthread_exit(void*) __noreturn; 180 181int pthread_equal(pthread_t, pthread_t); 182 183int pthread_getattr_np(pthread_t, pthread_attr_t*) __nonnull((2)); 184 185int pthread_getcpuclockid(pthread_t, clockid_t*) __nonnull((2)); 186 187int pthread_getschedparam(pthread_t, int*, struct sched_param*) __nonnull((2, 3)); 188 189void* pthread_getspecific(pthread_key_t); 190 191int pthread_join(pthread_t, void**); 192 193int pthread_key_create(pthread_key_t*, void (*)(void*)) __nonnull((1)); 194int pthread_key_delete(pthread_key_t); 195 196int pthread_kill(pthread_t, int); 197 198int pthread_mutexattr_destroy(pthread_mutexattr_t*) __nonnull((1)); 199int pthread_mutexattr_getpshared(const pthread_mutexattr_t*, int*) __nonnull((1, 2)); 200int pthread_mutexattr_gettype(const pthread_mutexattr_t*, int*) __nonnull((1, 2)); 201int pthread_mutexattr_init(pthread_mutexattr_t*) __nonnull((1)); 202int pthread_mutexattr_setpshared(pthread_mutexattr_t*, int) __nonnull((1)); 203int pthread_mutexattr_settype(pthread_mutexattr_t*, int) __nonnull((1)); 204 205int pthread_mutex_destroy(pthread_mutex_t*) __nonnull((1)); 206int pthread_mutex_init(pthread_mutex_t*, const pthread_mutexattr_t*) __nonnull((1)); 207int pthread_mutex_lock(pthread_mutex_t*) __nonnull((1)); 208int pthread_mutex_timedlock(pthread_mutex_t*, const struct timespec*) __nonnull((1, 2)); 209int pthread_mutex_trylock(pthread_mutex_t*) __nonnull((1)); 210int pthread_mutex_unlock(pthread_mutex_t*) __nonnull((1)); 211 212int pthread_once(pthread_once_t*, void (*)(void)) __nonnull((1, 2)); 213 214int pthread_rwlockattr_destroy(pthread_rwlockattr_t*) __nonnull((1)); 215int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t*, int*) __nonnull((1, 2)); 216int pthread_rwlockattr_init(pthread_rwlockattr_t*) __nonnull((1)); 217int pthread_rwlockattr_setpshared(pthread_rwlockattr_t*, int) __nonnull((1)); 218 219int pthread_rwlock_destroy(pthread_rwlock_t*) __nonnull((1)); 220int pthread_rwlock_init(pthread_rwlock_t*, const pthread_rwlockattr_t*) __nonnull((1)); 221int pthread_rwlock_rdlock(pthread_rwlock_t*) __nonnull((1)); 222int pthread_rwlock_timedrdlock(pthread_rwlock_t*, const struct timespec*) __nonnull((1, 2)); 223int pthread_rwlock_timedwrlock(pthread_rwlock_t*, const struct timespec*) __nonnull((1, 2)); 224int pthread_rwlock_tryrdlock(pthread_rwlock_t*) __nonnull((1)); 225int pthread_rwlock_trywrlock(pthread_rwlock_t*) __nonnull((1)); 226int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) __nonnull((1)); 227int pthread_rwlock_wrlock(pthread_rwlock_t*) __nonnull((1)); 228 229pthread_t pthread_self(void) __pure2; 230 231int pthread_setname_np(pthread_t, const char*) __nonnull((2)); 232 233int pthread_setschedparam(pthread_t, int, const struct sched_param*) __nonnull((3)); 234 235int pthread_setspecific(pthread_key_t, const void*); 236 237int pthread_sigmask(int, const sigset_t*, sigset_t*); 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 247extern void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t, void*); 248extern void __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 266#if !defined(__LP64__) 267 268// Bionic additions that are deprecated even in the 32-bit ABI. 269// 270// TODO: Remove them once chromium_org / NFC have switched over. 271int pthread_cond_timedwait_monotonic_np(pthread_cond_t*, pthread_mutex_t*, const struct timespec*); 272int pthread_cond_timedwait_monotonic(pthread_cond_t*, pthread_mutex_t*, const struct timespec*); 273 274int pthread_cond_timedwait_relative_np(pthread_cond_t*, pthread_mutex_t*, const struct timespec*) /* TODO: __attribute__((deprecated("use pthread_cond_timedwait instead")))*/; 275#define HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE 1 /* TODO: stop defining this to push LP32 off this API sooner. */ 276int pthread_cond_timeout_np(pthread_cond_t*, pthread_mutex_t*, unsigned) /* TODO: __attribute__((deprecated("use pthread_cond_timedwait instead")))*/; 277 278int pthread_mutex_lock_timeout_np(pthread_mutex_t*, unsigned) __attribute__((deprecated("use pthread_mutex_timedlock instead"))); 279 280#endif /* !defined(__LP64__) */ 281 282__END_DECLS 283 284#endif /* _PTHREAD_H_ */ 285