--- BDB/BDB.xs 2007/03/05 19:47:01 1.10 +++ BDB/BDB.xs 2007/05/09 06:42:24 1.11 @@ -1,12 +1,4 @@ -/* solaris */ -#define _POSIX_PTHREAD_SEMANTICS 1 - -#if __linux && !defined(_GNU_SOURCE) -# define _GNU_SOURCE -#endif - -/* just in case */ -#define _REENTRANT 1 +#include "xthread.h" #include @@ -14,8 +6,6 @@ #include "perl.h" #include "XSUB.h" -#include - #include #include #include @@ -34,18 +24,6 @@ /* number of seconds after which idle threads exit */ #define IDLE_TIMEOUT 10 -/* 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 DB_ENV DB_ENV_ornull; typedef DB_TXN DB_TXN_ornull; typedef DBC DBC_ornull; @@ -149,23 +127,14 @@ static unsigned int started, idle, wanted; -#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) -# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP -#else -# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER -#endif - -#define LOCK(mutex) pthread_mutex_lock (&(mutex)) -#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) - /* worker threads management */ -static pthread_mutex_t wrklock = AIO_MUTEX_INIT; +static mutex_t wrklock = MUTEX_INIT; typedef struct worker { /* locked by wrklock */ struct worker *prev, *next; - pthread_t tid; + thread_t tid; /* locked by reslock, reqlock or wrklock */ aio_req req; /* currently processed request */ @@ -192,9 +161,9 @@ static volatile unsigned int max_outstanding = 0xffffffff; static int respipe [2]; -static pthread_mutex_t reslock = AIO_MUTEX_INIT; -static pthread_mutex_t reqlock = AIO_MUTEX_INIT; -static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; +static mutex_t reslock = MUTEX_INIT; +static mutex_t reqlock = MUTEX_INIT; +static cond_t reqwait = COND_INIT; #if WORDACCESS_UNSAFE @@ -375,26 +344,13 @@ static void start_thread (void) { - sigset_t fullsigset, oldsigset; - pthread_attr_t attr; - worker *wrk = calloc (1, sizeof (worker)); if (!wrk) croak ("unable to allocate worker thread data"); - 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); - LOCK (wrklock); - pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); - - if (pthread_create (&wrk->tid, &attr, aio_proc, (void *)wrk) == 0) + if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) { wrk->prev = &wrk_first; wrk->next = wrk_first.next; @@ -405,7 +361,6 @@ else free (wrk); - pthread_sigmask (SIG_SETMASK, &oldsigset, 0); UNLOCK (wrklock); } @@ -447,7 +402,7 @@ LOCK (reqlock); ++nready; reqq_push (&req_queue, req); - pthread_cond_signal (&reqwait); + COND_SIGNAL (reqwait); UNLOCK (reqlock); maybe_start_thread (); @@ -473,7 +428,7 @@ LOCK (reqlock); reqq_push (&req_queue, req); - pthread_cond_signal (&reqwait); + COND_SIGNAL (reqwait); UNLOCK (reqlock); LOCK (wrklock); @@ -639,7 +594,7 @@ ++idle; - if (pthread_cond_timedwait (&reqwait, &reqlock, &ts) + if (COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) { if (idle > max_idle) @@ -653,7 +608,7 @@ } /* we are allowed to idle, so do so without any timeout */ - pthread_cond_wait (&reqwait, &reqlock); + COND_WAIT (reqwait, reqlock); ts.tv_sec = time (0) + IDLE_TIMEOUT; } @@ -1047,7 +1002,7 @@ newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); create_pipe (); - pthread_atfork (atfork_prepare, atfork_parent, atfork_child); + ATFORK (atfork_prepare, atfork_parent, atfork_child); } void