xdasd_thread.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00039 #ifndef XDASD_THREAD_H_INCLUDED
00040 #define XDASD_THREAD_H_INCLUDED
00041
00047 #ifdef _WIN32
00048 # define WIN32_LEAN_AND_MEAN
00049 # define _WIN32_WINNT 0x0400
00050 # pragma warning(disable: 4201)
00051 # include <windows.h>
00052
00053
00054 # define mutex_t CRITICAL_SECTION
00055 # define mutex_create InitializeCriticalSection
00056 # define mutex_destroy DeleteCriticalSection
00057 # define mutex_acquire EnterCriticalSection
00058 # define mutex_release LeaveCriticalSection
00059
00060
00061 # define sem_t HANDLE
00062 # define sem_create(y) CreateSemaphore(0,(y),0x7FFFFFFFL,0)) == 0? -1: 0)
00063 # define sem_destroy (void)CloseHandle
00064 # define sem_wait(x) (void)WaitForSingleObject((x),INFINITE)
00065 # define sem_signal(x,y) (void)ReleaseSemaphore((x),(y),0)
00066
00067 #else
00068 # include <pthread.h>
00069
00070
00071 # define mutex_t pthread_mutex_t
00072 # define mutex_create(x) (void)pthread_mutex_init((x),0)
00073 # define mutex_destroy (void)pthread_mutex_destroy
00074 # define mutex_acquire (void)pthread_mutex_lock
00075 # define mutex_release (void)pthread_mutex_unlock
00076
00077
00078 # define cv_t pthread_cond_t
00079 # define cv_create(x) pthread_cond_init((x),0)
00080 # define cv_destroy pthread_cond_destroy
00081 # define cv_signal pthread_cond_signal
00082 # define cv_wait pthread_cond_wait
00083
00084 #endif
00085
00086 typedef void * thread_t;
00087
00088 thread_t thread_create(void *(*thread_proc)(void *), void * arg);
00089 void * thread_wait(thread_t th);
00090
00091 #ifdef _WIN32
00092
00093 typedef struct cv_t
00094 {
00095 long waitersBlocked;
00096 long waitersGone;
00097 long waitersToUnblock;
00098 HANDLE semBlockQueue;
00099 HANDLE semBlockLock;
00100 CRITICAL_SECTION csUnblockLock;
00101 } cv_t;
00102
00103 int cv_create(cv_t * cvp);
00104 void cv_destroy(cv_t * cvp);
00105 int cv_wait(cv_t * cvp, mutex_t * mp);
00106 int cv_signal(cv_t * cvp);
00107 int cv_broadcast(cv_t * cvp);
00108
00109 #else
00110
00111 typedef struct sem_t
00112 {
00113 pthread_mutex_t lock;
00114 pthread_cond_t cond;
00115 int count;
00116 } sem_t;
00117
00118 int sem_create(sem_t * sp, int count);
00119 void sem_destroy(sem_t * sp);
00120 void sem_wait(sem_t * sp);
00121 void sem_signal(sem_t * sp, int count);
00122
00123 #endif
00124
00128 #endif
00129