1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/storage/storage_lock.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/constants.hpp"
12#include "duckdb/common/atomic.hpp"
13#include "duckdb/common/mutex.hpp"
14
15namespace duckdb {
16class StorageLock;
17
18enum class StorageLockType { SHARED = 0, EXCLUSIVE = 1 };
19
20class StorageLockKey {
21public:
22 StorageLockKey(StorageLock &lock, StorageLockType type);
23 ~StorageLockKey();
24
25private:
26 StorageLock &lock;
27 StorageLockType type;
28};
29
30class StorageLock {
31 friend class StorageLockKey;
32
33public:
34 StorageLock();
35
36 //! Get an exclusive lock
37 unique_ptr<StorageLockKey> GetExclusiveLock();
38 //! Get a shared lock
39 unique_ptr<StorageLockKey> GetSharedLock();
40
41private:
42 mutex exclusive_lock;
43 atomic<idx_t> read_count;
44
45private:
46 //! Release an exclusive lock
47 void ReleaseExclusiveLock();
48 //! Release a shared lock
49 void ReleaseSharedLock();
50};
51
52} // namespace duckdb
53