-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority_mutex.h
More file actions
74 lines (57 loc) · 1.48 KB
/
priority_mutex.h
File metadata and controls
74 lines (57 loc) · 1.48 KB
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
//
// Created by YongGyu Lee on 2021/06/20.
//
#ifndef EYEDID_CPP_SAMPLE_PRIORITY_MUTEX_H_
#define EYEDID_CPP_SAMPLE_PRIORITY_MUTEX_H_
#include <atomic>
#include <condition_variable>
#include <mutex>
namespace sample {
class PriorityMutex {
class HighMutex {
public:
explicit HighMutex(PriorityMutex& m) noexcept : m_(m) {}
void lock();
void unlock();
bool try_to_lock();
HighMutex(HighMutex const&) = delete;
HighMutex(HighMutex &&) = delete;
HighMutex& operator=(HighMutex const&) = delete;
HighMutex& operator=(HighMutex &&) = delete;
private:
PriorityMutex& m_;
};
class LowMutex {
public:
explicit LowMutex(PriorityMutex& m) noexcept : m_(m) {}
void lock();
void unlock();
bool try_to_lock();
LowMutex(LowMutex const&) = delete;
LowMutex(LowMutex &&) = delete;
LowMutex& operator=(LowMutex const&) = delete;
LowMutex& operator=(LowMutex &&) = delete;
private:
PriorityMutex& m_;
};
public:
using mutex_type = std::mutex;
using low_mutex_type = LowMutex;
using high_mutex_type = HighMutex;
void lock_low();
void unlock_low();
bool try_to_lock_low();
LowMutex& low();
void lock_high();
void unlock_high();
bool try_to_lock_high();
HighMutex& high();
private:
mutex_type m_;
std::condition_variable cv_;
std::atomic_int high_accessing_{0};
low_mutex_type low_{*this};
high_mutex_type high_{*this};
};
} // namespace sample
#endif // EYEDID_CPP_SAMPLE_PRIORITY_MUTEX_H_