1#include "duckdb/storage/storage_lock.hpp"
2#include "duckdb/common/common.hpp"
3
4using namespace duckdb;
5using namespace std;
6
7StorageLockKey::StorageLockKey(StorageLock &lock, StorageLockType type) : lock(lock), type(type) {
8}
9
10StorageLockKey::~StorageLockKey() {
11 if (type == StorageLockType::EXCLUSIVE) {
12 lock.ReleaseExclusiveLock();
13 } else {
14 assert(type == StorageLockType::SHARED);
15 lock.ReleaseSharedLock();
16 }
17}
18
19StorageLock::StorageLock() : read_count(0) {
20}
21
22unique_ptr<StorageLockKey> StorageLock::GetExclusiveLock() {
23 exclusive_lock.lock();
24 while (read_count != 0) {
25 }
26 return make_unique<StorageLockKey>(*this, StorageLockType::EXCLUSIVE);
27}
28
29unique_ptr<StorageLockKey> StorageLock::GetSharedLock() {
30 exclusive_lock.lock();
31 read_count++;
32 exclusive_lock.unlock();
33 return make_unique<StorageLockKey>(*this, StorageLockType::SHARED);
34}
35
36void StorageLock::ReleaseExclusiveLock() {
37 exclusive_lock.unlock();
38}
39
40void StorageLock::ReleaseSharedLock() {
41 read_count--;
42}
43