/* solaris */ #define _POSIX_PTHREAD_SEMANTICS 1 #if __linux && !defined(_GNU_SOURCE) # define _GNU_SOURCE #endif /* just in case */ #define _REENTRANT 1 #include #include #ifndef PTHREAD_STACK_MIN /* care for broken platforms, e.g. windows */ # define PTHREAD_STACK_MIN 16384 #endif #if __ia64 # define STACKSIZE 65536 #elif __i386 || __x86_64 /* 16k is unreasonably high :( */ # define STACKSIZE PTHREAD_STACK_MIN #else # define STACKSIZE 16384 #endif /* wether word reads are potentially non-atomic. * this is conservatice, likely most arches this runs * on have atomic word read/writes. */ #ifndef WORDACCESS_UNSAFE # if __i386 || __x86_64 # define WORDACCESS_UNSAFE 0 # else # define WORDACCESS_UNSAFE 1 # endif #endif typedef pthread_mutex_t mutex_t; #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) # define MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP #else # define MUTEX_INIT PTHREAD_MUTEX_INITIALIZER #endif typedef pthread_cond_t cond_t; #define COND_INIT PTHREAD_COND_INITIALIZER #define LOCK(mutex) pthread_mutex_lock (&(mutex)) #define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) #define COND_SIGNAL(cond) pthread_cond_signal (&(cond)) #define COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) #define COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) typedef pthread_t thread_t; static int thread_create (thread_t *tid, void *(*proc)(void *), void *arg) { int retval; sigset_t fullsigset, oldsigset; pthread_attr_t attr; pthread_attr_init (&attr); pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); #ifdef PTHREAD_SCOPE_PROCESS pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); #endif sigfillset (&fullsigset); pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); retval = pthread_create (tid, &attr, proc, arg) == 0; pthread_sigmask (SIG_SETMASK, &oldsigset, 0); return retval; } #define ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child)