forked from couchbase/ep-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrwlock.h
More file actions
135 lines (114 loc) · 3.23 KB
/
Copy pathrwlock.h
File metadata and controls
135 lines (114 loc) · 3.23 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2015 Couchbase, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <platform/platform.h>
#include <string>
#include "utility.h"
class ReaderLock;
class WriterLock;
/**
* Reader/Write lock abstraction for platform provided cb_rw_lock
*
* The lock allows many readers but mutual exclusion with a writer.
*/
class RWLock {
public:
RWLock() {
cb_rw_lock_initialize(&lock);
}
~RWLock() {
cb_rw_lock_destroy(&lock);
}
/**
* Returns a `ReaderLock` reference which implements BasicLockable
* to allow managing the reader lock with a std::lock_guard
*/
ReaderLock& reader();
operator ReaderLock&() {
return reader();
}
/**
* Returns a `WriterLock` reference which implements BasicLockable
* to allow managing the writer lock with a std::lock_guard
*/
WriterLock& writer();
operator WriterLock&() {
return writer();
}
protected:
void readerLock() {
auto locked = cb_rw_reader_enter(&lock);
if (locked != 0) {
throw std::runtime_error(std::to_string(locked) +
" returned by cb_rw_reader_enter()");
}
}
void readerUnlock() {
int unlocked = cb_rw_reader_exit(&lock);
if (unlocked != 0) {
throw std::runtime_error(std::to_string(unlocked) +
" returned by cb_rw_reader_exit()");
}
}
void writerLock() {
int locked = cb_rw_writer_enter(&lock);
if (locked != 0) {
throw std::runtime_error(std::to_string(locked) +
" returned by cb_rw_writer_enter()");
}
}
void writerUnlock() {
int unlocked = cb_rw_writer_exit(&lock);
if (unlocked != 0) {
throw std::runtime_error(std::to_string(unlocked) +
" returned by cb_rw_writer_exit()");
}
}
private:
cb_rwlock_t lock;
DISALLOW_COPY_AND_ASSIGN(RWLock);
};
/**
* BasicLockable abstraction around the reader lock of an RWLock
*/
class ReaderLock : public RWLock {
public:
void lock() {
readerLock();
}
void unlock() {
readerUnlock();
}
};
/**
* BasicLockable abstraction around the writer lock of an RWLock
*/
class WriterLock : public RWLock {
public:
void lock() {
writerLock();
}
void unlock() {
writerUnlock();
}
};
inline ReaderLock& RWLock::reader() {
return static_cast<ReaderLock&>(*this);
}
inline WriterLock& RWLock::writer() {
return static_cast<WriterLock&>(*this);
}