xoreos  0.0.5
mutex.h
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 #ifndef COMMON_MUTEX_H
26 #define COMMON_MUTEX_H
27 
28 #include "src/common/fallthrough.h"
30 #include <SDL_thread.h>
32 
33 #include <boost/noncopyable.hpp>
34 
35 #include "src/common/types.h"
36 
37 namespace Common {
38 
40 class Mutex : boost::noncopyable {
41 public:
42  Mutex();
43  ~Mutex();
44 
45  void lock();
46  void unlock();
47 
48 private:
49  SDL_mutex *_mutex;
50 
51  friend class Condition;
52 };
53 
55 class Semaphore : boost::noncopyable {
56 public:
57  Semaphore(uint value = 0);
58  ~Semaphore();
59 
60  bool lock(uint32 timeout = 0);
61  bool lockTry();
62  void unlock();
63 
64  uint32 getValue();
65 
66 private:
67  SDL_sem *_semaphore;
68 };
69 
71 class StackLock : boost::noncopyable {
72 public:
73  StackLock(Mutex &mutex);
74  StackLock(Semaphore &semaphore);
75  ~StackLock();
76 
77 private:
80 };
81 
83 class Condition : boost::noncopyable {
84 public:
85  Condition();
86  Condition(Mutex &mutex);
87  ~Condition();
88 
89  bool wait(uint32 timeout = 0);
90  void signal();
91 
92 private:
93  bool _ownMutex;
94 
96  SDL_cond *_condition;
97 };
98 
99 } // End of namespace Common
100 
101 #endif // COMMON_MUTEX_H
Definition: 2dafile.h:39
#define START_IGNORE_IMPLICIT_FALLTHROUGH
Definition: fallthrough.h:79
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
A condition.
Definition: mutex.h:83
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
Low-level type definitions to handle fixed width types portably.
Convenience class that locks a mutex on creation and unlocks it on destruction.
Definition: mutex.h:71
#define STOP_IGNORE_IMPLICIT_FALLTHROUGH
Definition: fallthrough.h:80
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
Compiler-specific defines to mark an implicit switch-case fallthrough.
void unlock()
Definition: mutex.cpp:47
Semaphore(uint value=0)
Definition: mutex.cpp:52
unsigned int uint
Definition: types.h:211