1 | #include "duckdb/storage/storage_lock.hpp" |
---|---|
2 | #include "duckdb/common/common.hpp" |
3 | #include "duckdb/common/assert.hpp" |
4 | |
5 | namespace duckdb { |
6 | |
7 | StorageLockKey::StorageLockKey(StorageLock &lock, StorageLockType type) : lock(lock), type(type) { |
8 | } |
9 | |
10 | StorageLockKey::~StorageLockKey() { |
11 | if (type == StorageLockType::EXCLUSIVE) { |
12 | lock.ReleaseExclusiveLock(); |
13 | } else { |
14 | D_ASSERT(type == StorageLockType::SHARED); |
15 | lock.ReleaseSharedLock(); |
16 | } |
17 | } |
18 | |
19 | StorageLock::StorageLock() : read_count(0) { |
20 | } |
21 | |
22 | unique_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 | |
29 | unique_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 | |
36 | void StorageLock::ReleaseExclusiveLock() { |
37 | exclusive_lock.unlock(); |
38 | } |
39 | |
40 | void StorageLock::ReleaseSharedLock() { |
41 | read_count--; |
42 | } |
43 | |
44 | } // namespace duckdb |
45 |