X-Boost  2.3.8
mutex.h
Go to the documentation of this file.
1 /******************************************************************************
2 * sprint::mutex
3 *
4 * Copyright (C) 2003-2011 Paolo Medici <www.pmx.it>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 *******************************************************************************/
21 
27 #ifndef _SPRINT_MUTEX_H
28 #define _SPRINT_MUTEX_H
29 
30 // #include <sprint/sysdef.h>
31 // #include <sprint/cpptraits.h>
32 
33 #ifndef WIN32
34 # include <pthread.h>
35 #endif
36 
37 
38 namespace sprint {
39 
47 template<class T>
48 class scoped_lock {
49  T & m_mutex;
50  public:
52  scoped_lock(T & m) : m_mutex(m) { m_mutex.lock(); }
54  ~scoped_lock() { m_mutex.unlock(); }
55 };
56 
57 #ifdef WIN32
58 
63 #define USE_CRITICAL_SECTION
64 
65 /* ************************************* WIN32 ******************************* */
66 
67 #ifdef USE_CRITICAL_SECTION
68 
70 class mutex: public sprint::uncopiable {
71 private:
72  CRITICAL_SECTION m_cs;
73 public:
74  typedef sprint::scoped_lock<sprint::mutex> scoped_lock;
75 public:
76  inline mutex()
77  {
78  ::InitializeCriticalSection(&m_cs);
79  }
80  inline ~mutex()
81  {
82  ::DeleteCriticalSection(&m_cs);
83  }
85  inline void lock()
86  {
87  ::EnterCriticalSection (&m_cs);
88  }
90  inline void unlock()
91  {
92  ::LeaveCriticalSection( &m_cs );
93  }
94 
95 };
96 
97 #else
98 
100 class mutex: public sprint::uncopiable {
101 private:
102  HANDLE m_mutex;
103 
105  mutex(const mutex & src) { }
106 public:
107  typedef sprint::scoped_lock<sprint::mutex> scoped_lock;
108 public:
109  inline mutex()
110  {
111  m_mutex = ::CreateMutex(NULL, FALSE, NULL);
112  }
113  inline ~mutex()
114  {
115  ::CloseHandle(m_mutex);
116  }
117 
118  inline bool lock(unsigned int timeout = INFINITE)
119  {
120  return ::WaitForSingleObject(m_mutex, timeout) == WAIT_OBJECT_0;
121  }
122 
123  inline bool unlock()
124  {
125  return ::ReleaseMutex(m_mutex);
126  }
127 
128  inline HANDLE operator HANDLE()
129  {
130  return m_mutex;
131  }
132 
133 };
134 
135 #endif
136 
138 typedef mutex thread_mutex;
139 
140 #else // LINUX
141 
143 class mutex {
144 private:
145  pthread_mutex_t m_mutex;
146 public:
148 public:
149  mutex()
150  {
151  pthread_mutexattr_t attr;
152 
153  pthread_mutexattr_init(&attr);
154 
155  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
156 
157  pthread_mutex_init(&m_mutex, &attr);
158 
159  pthread_mutexattr_destroy(&attr);
160  }
161 
162  ~mutex()
163  {
164  pthread_mutex_destroy(&m_mutex);
165  }
166 
167  inline bool lock()
168  {
169  return pthread_mutex_lock(&m_mutex);
170  }
171 
172  inline void unlock()
173  {
174  pthread_mutex_unlock(&m_mutex);
175  }
176 
177  inline pthread_mutex_t gethandle()
178  {
179  return m_mutex;
180  }
181 
182 };
183 
184 #endif // WIN32
185 
186 
187 } // namespace sprint
188 
189 
190 #endif // _PM_MUTEX_
scoped_lock(T &m)
ctor: lock the resource
Definition: mutex.h:52
Definition: mutex.h:48
~scoped_lock()
dtor: unlock the resource
Definition: mutex.h:54
Mutex (implemented using pthread)
Definition: mutex.h:143