--- IO-AIO/xthread.h 2007/07/08 11:12:15 1.4 +++ IO-AIO/xthread.h 2007/07/08 13:43:29 1.5 @@ -20,9 +20,11 @@ #include //D #include #include +#include #include #include #include +#include #define sigset_t int #define sigfillset(a) #define pthread_sigmask(a,b,c) @@ -30,51 +32,32 @@ #define sigemptyset(s) #define sigfillset(s) -#define pthread_kill(a,b) -#define pthread_self() 0 +typedef pthread_mutex_t mutex_t; +#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER +#define X_MUTEX_CHECK(mutex) +#define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) +#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) + +typedef pthread_cond_t cond_t; +#define X_COND_INIT PTHREAD_COND_INITIALIZER +#define X_COND_CHECK(cond) +#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) +#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) +#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) -typedef HANDLE mutex_t; -#define X_MUTEX_INIT 0 -#define X_MUTEX_CHECK(mutex) if (!(mutex)) (mutex) = CreateMutex (NULL, FALSE, NULL) -#define X_LOCK(mutex) WaitForSingleObject ((mutex), INFINITE) -#define X_UNLOCK(mutex) ReleaseMutex (mutex) - -typedef HANDLE cond_t; -#define X_COND_INIT 0 -#define X_COND_CHECK(cond) if (!(cond)) (cond) = CreateEvent (NULL, FALSE, FALSE, NULL) -#define X_COND_SIGNAL(cond) SetEvent (cond) -#define X_COND_WAIT(cond,mutex) do { SignalObjectAndWait ((mutex), (cond),INFINITE, FALSE); WaitForSingleObject ((mutex), INFINITE); } while (0) - -#define ETIMEDOUT 1 - -struct timespec { - unsigned long tv_sec, tv_nsec; -}; +typedef pthread_t thread_t; +#define X_THREAD_PROC(name) void *name (void *thr_arg) +#define X_THREAD_ATFORK(a,b,c) static int -X_COND_TIMEDWAIT (mutex_t mutex, cond_t cond, struct timespec to) +thread_create (thread_t *tid, void *(*proc)(void *), void *arg) { - unsigned long ms = to.tv_nsec / 1000 + to.tv_sec * 1000; - - if (SignalObjectAndWait (mutex, cond, ms, FALSE) == WAIT_TIMEOUT) - return ETIMEDOUT; - - if (WaitForSingleObject (mutex, ms) == WAIT_TIMEOUT) - return ETIMEDOUT; - - return 0; -} + pthread_attr_t attr; -typedef DWORD thread_t; -#define X_THREAD_PROC(name) DWORD WINAPI name (LPVOID thr_arg) -#define X_THREAD_ATFORK(a,b,c) + pthread_attr_init (&attr); + pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); -static int -thread_create (thread_t *tid, LPTHREAD_START_ROUTINE proc, void *arg) -{ - *tid = 0; - CreateThread (0, 4096, proc, arg, 0, tid); - return !!*tid; + return pthread_create (tid, &attr, proc, arg) == 0; } #define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0)