--- IO-AIO/AIO.xs 2006/11/26 22:47:58 1.95 +++ IO-AIO/AIO.xs 2007/05/09 06:45:12 1.98 @@ -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 @@ -16,8 +8,6 @@ #include "autoconf/config.h" -#include - #include #include #include @@ -54,31 +44,6 @@ # define NAME_MAX 4096 #endif -#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 - /* buffer size for various temporary buffers */ #define AIO_BUFSIZE 65536 @@ -99,7 +64,7 @@ REQ_SENDFILE, REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_FSYNC, REQ_FDATASYNC, - REQ_UNLINK, REQ_RMDIR, REQ_RENAME, + REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME, REQ_MKNOD, REQ_READDIR, REQ_LINK, REQ_SYMLINK, REQ_READLINK, REQ_GROUP, REQ_NOP, @@ -163,7 +128,7 @@ + ((tv2->tv_usec - tv1->tv_usec) >> 10); } -static pthread_t main_tid; +static thread_t main_tid; static int main_sig; static int block_sig_level; @@ -201,23 +166,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 */ @@ -255,9 +211,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 @@ -621,27 +577,14 @@ 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_setstacksize (&attr, STACKSIZE); - 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; @@ -652,7 +595,6 @@ else free (wrk); - pthread_sigmask (SIG_SETMASK, &oldsigset, 0); UNLOCK (wrklock); } @@ -677,7 +619,7 @@ LOCK (reqlock); ++nready; reqq_push (&req_queue, req); - pthread_cond_signal (&reqwait); + COND_SIGNAL (reqwait); UNLOCK (reqlock); unblock_sig (); @@ -696,7 +638,7 @@ LOCK (reqlock); reqq_push (&req_queue, req); - pthread_cond_signal (&reqwait); + COND_SIGNAL (reqwait); UNLOCK (reqlock); LOCK (wrklock); @@ -859,7 +801,7 @@ * normal read/write by using a mutex. slows down execution a lot, * but that's your problem, not mine. */ -static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; +static mutex_t preadwritelock = MUTEX_INIT; static ssize_t pread (int fd, void *buf, size_t count, off_t offset) { @@ -920,7 +862,7 @@ #if !HAVE_READDIR_R # define readdir_r aio_readdir_r -static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; +static mutex_t readdirlock = MUTEX_INIT; static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) { @@ -1129,7 +1071,7 @@ ++idle; - if (pthread_cond_timedwait (&reqwait, &reqlock, &ts) + if (COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) { if (idle > max_idle) @@ -1143,7 +1085,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; } @@ -1173,6 +1115,7 @@ case REQ_CLOSE: req->result = close (req->int1); break; case REQ_UNLINK: req->result = unlink (req->ptr1); break; case REQ_RMDIR: req->result = rmdir (req->ptr1); break; + case REQ_MKDIR: req->result = mkdir (req->ptr1, req->mode); break; case REQ_RENAME: req->result = rename (req->ptr2, req->ptr1); break; case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break; case REQ_SYMLINK: req->result = symlink (req->ptr2, req->ptr1); break; @@ -1335,7 +1278,7 @@ newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); create_pipe (); - pthread_atfork (atfork_prepare, atfork_parent, atfork_child); + ATFORK (atfork_prepare, atfork_parent, atfork_child); } void @@ -1448,7 +1391,7 @@ else { /* read: grow scalar as necessary */ - svptr = SvGROW (data, length + dataoffset); + svptr = SvGROW (data, length + dataoffset + 1); } if (length < 0) @@ -1598,6 +1541,23 @@ } void +aio_mkdir (pathname,mode,callback=&PL_sv_undef) + SV8 * pathname + UV mode + SV * callback + PPCODE: +{ + dREQ; + + req->type = REQ_MKDIR; + req->sv1 = newSVsv (pathname); + req->ptr1 = SvPVbyte_nolen (req->sv1); + req->mode = mode; + + REQ_SEND; +} + +void aio_link (oldpath,newpath,callback=&PL_sv_undef) SV8 * oldpath SV8 * newpath @@ -1622,9 +1582,9 @@ void aio_mknod (pathname,mode,dev,callback=&PL_sv_undef) SV8 * pathname - SV * callback UV mode UV dev + SV * callback PPCODE: { dREQ;