gloox  1.0.1
mutex.cpp
1 /*
2  Copyright (c) 2007-2012 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 #include "config.h"
17 
18 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
19 # include <windows.h>
20 #endif
21 
22 #ifdef _WIN32_WCE
23 # include <winbase.h>
24 #endif
25 
26 #ifdef HAVE_PTHREAD
27 # include <pthread.h>
28 #endif
29 
30 namespace gloox
31 {
32 
33  namespace util
34  {
35 
36  class Mutex::MutexImpl
37  {
38  public:
39  MutexImpl();
40  ~MutexImpl();
41  void lock();
42  bool trylock();
43  void unlock();
44  private:
45  MutexImpl( const MutexImpl& );
46  MutexImpl& operator=( const MutexImpl& );
47 
48 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
49  CRITICAL_SECTION m_cs;
50 #elif defined( HAVE_PTHREAD )
51  pthread_mutex_t m_mutex;
52 #endif
53 
54  };
55 
56  Mutex::MutexImpl::MutexImpl()
57  {
58 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
59  // NOTE: Critical sections by nature allow "recursive"
60  // (the same thread can get it again, and just bump the ref count).
61  InitializeCriticalSection( &m_cs );
62 #elif defined( HAVE_PTHREAD )
63  // For pthreads, configured the mutex to be recursive
64  // (the same thread can get it again, and just bump the ref count).
65  pthread_mutexattr_t mutexAttribute;
66  pthread_mutexattr_init( &mutexAttribute );
67  pthread_mutexattr_settype( &mutexAttribute, PTHREAD_MUTEX_RECURSIVE );
68  pthread_mutex_init( &m_mutex, &mutexAttribute );
69  pthread_mutexattr_destroy( &mutexAttribute );
70 #endif
71  }
72 
73  Mutex::MutexImpl::~MutexImpl()
74  {
75 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
76  DeleteCriticalSection( &m_cs );
77 #elif defined( HAVE_PTHREAD )
78  pthread_mutex_destroy( &m_mutex );
79 #endif
80  }
81 
83  {
84 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
85  EnterCriticalSection( &m_cs );
86 #elif defined( HAVE_PTHREAD )
87  pthread_mutex_lock( &m_mutex );
88 #endif
89  }
90 
92  {
93 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
94  return TryEnterCriticalSection( &m_cs ) ? true : false;
95 #elif defined( HAVE_PTHREAD )
96  return !( pthread_mutex_trylock( &m_mutex ) );
97 #else
98  return true;
99 #endif
100  }
101 
103  {
104 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
105  LeaveCriticalSection( &m_cs );
106 #elif defined( HAVE_PTHREAD )
107  pthread_mutex_unlock( &m_mutex );
108 #endif
109  }
110 
112  : m_mutex( new MutexImpl() )
113  {
114  }
115 
117  {
118  delete m_mutex;
119  }
120 
121  void Mutex::lock()
122  {
123  m_mutex->lock();
124  }
125 
127  {
128  return m_mutex->trylock();
129  }
130 
132  {
133  m_mutex->unlock();
134  }
135 
136  }
137 
138 }