-
Notifications
You must be signed in to change notification settings - Fork 29
/
locks.hh
124 lines (107 loc) · 2.5 KB
/
locks.hh
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
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#ifndef LOCKS_H
#define LOCKS_H 1
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <functional>
#include "common.hh"
#include "mutex.hh"
#include "syncobject.hh"
/**
* RAII lock holder to guarantee release of the lock.
*
* It is a very bad idea to unlock a lock held by a LockHolder without
* using the LockHolder::unlock method.
*/
class LockHolder {
public:
/**
* Acquire the lock in the given mutex.
*/
LockHolder(Mutex &m) : mutex(m), locked(false) {
lock();
}
/**
* Copy constructor hands this lock to the new copy and then
* consider it released locally (i.e. renders unlock() a noop).
*/
LockHolder(const LockHolder& from) : mutex(from.mutex), locked(true) {
const_cast<LockHolder*>(&from)->locked = false;
}
/**
* Release the lock.
*/
~LockHolder() {
unlock();
}
/**
* Relock a lock that was manually unlocked.
*/
void lock() {
mutex.acquire();
locked = true;
}
/**
* Manually unlock a lock.
*/
void unlock() {
if (locked) {
locked = false;
mutex.release();
}
}
private:
Mutex &mutex;
bool locked;
void operator=(const LockHolder&);
};
/**
* RAII lock holder over multiple locks.
*/
class MultiLockHolder {
public:
/**
* Acquire a series of locks.
*
* @param m beginning of an array of locks
* @param n the number of locks to lock
*/
MultiLockHolder(Mutex *m, size_t n) : mutexes(m),
locked(new bool[n]),
n_locks(n) {
std::fill_n(locked, n_locks, false);
lock();
}
~MultiLockHolder() {
unlock();
delete[] locked;
}
/**
* Relock the series after having manually unlocked it.
*/
void lock() {
for (size_t i = 0; i < n_locks; i++) {
assert(!locked[i]);
mutexes[i].acquire();
locked[i] = true;
}
}
/**
* Manually unlock the series.
*/
void unlock() {
for (size_t i = 0; i < n_locks; i++) {
if (locked[i]) {
locked[i] = false;
mutexes[i].release();
}
}
}
private:
Mutex *mutexes;
bool *locked;
size_t n_locks;
DISALLOW_COPY_AND_ASSIGN(MultiLockHolder);
};
#endif /* LOCKS_H */