-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutex.h
71 lines (55 loc) · 1.48 KB
/
mutex.h
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
#ifndef _MY_MUTEX
#define _MY_MUTEX
#include <mutex> //cannot do: lock
#include "mutex/lock_guard.h"
#include "mutex/tag_types.h"
#include "tuple.h"
namespace my {
// scoped_lock
template <class... Mutexes>
class scoped_lock {
public:
explicit scoped_lock(Mutexes &...Mtxes) : My_Mutexes(Mtxes...) {
std::lock(Mtxes...);
}
explicit scoped_lock(my::adopt_lock_t, Mutexes &...Mtxes)
: My_Mutexes(Mtxes...) {}
~scoped_lock() noexcept {
my::apply(
[](Mutexes &...Mtxes) {
(..., Mtxes.unlock());
},
My_Mutexes);
}
scoped_lock(const scoped_lock &) = delete;
scoped_lock &operator=(const scoped_lock &) = delete;
private:
std::tuple<Mutexes &...> My_Mutexes;
};
template <class _Mutex>
class scoped_lock<_Mutex> {
public:
using mutex_type = _Mutex;
explicit scoped_lock(_Mutex &_Mtx) : _MyMutex(_Mtx) {
_MyMutex.lock();
}
explicit scoped_lock(my::adopt_lock_t, _Mutex &_Mtx) noexcept
: _MyMutex(_Mtx) {}
~scoped_lock() noexcept {
_MyMutex.unlock();
}
scoped_lock(const scoped_lock &) = delete;
scoped_lock &operator=(const scoped_lock &) = delete;
private:
_Mutex &_MyMutex;
};
template <>
class scoped_lock<> {
public:
explicit scoped_lock() = default;
explicit scoped_lock(adopt_lock_t) noexcept {}
scoped_lock(const scoped_lock &) = delete;
scoped_lock &operator=(const scoped_lock &) = delete;
};
} // namespace my
#endif