xoreos  0.0.5
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 
32  _mutex = SDL_CreateMutex();
33 
34  assert(_mutex);
35 }
36 
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 
53  _semaphore = SDL_CreateSemaphore(value);
54 }
55 
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 
72  return SDL_SemTryWait(_semaphore) == 0;
73 }
74 
76  SDL_SemPost(_semaphore);
77 }
78 
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 
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 
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 
139  SDL_CondSignal(_condition);
140 }
141 
142 } // End of namespace Common
Definition: 2dafile.h:39
bool lockTry()
Definition: mutex.cpp:71
bool _ownMutex
Definition: mutex.h:93
A semaphore .
Definition: mutex.h:55
bool lock(uint32 timeout=0)
Definition: mutex.cpp:60
SDL_cond * _condition
Definition: mutex.h:96
uint32 getValue()
Definition: mutex.cpp:79
StackLock(Mutex &mutex)
Definition: mutex.cpp:84
bool wait(uint32 timeout=0)
Definition: mutex.cpp:121
A mutex.
Definition: mutex.h:40
SDL_mutex * _mutex
Definition: mutex.h:49
Mutex * _mutex
Definition: mutex.h:95
Semaphore * _semaphore
Definition: mutex.h:79
Thread mutex classes.
SDL_sem * _semaphore
Definition: mutex.h:67
Mutex * _mutex
Definition: mutex.h:78
uint32_t uint32
Definition: types.h:204
void lock()
Definition: mutex.cpp:41
void unlock()
Definition: mutex.cpp:75
void unlock()
Definition: mutex.cpp:47
Semaphore(uint value=0)
Definition: mutex.cpp:52
unsigned int uint
Definition: types.h:211