forked from fuzziqersoftware/phosg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Concurrency.cc
45 lines (37 loc) · 932 Bytes
/
Concurrency.cc
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
#include "Concurrency.hh"
using namespace std;
rw_lock::rw_lock() {
#ifdef LINUX
pthread_rwlockattr_t attr;
pthread_rwlockattr_init(&attr);
pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
pthread_rwlock_init(&this->lock, &attr);
#else
pthread_rwlock_init(&this->lock, NULL);
#endif
}
rw_lock::~rw_lock() {
pthread_rwlock_destroy(&this->lock);
}
rw_guard::rw_guard(rw_lock& lock, bool exclusive) : lock(&lock.lock) {
if (exclusive) {
pthread_rwlock_wrlock(this->lock);
} else {
pthread_rwlock_rdlock(this->lock);
}
}
rw_guard::rw_guard(pthread_rwlock_t* lock, bool exclusive) : lock(lock) {
if (exclusive) {
pthread_rwlock_wrlock(this->lock);
} else {
pthread_rwlock_rdlock(this->lock);
}
}
rw_guard::rw_guard(rw_guard&& g) : lock(g.lock) {
g.lock = NULL;
}
rw_guard::~rw_guard() {
if (this->lock) {
pthread_rwlock_unlock(this->lock);
}
}