1
2
3
4
5
6 #include <soulng/util/Mutex.hpp>
7
8 namespace soulng { namespace util {
9
10 MutexOwner::MutexOwner(char id_): id(id_)
11 {
12 }
13
14 void MutexOwner::Push(char mutexId)
15 {
16 ownedMutexes.push_back(mutexId);
17 }
18
19 void MutexOwner::Pop()
20 {
21 ownedMutexes.pop_back();
22 }
23
24 OwnerGuard::OwnerGuard(Mutex& mtx_, MutexOwner& owner_) : mtx(mtx_), owner(owner_)
25 {
26 owner.Push(mtx.Id());
27 }
28
29 OwnerGuard::~OwnerGuard()
30 {
31 owner.Pop();
32 }
33
34 LockGuard::LockGuard(Mutex& mtx_, MutexOwner& owner_) : mtx(mtx_), owner(owner_), locked(false)
35 {
36 Lock();
37 }
38
39 LockGuard::~LockGuard()
40 {
41 if (locked)
42 {
43 Unlock();
44 }
45 }
46
47 void LockGuard::Lock()
48 {
49 owner.Push(mtx.Id());
50 mtx.Lock();
51 locked = true;
52 }
53
54 void LockGuard::Unlock()
55 {
56 mtx.Unlock();
57 owner.Pop();
58 locked = false;
59 }
60
61 Mutex::Mutex(char id_) : id(id_)
62 {
63 }
64
65 } }