00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "mutex.h"
00015
00016 #if !defined( _WIN32 ) && !defined( _WIN32_WCE )
00017 # include "config.h"
00018 #endif
00019
00020 #ifdef _WIN32
00021 # include <windows.h>
00022 #endif
00023
00024 #ifdef _WIN32_WCE
00025 # include <winbase.h>
00026 #endif
00027
00028 #ifdef HAVE_PTHREAD
00029 # include <pthread.h>
00030 #endif
00031
00032 namespace gloox
00033 {
00034
00035 namespace util
00036 {
00037
00038 class Mutex::MutexImpl
00039 {
00040 public:
00041 MutexImpl();
00042 ~MutexImpl();
00043 void lock();
00044 void unlock();
00045 private:
00046 MutexImpl( const MutexImpl& );
00047 MutexImpl& operator=( const MutexImpl& );
00048
00049 #ifdef _WIN32
00050 CRITICAL_SECTION m_cs;
00051 #elif defined( HAVE_PTHREAD )
00052 pthread_mutex_t m_mutex;
00053 #endif
00054
00055 };
00056
00057 Mutex::MutexImpl::MutexImpl()
00058 {
00059 #ifdef _WIN32
00060 InitializeCriticalSection( &m_cs );
00061 #elif defined( HAVE_PTHREAD )
00062 pthread_mutex_init( &m_mutex, 0 );
00063 #endif
00064 }
00065
00066 Mutex::MutexImpl::~MutexImpl()
00067 {
00068 #ifdef _WIN32
00069 DeleteCriticalSection( &m_cs );
00070 #elif defined( HAVE_PTHREAD )
00071 pthread_mutex_destroy( &m_mutex );
00072 #endif
00073 }
00074
00075 void Mutex::MutexImpl::lock()
00076 {
00077 #ifdef _WIN32
00078 EnterCriticalSection( &m_cs );
00079 #elif defined( HAVE_PTHREAD )
00080 pthread_mutex_lock( &m_mutex );
00081 #endif
00082 }
00083
00084 void Mutex::MutexImpl::unlock()
00085 {
00086 #ifdef _WIN32
00087 LeaveCriticalSection( &m_cs );
00088 #elif defined( HAVE_PTHREAD )
00089 pthread_mutex_unlock( &m_mutex );
00090 #endif
00091 }
00092
00093 Mutex::Mutex()
00094 : m_mutex( new MutexImpl() )
00095 {
00096 }
00097
00098 Mutex::~Mutex()
00099 {
00100 delete m_mutex;
00101 }
00102
00103 void Mutex::lock()
00104 {
00105 m_mutex->lock();
00106 }
00107
00108 void Mutex::unlock()
00109 {
00110 m_mutex->unlock();
00111 }
00112
00113 }
00114
00115 }