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 | |
15 | namespace duckdb { |
16 | class StorageLock; |
17 | |
18 | enum class StorageLockType { SHARED = 0, EXCLUSIVE = 1 }; |
19 | |
20 | class StorageLockKey { |
21 | public: |
22 | StorageLockKey(StorageLock &lock, StorageLockType type); |
23 | ~StorageLockKey(); |
24 | |
25 | private: |
26 | StorageLock &lock; |
27 | StorageLockType type; |
28 | }; |
29 | |
30 | class StorageLock { |
31 | friend class StorageLockKey; |
32 | |
33 | public: |
34 | StorageLock(); |
35 | |
36 | //! Get an exclusive lock |
37 | unique_ptr<StorageLockKey> GetExclusiveLock(); |
38 | //! Get a shared lock |
39 | unique_ptr<StorageLockKey> GetSharedLock(); |
40 | |
41 | private: |
42 | mutex exclusive_lock; |
43 | atomic<idx_t> read_count; |
44 | |
45 | private: |
46 | //! Release an exclusive lock |
47 | void ReleaseExclusiveLock(); |
48 | //! Release a shared lock |
49 | void ReleaseSharedLock(); |
50 | }; |
51 | |
52 | } // namespace duckdb |
53 | |