-
Notifications
You must be signed in to change notification settings - Fork 0
/
turnstile.cpp
57 lines (45 loc) · 1.32 KB
/
turnstile.cpp
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
//
// Copyright 2018 CStanKonrad
//
#include "turnstile.h"
#include <mutex>
#include "turnstile_pool.h"
static TurnstilePool &getTurnstilePool() {
static TurnstilePool pool;
return pool;
}
void Mutex::lock() {
LockList *tmpLock = getTurnstilePool().getTmpLock();
std::unique_lock<std::mutex> lock(*getTurnstilePool().getGlobalLock(this));
if (this->turnstile == nullptr) {
this->turnstile = tmpLock;
} else {
LockList *threadLock = getTurnstilePool().getThreadLock().release();
threadLock->setNext(nullptr);
if (this->turnstile == tmpLock) {
this->turnstile = threadLock;
} else {
threadLock->setNext(this->turnstile->getNext());
this->turnstile->setNext(threadLock);
}
this->turnstile->lock(lock);
threadLock = this->turnstile->getNext();
if (threadLock == nullptr) {
threadLock = this->turnstile;
this->turnstile = tmpLock;
} else {
this->turnstile->setNext(threadLock->getNext());
}
lock.unlock();
getTurnstilePool().getThreadLock().reset(threadLock);
}
}
void Mutex::unlock() {
LockList *tmpLock = getTurnstilePool().getTmpLock();
std::unique_lock<std::mutex> lock(*getTurnstilePool().getGlobalLock(this));
if (this->turnstile == tmpLock) {
this->turnstile = nullptr;
} else {
this->turnstile->unlock(lock);
}
}