-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbdrwlock.h
179 lines (150 loc) · 4.49 KB
/
bdrwlock.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
Copyright (C) 2016 Blaise Dias
This file is part of sqzbsrv.
sqzbsrv 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 2 of the License, or
(at your option) any later version.
sqzbsrv 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 sqzbsrv. If not, see <http://www.gnu.org/licenses/>.
Classes implementing simple read write locks,
based on
1) "Fast Userspace Read/Write locks, built on top of mutexes. Paul Mackerras and Rusty Russel."
2) "Futexes are tricky by Ulrich Drepper"
The rules are simple and harsh, acquiring the same lock read or write more than
once in the same thread *WILL* lock up.
1) read lock followed by write lock, will deadlock since write locking waits for
current reads to end.
2) read lock followed by read lock, will deadlock if a write lock starts between the
the read lock operations.
3) write lock followed by read lock, will deadlock since read locking after a write
lock has started will block till the write has completed.
4) write lock followed by write lock, will deadlock since write locking waits for
previous writes to complete.
This is good enough for our purposes!
The exception to these rules may be PthreadRWLock, which uses pthread_rwlock.
*/
#include <semaphore.h>
#include <pthread.h>
namespace benedias {
class RWLock
{
public:
virtual ~RWLock(){}
virtual void read_lock()=0;
virtual void read_unlock()=0;
virtual void write_lock()=0;
virtual void write_unlock()=0;
};
// Simple FIFO read write lock, using a pair of futexes and atomic ops.
// "FIFO" means first in first out, writers are not starved.
class FurwLock1:public RWLock
{
protected:
int gate = 0;
int nreaders = 0;
void enter_gate();
void leave_gate();
public:
FurwLock1():gate(0),nreaders(0){}
~FurwLock1(){}
void read_lock();
void read_unlock();
void write_lock();
void write_unlock();
};
// Simple FIFO read write lock, using a futexes, a semaphore and atomic ops.
// "FIFO" means first in first out, writers are not starved.
class FurwLockSem:public RWLock
{
protected:
sem_t sem;
int nreaders;
public:
FurwLockSem()
{
sem_init(&sem, 0, 1);
nreaders = 0;
}
~FurwLockSem()
{
sem_destroy(&sem);
}
void read_lock();
void read_unlock();
void write_lock();
void write_unlock();
};
// Simplistic wrapping of pthread_rwlock.
// Default attributes, which means writers can be starved.
class PthreadRWLock:public RWLock
{
protected:
pthread_rwlock_t rwlock;
public:
PthreadRWLock()
{
pthread_rwlockattr_t attr;
pthread_rwlockattr_init(&attr);
pthread_rwlock_init(&rwlock, NULL);
}
~PthreadRWLock()
{
pthread_rwlock_destroy(&rwlock);
}
void read_lock() { pthread_rwlock_rdlock(&this->rwlock);}
void read_unlock() { pthread_rwlock_unlock(&this->rwlock);}
void write_lock() { pthread_rwlock_wrlock(&this->rwlock);}
void write_unlock() { pthread_rwlock_unlock(&this->rwlock);}
};
// Read lock guard impl
class read_lock_guard
{
RWLock& fu_rw_lock;
bool locked;
public:
read_lock_guard(RWLock& fl):fu_rw_lock(fl), locked(true)
{
fu_rw_lock.read_lock();
}
~read_lock_guard()
{
unlock();
}
void unlock()
{
if (locked)
{
locked = false;
fu_rw_lock.read_unlock();
}
}
};
// Write lock guard impl
class write_lock_guard
{
RWLock& fu_rw_lock;
bool locked;
public:
write_lock_guard(RWLock& fl):fu_rw_lock(fl), locked(true)
{
fu_rw_lock.write_lock();
}
~write_lock_guard()
{
unlock();
}
void unlock()
{
if (locked)
{
locked = false;
fu_rw_lock.write_unlock();
}
}
};
} // namespace benedias