ClanLib
2.3.7
Main Page
Modules
Namespaces
Classes
Files
File List
File Members
Sources
API
Core
System
mutex.h
Go to the documentation of this file.
1
/*
2
** ClanLib SDK
3
** Copyright (c) 1997-2011 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
31
32
33
#pragma once
34
35
36
#include "../api_core.h"
37
38
#ifdef WIN32
39
#include <windows.h>
40
#else
41
#ifndef __USE_UNIX98
42
#define __USE_UNIX98
43
#endif
44
#include <pthread.h>
45
#endif
46
50
class
CL_API_CORE
CL_Mutex
51
{
54
55
public
:
57
CL_Mutex
();
58
59
~
CL_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
100
class
CL_API_CORE
CL_MutexSection
101
{
104
105
public
:
107
CL_MutexSection
(
CL_Mutex
*mutex,
bool
lock_mutex =
true
)
108
: mutex(mutex), lock_count(0)
109
{
110
if
(lock_mutex)
111
lock();
112
}
113
114
~CL_MutexSection
()
115
{
116
if
(lock_count > 0 && mutex)
117
mutex->unlock();
118
lock_count = 0;
119
}
120
121
125
126
public
:
128
int
get_lock_count
()
const
129
{
130
return
lock_count;
131
}
132
133
137
138
public
:
140
void
lock
()
141
{
142
if
(mutex)
143
mutex->lock();
144
lock_count++;
145
}
146
148
bool
try_lock
()
149
{
150
if
(mutex == 0 || mutex->try_lock())
151
{
152
lock_count++;
153
return
true
;
154
}
155
return
false
;
156
}
157
159
void
unlock
()
160
{
161
if
(lock_count <= 0)
162
return
;
163
164
if
(mutex)
165
mutex->unlock();
166
lock_count--;
167
}
168
169
173
174
private
:
175
CL_Mutex
*mutex;
176
177
int
lock_count;
179
};
180
181
Generated on Fri Jun 14 2013 13:05:45 for ClanLib by
1.8.4