stage/store_lock.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <atomic> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <functional> | ||
| 8 | #include <mutex> | ||
| 9 | #include <optional> | ||
| 10 | #include <thread> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #include "chrono_batch_render/export.h" | ||
| 14 | |||
| 15 | namespace chrono::batch_render { | ||
| 16 | |||
| 17 | 377 | class CHRONO_BATCH_RENDER_API StoreLock { | |
| 18 | public: | ||
| 19 | class CHRONO_BATCH_RENDER_API Guard { | ||
| 20 | public: | ||
| 21 | 69 | Guard() = default; | |
| 22 | explicit Guard(const StoreLock& lock); | ||
| 23 | Guard(const StoreLock& lock, std::try_to_lock_t); | ||
| 24 | ~Guard(); | ||
| 25 | Guard(const Guard&) = delete; | ||
| 26 | Guard& operator=(const Guard&) = delete; | ||
| 27 | Guard(Guard&& other) noexcept; | ||
| 28 | Guard& operator=(Guard&& other) noexcept; | ||
| 29 | |||
| 30 | 250662 | bool OwnsLock() const { | |
| 31 | 250662 | return m_lock != nullptr; | |
| 32 | } | ||
| 33 | |||
| 34 | private: | ||
| 35 | void Enter(); | ||
| 36 | void Leave() noexcept; | ||
| 37 | |||
| 38 | const StoreLock* m_lock = nullptr; | ||
| 39 | }; | ||
| 40 | |||
| 41 | 377 | StoreLock() = default; | |
| 42 | StoreLock(const StoreLock&) = delete; | ||
| 43 | StoreLock& operator=(const StoreLock&) = delete; | ||
| 44 | |||
| 45 | 1044299 | [[nodiscard]] Guard Lock() const { | |
| 46 | 1044297 | return Guard(*this); | |
| 47 | } | ||
| 48 | |||
| 49 | [[nodiscard]] std::optional<Guard> TryLock() const; | ||
| 50 | |||
| 51 | 225095 | bool HeldByThisThread() const { | |
| 52 | 225095 | return m_owner.load(std::memory_order_relaxed) == std::this_thread::get_id(); | |
| 53 | } | ||
| 54 | |||
| 55 | void Check(const char* what) const; | ||
| 56 | |||
| 57 | void Defer(std::function<void()> write) const; | ||
| 58 | |||
| 59 | bool CanDefer() const; | ||
| 60 | |||
| 61 | static constexpr size_t kMaxDeferred = 256; | ||
| 62 | |||
| 63 | void DropDeferred() const; | ||
| 64 | |||
| 65 | using BlockingWait = void (*)(void (*wait)(void* context), void* context); | ||
| 66 | static void SetBlockingWait(BlockingWait blockingWait); | ||
| 67 | |||
| 68 | private: | ||
| 69 | 937467 | bool HasDeferred() const { | |
| 70 | 937467 | return m_hasDeferred.load(std::memory_order_acquire); | |
| 71 | } | ||
| 72 | |||
| 73 | void RunDeferred() const; | ||
| 74 | |||
| 75 | mutable std::recursive_mutex m_mutex; | ||
| 76 | mutable std::atomic<std::thread::id> m_owner{}; | ||
| 77 | mutable uint32_t m_depth = 0; | ||
| 78 | mutable std::mutex m_deferredMutex; | ||
| 79 | mutable std::vector<std::function<void()>> m_deferred; | ||
| 80 | mutable std::atomic<bool> m_hasDeferred{false}; | ||
| 81 | }; | ||
| 82 | |||
| 83 | 546374 | [[nodiscard]] inline StoreLock::Guard LockStore(const StoreLock* lock) { | |
| 84 | 546375 | return lock ? lock->Lock() : StoreLock::Guard(); | |
| 85 | } | ||
| 86 | |||
| 87 | 260068 | inline void CheckStoreLock(const StoreLock* lock, const char* what) { | |
| 88 | 260068 | if (lock) { | |
| 89 | 225090 | lock->Check(what); | |
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | } | ||
| 94 |