pthread.h revision 4e468ed2eb86a2406e14f1eca82072ee501d05fd
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_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/* 101 * Prototypes 102 */ 103#if __cplusplus 104extern "C" { 105#endif 106 107int pthread_attr_init(pthread_attr_t * attr); 108int pthread_attr_destroy(pthread_attr_t * attr); 109 110int pthread_attr_setdetachstate(pthread_attr_t * attr, int state); 111int pthread_attr_getdetachstate(pthread_attr_t const * attr, int * state); 112 113int pthread_attr_setschedpolicy(pthread_attr_t * attr, int policy); 114int pthread_attr_getschedpolicy(pthread_attr_t const * attr, int * policy); 115 116int pthread_attr_setschedparam(pthread_attr_t * attr, struct sched_param const * param); 117int pthread_attr_getschedparam(pthread_attr_t const * attr, struct sched_param * param); 118 119int pthread_attr_setstacksize(pthread_attr_t * attr, size_t stack_size); 120int pthread_attr_getstacksize(pthread_attr_t const * attr, size_t * stack_size); 121 122int pthread_attr_setstackaddr(pthread_attr_t * attr, void * stackaddr); 123int pthread_attr_getstackaddr(pthread_attr_t const * attr, void ** stackaddr); 124 125int pthread_attr_setstack(pthread_attr_t * attr, void * stackaddr, size_t stack_size); 126int pthread_attr_getstack(pthread_attr_t const * attr, void ** stackaddr, size_t * stack_size); 127 128int pthread_attr_setguardsize(pthread_attr_t * attr, size_t guard_size); 129int pthread_attr_getguardsize(pthread_attr_t const * attr, size_t * guard_size); 130 131int pthread_getattr_np(pthread_t thid, pthread_attr_t * attr); 132 133int pthread_create(pthread_t *thread, pthread_attr_t const * attr, 134 void *(*start_routine)(void *), void * arg); 135void pthread_exit(void * retval); 136int pthread_join(pthread_t thid, void ** ret_val); 137int pthread_detach(pthread_t thid); 138 139pthread_t pthread_self(void); 140int pthread_equal(pthread_t one, pthread_t two); 141 142int pthread_getschedparam(pthread_t thid, int * policy, 143 struct sched_param * param); 144int pthread_setschedparam(pthread_t thid, int poilcy, 145 struct sched_param const * param); 146 147int pthread_mutexattr_init(pthread_mutexattr_t *attr); 148int pthread_mutexattr_destroy(pthread_mutexattr_t *attr); 149int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type); 150int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type); 151int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared); 152int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared); 153 154int pthread_mutex_init(pthread_mutex_t *mutex, 155 const pthread_mutexattr_t *attr); 156int pthread_mutex_destroy(pthread_mutex_t *mutex); 157int pthread_mutex_lock(pthread_mutex_t *mutex); 158int pthread_mutex_unlock(pthread_mutex_t *mutex); 159int pthread_mutex_trylock(pthread_mutex_t *mutex); 160int pthread_mutex_timedlock(pthread_mutex_t *mutex, struct timespec* ts); 161 162int pthread_cond_init(pthread_cond_t *cond, 163 const pthread_condattr_t *attr); 164int pthread_cond_destroy(pthread_cond_t *cond); 165int pthread_cond_broadcast(pthread_cond_t *cond); 166int pthread_cond_signal(pthread_cond_t *cond); 167int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); 168int pthread_cond_timedwait(pthread_cond_t *cond, 169 pthread_mutex_t * mutex, 170 const struct timespec *abstime); 171 172/* BIONIC: same as pthread_cond_timedwait, except the 'abstime' given refers 173 * to the CLOCK_MONOTONIC clock instead, to avoid any problems when 174 * the wall-clock time is changed brutally 175 */ 176int pthread_cond_timedwait_monotonic(pthread_cond_t *cond, 177 pthread_mutex_t *mutex, 178 const struct timespec *abstime); 179 180int pthread_cond_timeout_np(pthread_cond_t *cond, 181 pthread_mutex_t * mutex, 182 unsigned msecs); 183 184int pthread_key_create(pthread_key_t *key, void (*destructor_function)(void *)); 185int pthread_key_delete (pthread_key_t); 186int pthread_setspecific(pthread_key_t key, const void *value); 187void *pthread_getspecific(pthread_key_t key); 188 189int pthread_kill(pthread_t tid, int sig); 190int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset); 191 192int pthread_getcpuclockid(pthread_t tid, clockid_t *clockid); 193 194int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)); 195 196typedef void (*__pthread_cleanup_func_t)(void*); 197 198typedef struct __pthread_cleanup_t { 199 struct __pthread_cleanup_t* __cleanup_prev; 200 __pthread_cleanup_func_t __cleanup_routine; 201 void* __cleanup_arg; 202} __pthread_cleanup_t; 203 204extern void __pthread_cleanup_push(__pthread_cleanup_t* c, 205 __pthread_cleanup_func_t routine, 206 void* arg); 207 208extern void __pthread_cleanup_pop(__pthread_cleanup_t* c, 209 int execute); 210 211/* Believe or not, the definitions of pthread_cleanup_push and 212 * pthread_cleanup_pop below are correct. Posix states that these 213 * can be implemented as macros that might introduce opening and 214 * closing braces, and that using setjmp/longjmp/return/break/continue 215 * between them results in undefined behaviour. 216 * 217 * And indeed, GLibc and other C libraries use a similar definition 218 */ 219#define pthread_cleanup_push(routine, arg) \ 220 do { \ 221 __pthread_cleanup_t __cleanup; \ 222 __pthread_cleanup_push( &__cleanup, (routine), (arg) ); \ 223 224#define pthread_cleanup_pop(execute) \ 225 __pthread_cleanup_pop( &__cleanup, (execute)); \ 226 } while (0); 227 228#if __cplusplus 229} /* extern "C" */ 230#endif 231 232/************ TO FIX ************/ 233 234#define LONG_LONG_MAX __LONG_LONG_MAX__ 235#define LONG_LONG_MIN (-__LONG_LONG_MAX__ - 1) 236 237#endif // _PTHREAD_H_ 238