1#include "duckdb/storage/storage_lock.hpp"
2#include "duckdb/common/common.hpp"
3#include "duckdb/common/assert.hpp"
4
5namespace duckdb {
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 D_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_uniq<StorageLockKey>(args&: *this, args: StorageLockType::EXCLUSIVE);
27}
28
29unique_ptr<StorageLockKey> StorageLock::GetSharedLock() {
30 exclusive_lock.lock();
31 read_count++;
32 exclusive_lock.unlock();
33 return make_uniq<StorageLockKey>(args&: *this, args: StorageLockType::SHARED);
34}
35
36void StorageLock::ReleaseExclusiveLock() {
37 exclusive_lock.unlock();
38}
39
40void StorageLock::ReleaseSharedLock() {
41 read_count--;
42}
43
44} // namespace duckdb
45