xoreos
0.0.5
src
common
mutex.cpp
Go to the documentation of this file.
1
/* xoreos - A reimplementation of BioWare's Aurora engine
2
*
3
* xoreos is the legal property of its developers, whose names
4
* can be found in the AUTHORS file distributed with this source
5
* distribution.
6
*
7
* xoreos is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License
9
* as published by the Free Software Foundation; either version 3
10
* of the License, or (at your option) any later version.
11
*
12
* xoreos is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
16
*
17
* You should have received a copy of the GNU General Public License
18
* along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19
*/
20
25
#include <cassert>
26
27
#include "
src/common/mutex.h
"
28
29
namespace
Common
{
30
31
Mutex::Mutex
() {
32
_mutex
= SDL_CreateMutex();
33
34
assert(
_mutex
);
35
}
36
37
Mutex::~Mutex
() {
38
SDL_DestroyMutex(
_mutex
);
39
}
40
41
void
Mutex::lock
() {
42
int
res = SDL_LockMutex(
_mutex
);
43
44
assert(res == 0);
45
}
46
47
void
Mutex::unlock
() {
48
SDL_UnlockMutex(
_mutex
);
49
}
50
51
52
Semaphore::Semaphore
(
uint
value) {
53
_semaphore
= SDL_CreateSemaphore(value);
54
}
55
56
Semaphore::~Semaphore
() {
57
SDL_DestroySemaphore(
_semaphore
);
58
}
59
60
bool
Semaphore::lock
(
uint32
timeout) {
61
int
ret;
62
63
if
(timeout == 0)
64
ret = SDL_SemWait(
_semaphore
);
65
else
66
ret = SDL_SemWaitTimeout(
_semaphore
, timeout);
67
68
return
ret == 0;
69
}
70
71
bool
Semaphore::lockTry
() {
72
return
SDL_SemTryWait(
_semaphore
) == 0;
73
}
74
75
void
Semaphore::unlock
() {
76
SDL_SemPost(
_semaphore
);
77
}
78
79
uint32
Semaphore::getValue
() {
80
return
SDL_SemValue(
_semaphore
);
81
}
82
83
84
StackLock::StackLock
(
Mutex
&mutex) : _mutex(&mutex), _semaphore(0) {
85
_mutex
->
lock
();
86
}
87
88
StackLock::StackLock
(
Semaphore
&semaphore) : _mutex(0), _semaphore(&semaphore) {
89
_semaphore
->
lock
();
90
}
91
92
StackLock::~StackLock
() {
93
if
(
_mutex
)
94
_mutex
->
unlock
();
95
if
(
_semaphore
)
96
_semaphore
->
unlock
();
97
}
98
99
100
Condition::Condition
() : _ownMutex(true) {
101
_mutex
=
new
Mutex
;
102
103
_condition
= SDL_CreateCond();
104
105
assert(
_condition
);
106
}
107
108
Condition::Condition
(
Mutex
&mutex) : _ownMutex(false), _mutex(&mutex) {
109
_condition
= SDL_CreateCond();
110
111
assert(
_condition
);
112
}
113
114
Condition::~Condition
() {
115
SDL_DestroyCond(
_condition
);
116
117
if
(
_ownMutex
)
118
delete
_mutex
;
119
}
120
121
bool
Condition::wait
(
uint32
timeout) {
122
int
gotSignal;
123
124
if
(
_ownMutex
)
125
_mutex
->
lock
();
126
127
if
(timeout == 0)
128
gotSignal = SDL_CondWait(
_condition
,
_mutex
->
_mutex
);
129
else
130
gotSignal = SDL_CondWaitTimeout(
_condition
,
_mutex
->
_mutex
, timeout);
131
132
if
(
_ownMutex
)
133
_mutex
->
unlock
();
134
135
return
gotSignal == 0;
136
}
137
138
void
Condition::signal
() {
139
SDL_CondSignal(
_condition
);
140
}
141
142
}
// End of namespace Common
Common::StackLock::~StackLock
~StackLock()
Definition:
mutex.cpp:92
Common
Definition:
2dafile.h:39
Common::Semaphore::lockTry
bool lockTry()
Definition:
mutex.cpp:71
Common::Condition::_ownMutex
bool _ownMutex
Definition:
mutex.h:93
Common::Semaphore
A semaphore .
Definition:
mutex.h:55
Common::Mutex::Mutex
Mutex()
Definition:
mutex.cpp:31
Common::Semaphore::lock
bool lock(uint32 timeout=0)
Definition:
mutex.cpp:60
Common::Condition::_condition
SDL_cond * _condition
Definition:
mutex.h:96
Common::Semaphore::getValue
uint32 getValue()
Definition:
mutex.cpp:79
Common::StackLock::StackLock
StackLock(Mutex &mutex)
Definition:
mutex.cpp:84
Common::Condition::wait
bool wait(uint32 timeout=0)
Definition:
mutex.cpp:121
Common::Mutex
A mutex.
Definition:
mutex.h:40
Common::Mutex::_mutex
SDL_mutex * _mutex
Definition:
mutex.h:49
Common::Condition::_mutex
Mutex * _mutex
Definition:
mutex.h:95
Common::StackLock::_semaphore
Semaphore * _semaphore
Definition:
mutex.h:79
Common::Condition::Condition
Condition()
Definition:
mutex.cpp:100
mutex.h
Thread mutex classes.
Common::Semaphore::_semaphore
SDL_sem * _semaphore
Definition:
mutex.h:67
Common::StackLock::_mutex
Mutex * _mutex
Definition:
mutex.h:78
Common::Semaphore::~Semaphore
~Semaphore()
Definition:
mutex.cpp:56
uint32
uint32_t uint32
Definition:
types.h:204
Common::Mutex::lock
void lock()
Definition:
mutex.cpp:41
Common::Semaphore::unlock
void unlock()
Definition:
mutex.cpp:75
Common::Mutex::unlock
void unlock()
Definition:
mutex.cpp:47
Common::Condition::signal
void signal()
Definition:
mutex.cpp:138
Common::Condition::~Condition
~Condition()
Definition:
mutex.cpp:114
Common::Semaphore::Semaphore
Semaphore(uint value=0)
Definition:
mutex.cpp:52
Common::Mutex::~Mutex
~Mutex()
Definition:
mutex.cpp:37
uint
unsigned int uint
Definition:
types.h:211
Generated on Sun Nov 18 2018 15:13:38 for xoreos by
1.8.14