Pol  Rev 533
clib/hbmutex.h
Go to the documentation of this file.
00001 /*
00002  * With permission to reuse:
00003  * Author:  Thomas Volkert
00004  * Libary:  Homerbase http://www.homer-conferencing.com/
00005  * Since:   2010-09-20
00006  */
00007 
00008 #ifndef _BASE_MUTEX_
00009 #define _BASE_MUTEX_
00010 
00011 #ifdef _WIN32
00012         #include <windows.h>
00013         #include <stdio.h>
00014         #define OS_DEP_MUTEX HANDLE
00015 #else
00016         #include <pthread.h>
00017         #include <stdlib.h>
00018         #include <errno.h>
00019         #define OS_DEP_MUTEX pthread_mutex_t*
00020 #endif
00021 
00023 
00024 class Mutex
00025 {
00026 public:
00027     Mutex( );
00028 
00029     virtual ~Mutex( );
00030 
00031     /* every function returns TRUE if successful */
00032     bool lock();
00033     bool unlock();
00034     bool tryLock();
00035     bool tryLock(int pMSecs);
00036 
00037 private:
00038     OS_DEP_MUTEX mPThreadMutex;
00039 };
00040 
00042 
00043 // Small mod: Guard Class for exception safe lock
00044 class LocalMutex
00045 {
00046 public:
00047         inline LocalMutex(Mutex* mutex);
00048         inline ~LocalMutex();
00049         inline void unlock();
00050 private:
00051         Mutex* _mutex;
00052         LocalMutex( LocalMutex& ); 
00053         LocalMutex& operator=( LocalMutex& );
00054         LocalMutex(const Mutex*);
00055 };
00056 inline LocalMutex::LocalMutex(Mutex* mutex):_mutex(mutex)
00057 {
00058         _mutex->lock();
00059 }
00060 inline LocalMutex::~LocalMutex()
00061 {
00062         _mutex->unlock();
00063 }
00064 inline void LocalMutex::unlock()
00065 {
00066         if (_mutex != 0)
00067         {
00068                 _mutex->unlock();
00069                 _mutex = 0;
00070         }
00071 }
00072 
00073 #endif