mutex.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
29 
30 
31 #pragma once
32 
33 #include "../api_core.h"
34 
35 #ifdef WIN32
36 #include <windows.h>
37 #else
38 #ifndef __USE_UNIX98
39 #define __USE_UNIX98
40 #endif
41 #include <pthread.h>
42 #endif
43 
44 namespace clan
45 {
48 
50 class CL_API_CORE Mutex
51 {
54 
55 public:
57  Mutex();
58 
59  ~Mutex();
60 
61 
65 
66 public:
67 
68 
72 
73 public:
75  void lock();
76 
78  bool try_lock();
79 
81  void unlock();
82 
83 
87 
88 private:
89 #ifdef WIN32
90  CRITICAL_SECTION critical_section;
91 #else
92  pthread_mutex_t handle;
93 #endif
94 };
96 
98 class CL_API_CORE MutexSection
99 {
102 
103 public:
105  MutexSection(Mutex *mutex, bool lock_mutex = true)
106  : mutex(mutex), lock_count(0)
107  {
108  if (lock_mutex)
109  lock();
110  }
111 
113  {
114  if (lock_count > 0 && mutex)
115  mutex->unlock();
116  lock_count = 0;
117  }
118 
119 
123 
124 public:
126  int get_lock_count() const
127  {
128  return lock_count;
129  }
130 
131 
135 
136 public:
138  void lock()
139  {
140  if (mutex)
141  mutex->lock();
142  lock_count++;
143  }
144 
146  bool try_lock()
147  {
148  if (mutex == 0 || mutex->try_lock())
149  {
150  lock_count++;
151  return true;
152  }
153  return false;
154  }
155 
157  void unlock()
158  {
159  if (lock_count <= 0)
160  return;
161 
162  if (mutex)
163  mutex->unlock();
164  lock_count--;
165  }
166 
167 
171 
172 private:
173  Mutex *mutex;
174 
175  int lock_count;
177 };
178 
179 }
180 
int get_lock_count() const
Returns the amounts of recursive mutex locks performed by this section.
Definition: mutex.h:126
~MutexSection()
Definition: mutex.h:112
void lock()
Lock the mutex.
Definition: mutex.h:138
bool try_lock()
Attempt to lock mutex.
Definition: mutex.h:146
void unlock()
Unlock mutex.
Definition: mutex.h:157
MutexSection(Mutex *mutex, bool lock_mutex=true)
Constructs a mutex section.
Definition: mutex.h:105
Mutex locking helper.
Definition: mutex.h:98
Mutex class.
Definition: mutex.h:50