-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathrw_mutex.h
115 lines (102 loc) · 2.73 KB
/
rw_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright (C) 2004-2017 Savoir-faire Linux Inc.
*
* Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef RW_MUTEX_H_
#define RW_MUTEX_H_
#include "noncopyable.h"
#include <mutex>
#include <atomic>
#include <condition_variable>
#include <string>
#include <sstream>
namespace ring {
/**
* rw_mutex is a shared mutex meant to protect
* rarely-modified, often-read data structures.
*
* Its goal is to optimize read throughput and latency.
* Multiple threads can concurrently read data while
* a writer thread gets exclusive access when needed.
*/
class rw_mutex {
public:
rw_mutex() : mutex(), canRead(), canWrite(), readers(0), writing(false) {}
void read_enter() {
std::unique_lock<std::mutex> lck(mutex);
canRead.wait(lck, [this]() { return !writing; });
readers++;
}
void read_exit() {
//std::lock_guard<std::mutex> lck(mutex);
readers--;
canWrite.notify_one();
}
void write_enter() {
std::unique_lock<std::mutex> lck(mutex);
canWrite.wait(lck, [this]() { return !writing && readers==0; });
writing = true;
}
void write_exit() {
std::lock_guard<std::mutex> lck(mutex);
writing = false;
canWrite.notify_one();
canRead.notify_all();
}
struct read_lock {
public:
read_lock(rw_mutex& m) : sem(m) {
sem.read_enter();
}
~read_lock() {
sem.read_exit();
}
private:
rw_mutex& sem;
};
struct write_lock {
public:
write_lock(rw_mutex& m) : sem(m) {
sem.write_enter();
}
~write_lock() {
sem.write_exit();
}
private:
rw_mutex& sem;
};
read_lock read() {
return read_lock(*this);
}
write_lock write() {
return write_lock(*this);
}
std::string toString() {
std::stringstream ss;
ss << "[rw_mutex write:" << (writing?"LOCKED":"unlocked") << " read:" << readers << "]";
return ss.str();
}
private:
NON_COPYABLE(rw_mutex);
std::mutex mutex;
std::condition_variable canRead, canWrite;
std::atomic<unsigned> readers;
bool writing;
};
} // namespace ring
#endif