00001 #ifndef MTSYNC_HEADER
00002 #define MTSYNC_HEADER
00003
00004
00005 #if defined(__linux) || defined(__alpha)
00006 #ifndef _REENTRANT
00007 #error Macro _REENTRANT must be defined for POSIX threads!
00008 #endif
00009 #endif
00010
00011 #ifdef __sgi
00012 #if !defined(_POSIX_C_SOURCE) || !defined(_PTHREADS)
00013 #error Macros _POSIX_C_SOURCE=199506L and _PTHREADS are needed for POSIX threads under IRIX!
00014 #endif
00015 #endif
00016
00017
00018
00019
00026
00027
00028
00029
00030 #include <pthread.h>
00031 #include <stdlib.h>
00032 #include <errno.h>
00033
00034
00035
00036 namespace RazorBack {
00037
00043 class Mutex_
00044 {
00045
00046 private:
00047
00048 pthread_mutex_t Mutex;
00049
00050
00051 public:
00052
00054 Mutex_() { pthread_mutex_init(&Mutex, NULL); }
00055
00057 ~Mutex_()
00058 {
00059 pthread_mutex_destroy(&Mutex);
00060 }
00061
00063 void lock() { pthread_mutex_lock(&Mutex); }
00064
00069 bool trylock()
00070 {
00071 return (EBUSY!=pthread_mutex_trylock(&Mutex));
00072 }
00073
00075 void unlock() { pthread_mutex_unlock(&Mutex); }
00076
00077 private:
00078 Mutex_(const Mutex_&);
00079 Mutex_& operator=(const Mutex_&);
00080 };
00081
00082
00087 class Condvar_
00088 {
00089
00090 private:
00091
00092 pthread_cond_t Cond;
00093 pthread_mutex_t Mtx;
00094 bool Signal;
00095
00096
00097 public:
00098
00100 Condvar_(): Signal(false)
00101 {
00102 pthread_cond_init(&Cond, NULL);
00103 pthread_mutex_init(&Mtx, NULL);
00104 }
00105
00107 ~Condvar_()
00108 {
00109 pthread_cond_destroy(&Cond);
00110 pthread_mutex_destroy(&Mtx);
00111 }
00112
00117 void reset();
00118
00124 void wait();
00125
00131 void timed_wait(long Sec, long Nanosec);
00132
00137 void signal();
00138
00143 void broadcast();
00144
00145 private:
00146 Condvar_(const Condvar_&);
00147 Condvar_& operator=(const Condvar_&);
00148 };
00149
00150
00154 class Semaphore_
00155 {
00156
00157 private:
00158
00159 int Value;
00160 pthread_cond_t Cond;
00161 pthread_mutex_t Mtx;
00162
00163
00165
00166
00168 Semaphore_(): Value(0)
00169 {
00170 pthread_cond_init(&Cond, NULL);
00171 pthread_mutex_init(&Mtx, NULL);
00172 }
00173
00175 ~Semaphore_()
00176 {
00177 pthread_cond_destroy(&Cond);
00178 pthread_mutex_destroy(&Mtx);
00179 }
00180
00186 void reset();
00187
00192 void increment(size_t Inc=1);
00193
00198 void wait();
00199
00206 void decrement_and_signal(size_t Dec=1);
00207
00213 void decrement_and_broadcast(size_t Dec=1);
00214
00215 private:
00216 Semaphore_(const Semaphore_&);
00217 Semaphore_& operator=(const Semaphore_&);
00218 };
00219
00220
00221 }
00222
00223
00224
00225 #endif // MTSYNC_HEADER