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