-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmutex.hpp
78 lines (65 loc) · 1.61 KB
/
mutex.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//@ {
//@ "targets":[{"name":"mutex.hpp","type":"include"}]
//@ ,"dependencies_extra":[{"ref":"mutex.o","rel":"implementation"}]
//@ }
#ifndef ANJA_MUTEX_HPP
#define ANJA_MUTEX_HPP
#include "error.hpp"
#include <cstdint>
namespace Anja
{
class Mutex
{
public:
class LockGuard
{
public:
LockGuard(const LockGuard&)=delete;
LockGuard& operator=(const LockGuard&)=delete;
explicit LockGuard(Mutex& m):r_m(m)
{m.lock();}
~LockGuard()
{r_m.unlock();}
private:
Mutex& r_m;
};
class LockGuardNonblocking
{
public:
LockGuardNonblocking(const LockGuardNonblocking&)=delete;
LockGuardNonblocking& operator=(const LockGuardNonblocking&)=delete;
LockGuardNonblocking(LockGuardNonblocking&&)=default;
LockGuardNonblocking& operator=(LockGuardNonblocking&&)=default;
explicit LockGuardNonblocking(Mutex& m):r_m(&m)
{
if(!m.lockTry())
{throw Error("The current resource is temporary busy. Please try again later.");}
}
~LockGuardNonblocking()
{r_m->unlock();}
private:
Mutex* r_m;
};
Mutex();
Mutex(const Mutex&)=delete;
Mutex(Mutex&&)=delete;
Mutex& operator=(const Mutex&)=delete;
Mutex& operator=(Mutex&&)=delete;
~Mutex();
void lock() noexcept;
void unlock() noexcept;
bool lockTry() noexcept;
private:
#if (__amd64 || __x86_64 || __x86_64__ || __amd64__)
static constexpr int MUTEX_SIZE=40;
#else
static constexpr int MUTEX_SIZE=24;
#endif
union
{
double dummy[MUTEX_SIZE/sizeof(double)];
uint8_t data[MUTEX_SIZE];
} m_impl;
};
}
#endif