00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef LOCKING_H
00020 #define LOCKING_H
00021 #ifndef _WIN32
00022 #include <pthread.h>
00023 #endif
00024
00025 class CriticalSection {
00026 #ifdef _WIN32
00027 public:
00028 void enter() throw() {
00029 EnterCriticalSection( &cs );
00030 }
00031
00032 void leave() throw() {
00033 LeaveCriticalSection( &cs );
00034 }
00035
00036 CriticalSection() throw() {
00037 InitializeCriticalSection( &cs );
00038 }
00039
00040 ~CriticalSection() throw() {
00041 DeleteCriticalSection( &cs );
00042 }
00043
00044 private:
00045 CRITICAL_SECTION cs;
00046 #else
00047 public:
00048 CriticalSection() throw() {
00049 pthread_mutexattr_init( &mtx_attr );
00050 pthread_mutexattr_settype( &mtx_attr, PTHREAD_MUTEX_RECURSIVE );
00051
00052 pthread_mutex_init( &mtx, &mtx_attr );
00053 };
00054 ~CriticalSection() throw() {
00055 pthread_mutex_destroy( &mtx );
00056 pthread_mutexattr_destroy( &mtx_attr );
00057 };
00058 void enter() throw() { pthread_mutex_lock( &mtx ); };
00059 void leave() throw() { pthread_mutex_unlock( &mtx ); };
00060 pthread_mutex_t& getMutex() { return mtx; };
00061
00062 private:
00063 pthread_mutex_t mtx;
00064 pthread_mutexattr_t mtx_attr;
00065 #endif
00066 };
00067
00068 template<class T>
00069 class LockBase {
00070 public:
00071 LockBase( T& aCs ) throw() : cs( aCs ) { cs.enter(); };
00072 ~LockBase() throw() { cs.leave(); };
00073
00074 private:
00075 T& cs;
00076 };
00077
00078 typedef LockBase<CriticalSection> Lock;
00079
00080 #endif