gloox  0.9.9.12
mutex.cpp
1 /*
2  Copyright (c) 2007-2008 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 #include "mutex.h"
15 
16 #if !defined( _WIN32 ) && !defined( _WIN32_WCE )
17 # include "config.h"
18 #endif
19 
20 #ifdef _WIN32
21 # include <windows.h>
22 #endif
23 
24 #ifdef _WIN32_WCE
25 # include <winbase.h>
26 #endif
27 
28 #ifdef HAVE_PTHREAD
29 # include <pthread.h>
30 #endif
31 
32 namespace gloox
33 {
34 
35  class MutexImpl
36  {
37  public:
38  MutexImpl();
39  ~MutexImpl();
40  void lock();
41  void unlock();
42  private:
43  MutexImpl( const MutexImpl& );
44  MutexImpl& operator=( const MutexImpl& );
45 
46 #ifdef _WIN32
47  CRITICAL_SECTION m_cs;
48 #elif defined( HAVE_PTHREAD )
49  pthread_mutex_t m_mutex;
50 #endif
51 
52  };
53 
54  MutexImpl::MutexImpl()
55  {
56 #ifdef _WIN32
57  InitializeCriticalSection( &m_cs );
58 #elif defined( HAVE_PTHREAD )
59  pthread_mutex_init( &m_mutex, 0 );
60 #endif
61  }
62 
63  MutexImpl::~MutexImpl()
64  {
65 #ifdef _WIN32
66  DeleteCriticalSection( &m_cs );
67 #elif defined( HAVE_PTHREAD )
68  pthread_mutex_destroy( &m_mutex );
69 #endif
70  }
71 
72  void MutexImpl::lock()
73  {
74 #ifdef _WIN32
75  EnterCriticalSection( &m_cs );
76 #elif defined( HAVE_PTHREAD )
77  pthread_mutex_lock( &m_mutex );
78 #endif
79  }
80 
81  void MutexImpl::unlock()
82  {
83 #ifdef _WIN32
84  LeaveCriticalSection( &m_cs );
85 #elif defined( HAVE_PTHREAD )
86  pthread_mutex_unlock( &m_mutex );
87 #endif
88  }
89 
91  : m_mutex( new MutexImpl() )
92  {
93  }
94 
96  {
97  delete m_mutex;
98  }
99 
100  void Mutex::lock()
101  {
102  m_mutex->lock();
103  }
104 
106  {
107  m_mutex->unlock();
108  }
109 
110 }