00001 #ifndef SYNC_WRAPPER_H_BY_DAVID_MAISONAVE_HEADER_GUARD_
00002 #define SYNC_WRAPPER_H_BY_DAVID_MAISONAVE_HEADER_GUARD_
00003
00026 #include <stdlib.h>
00027
00028 #ifdef _WIN32
00029 template<class T>
00030 class mutex_wrapper : public T
00031 {
00032 HANDLE m_hndl;
00033 inline void lock(){WaitForSingleObject(m_hndl, INFINITE);}
00034 inline void unlock(){ReleaseMutex(m_hndl);}
00035 mutex_wrapper& operator=(const mutex_wrapper&);
00036 public:
00037 inline mutex_wrapper():m_hndl(CreateMutex(NULL, FALSE, NULL)){}
00038 template<class T1> mutex_wrapper(T1& t1):T(t1), m_hndl(CreateMutex(NULL, FALSE, NULL)){}
00039 template<class T1, class T2> mutex_wrapper(T1& t1, T2& t2):T(t1, t2), m_hndl(CreateMutex(NULL, FALSE, NULL)){}
00040 template<class T1, class T2, class T3> mutex_wrapper(T1& t1, T2& t2, T3& t3):T(t1, t2, t3), m_hndl(CreateMutex(NULL, FALSE, NULL)){}
00041 inline ~mutex_wrapper()
00042 {
00043 ReleaseMutex(m_hndl);
00044 CloseHandle(m_hndl);
00045 }
00046 inline friend void intrusive_ptr_lock(mutex_wrapper<T> *p){p->lock();}
00047 inline friend void intrusive_ptr_unlock(mutex_wrapper<T> *p){p->unlock();}
00048 };
00049
00050 template<class T>
00051 class critical_sect_wrapper : public T
00052 {
00053 CRITICAL_SECTION m_cs;
00054 inline void lock(){EnterCriticalSection(&m_cs);}
00055 inline void unlock(){LeaveCriticalSection(&m_cs);}
00056 critical_sect_wrapper& operator=(const critical_sect_wrapper&);
00057 public:
00058 inline critical_sect_wrapper(){InitializeCriticalSection(&m_cs);}
00059 template<class T1> critical_sect_wrapper(T1& t1):T(t1){InitializeCriticalSection(&m_cs);}
00060 template<class T1, class T2> critical_sect_wrapper(T1& t1, T2& t2):T(t1, t2){InitializeCriticalSection(&m_cs);}
00061 template<class T1, class T2, class T3> critical_sect_wrapper(T1& t1, T2& t2, T3& t3):T(t1, t2, t3){InitializeCriticalSection(&m_cs);}
00062 inline ~critical_sect_wrapper(){DeleteCriticalSection(&m_cs);}
00063 inline friend void intrusive_ptr_lock(critical_sect_wrapper *p){p->lock();}
00064 inline friend void intrusive_ptr_unlock(critical_sect_wrapper *p){p->unlock();}
00065 };
00066
00067 template<class T>
00068 class fast_mutex_wrapper : public T
00069 {
00070 LONG volatile locker;
00071 void lock()
00072 {
00073
00074 while (InterlockedIncrement(&locker) > 1)
00075 {
00076 InterlockedDecrement(&locker);
00077 Sleep(rand()%3);
00078 }
00079 }
00080 inline void unlock(){assert (locker > 0);InterlockedDecrement(&locker);}
00081 fast_mutex_wrapper& operator=(const fast_mutex_wrapper&);
00082 public:
00083 inline fast_mutex_wrapper():locker(0){}
00084 template<class T1> fast_mutex_wrapper(T1& t1):T(t1), locker(0){}
00085 template<class T1, class T2> fast_mutex_wrapper(T1& t1, T2& t2):T(t1, t2), locker(0){}
00086 template<class T1, class T2, class T3> fast_mutex_wrapper(T1& t1, T2& t2, T3& t3):T(t1, t2, t3), locker(0){}
00087 inline friend void intrusive_ptr_lock(fast_mutex_wrapper *p){p->lock();}
00088 inline friend void intrusive_ptr_unlock(fast_mutex_wrapper *p){p->unlock();}
00089 };
00090
00091 #else
00092 #ifdef __POSIX__
00093 template<class T>
00094 class mutex_wrapper : public T
00095 {
00096 pthread_mutex_t m_mutex;
00097 inline void lock(){pthread_mutex_lock(&m_mutex);}
00098 inline void unlock(){pthread_mutex_unlock(&m_mutex);}
00099 mutex_wrapper& operator=(const mutex_wrapper&);
00100 public:
00101 inline mutex_wrapper():m_mutex(PTHREAD_MUTEX_INITIALIZER){}
00102 template<class T1> mutex_wrapper(T1& t1):T(t1), m_mutex(PTHREAD_MUTEX_INITIALIZER){}
00103 template<class T1, class T2> mutex_wrapper(T1& t1, T2& t2):T(t1, t2), m_mutex(PTHREAD_MUTEX_INITIALIZER){}
00104 template<class T1, class T2, class T3> mutex_wrapper(T1& t1, T2& t2, T3& t3):T(t1, t2, t3), m_mutex(PTHREAD_MUTEX_INITIALIZER){}
00105 inline ~mutex_wrapper(){pthread_mutex_destroy(&m_mutex);}
00106 inline friend void intrusive_ptr_lock(mutex_wrapper *p){p->lock();}
00107 inline friend void intrusive_ptr_unlock(mutex_wrapper *p){p->unlock();}
00108 };
00109 #endif //__POSIX__
00110 #endif //_WIN32
00111
00112 template <typename TT> void unlock_function_for_scope_lock(TT * ptr) {
00113 if (!ptr) return;
00114 ptr->unlock();
00115 }
00116
00130 class scope_lock
00131 {
00132 typedef void (*unlock_fct_Type ) (void *);
00133 unlock_fct_Type m_unlock_fct;
00134 void *m_type;
00135 public:
00136 template<class T> scope_lock(T& smrtPtr, bool LockIt = true):m_type((void*)&smrtPtr)
00137 {
00138 if (LockIt) smrtPtr.lock();
00139 void ( *tmp ) (T *) = unlock_function_for_scope_lock<T>;
00140 m_unlock_fct = (unlock_fct_Type)tmp;
00141 }
00142 ~scope_lock()
00143 {
00144 m_unlock_fct(m_type);
00145 }
00146 };
00147
00148
00169 #endif //SYNC_WRAPPER_H_BY_DAVID_MAISONAVE_HEADER_GUARD_
00170