mutex.cpp
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 class MutexImpl
00036 {
00037 public:
00038 MutexImpl();
00039 ~MutexImpl();
00040 void lock();
00041 void unlock();
00042 private:
00043 MutexImpl( const MutexImpl& );
00044 MutexImpl& operator=( const MutexImpl& );
00045
00046 #ifdef _WIN32
00047 CRITICAL_SECTION m_cs;
00048 #elif defined( HAVE_PTHREAD )
00049 pthread_mutex_t m_mutex;
00050 #endif
00051
00052 };
00053
00054 MutexImpl::MutexImpl()
00055 {
00056 #ifdef _WIN32
00057 InitializeCriticalSection( &m_cs );
00058 #elif defined( HAVE_PTHREAD )
00059 pthread_mutex_init( &m_mutex, 0 );
00060 #endif
00061 }
00062
00063 MutexImpl::~MutexImpl()
00064 {
00065 #ifdef _WIN32
00066 DeleteCriticalSection( &m_cs );
00067 #elif defined( HAVE_PTHREAD )
00068 pthread_mutex_destroy( &m_mutex );
00069 #endif
00070 }
00071
00072 void MutexImpl::lock()
00073 {
00074 #ifdef _WIN32
00075 EnterCriticalSection( &m_cs );
00076 #elif defined( HAVE_PTHREAD )
00077 pthread_mutex_lock( &m_mutex );
00078 #endif
00079 }
00080
00081 void MutexImpl::unlock()
00082 {
00083 #ifdef _WIN32
00084 LeaveCriticalSection( &m_cs );
00085 #elif defined( HAVE_PTHREAD )
00086 pthread_mutex_unlock( &m_mutex );
00087 #endif
00088 }
00089
00090 Mutex::Mutex()
00091 : m_mutex( new MutexImpl() )
00092 {
00093 }
00094
00095 Mutex::~Mutex()
00096 {
00097 delete m_mutex;
00098 }
00099
00100 void Mutex::lock()
00101 {
00102 m_mutex->lock();
00103 }
00104
00105 void Mutex::unlock()
00106 {
00107 m_mutex->unlock();
00108 }
00109
00110 }