1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#ifdef _WIN32
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#endif
#include <windows.h>
#define THR_TYPE DWORD WINAPI
static void *__thread_create(DWORD (WINAPI *func)(PVOID),void *arg)
{ return (void *)CreateThread(NULL,64*1024,func,arg,0,NULL); }
static void __thread_yield(void) { (void)SwitchToThread(); }
static int __thread_wait(void *h)
{ DWORD ret;
if (WaitForSingleObject(h,INFINITE)==WAIT_OBJECT_0 &&
GetExitCodeThread(h,&ret))
{ CloseHandle(h);
return ret;
}
return -1;
}
static void *__semaphore_create(int max){ return CreateSemaphore(NULL,0,max,NULL); }
static int __semaphore_wait(void *h) { return WaitForSingleObject(h,INFINITE)!=WAIT_FAILED; }
static int __semaphore_post(void *h) { return ReleaseSemaphore(h,1,NULL); }
static void __semaphore_close(void *h) { CloseHandle(h); }
#else
#include <pthread.h>
#include <sched.h>
#include <semaphore.h>
#include <stdlib.h>
#include <limits.h>
#define THR_TYPE int
static void *__thread_create(int (*func)(void *),void *arg)
{ pthread_t h;
pthread_attr_t attr;
size_t stack_sz=16*1024;
#ifdef PTHREAD_STACK_MIN
stack_sz += PTHREAD_STACK_MIN;
#endif
if (pthread_attr_init(&attr)==0 &&
pthread_attr_setstacksize(&attr,stack_sz)==0 &&
pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM)==0 &&
pthread_create(&h,&attr,(void *(*)(void *))func,arg)==0 )
return (void *)h;
return NULL;
}
static void __thread_yield(void) { (void)sched_yield(); }
static int __thread_wait(void *h)
{ void *ret;
if (pthread_join((pthread_t)((size_t)h),&ret)==0)
return (int)ret;
return -1;
}
#if defined(__APPLE__) && defined(__MACH__)
// Mac OS X doesn't implement unnamed semaphores, g-r-r-r-r...
static void *__semaphore_create(int max)
{ sem_t *h = sem_open ("growisofs",O_CREAT|O_EXCL,0700,0);
if (h==(sem_t*)SEM_FAILED) return NULL;
sem_unlink ("growisofs");
return h;
}
static void __semaphore_close(void *h) { sem_close(h); }
#else
static void *__semaphore_create(int max)
{ sem_t *h = calloc(1,sizeof(sem_t));
if (h==NULL) return h;
if (sem_init(h,0,0)<0) { free(h); return NULL; }
return h;
}
static void __semaphore_close(void *h) { sem_destroy(h), free(h); }
#endif
static int __semaphore_wait(void *h) { return sem_wait(h)==0; }
static int __semaphore_post(void *h) { return sem_post(h)==0; }
#endif
|