Photon Editor Library 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TConcurrentHandleDispatcher.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <Utility/Concurrent/TAtomicQuasiQueue.h>
6#include <Utility/IMoveOnly.h>
7#include <Common/assertion.h>
8
9#include <atomic>
10#include <utility>
11
12namespace ph::editor
13{
14
15template<CWeakHandle Handle>
16class TConcurrentHandleDispatcher final : private IMoveOnly
17{
18 using Index = typename Handle::IndexType;
19 using Generation = typename Handle::GenerationType;
20
21public:
22 using HandleType = Handle;
23
25 : m_handles()
26 , m_nextNewIdx(0)
27 {}
28
31 {
32 swap(*this, other);
33 }
34
36 {
37 swap(*this, rhs);
38
39 return *this;
40 }
41
45 [[nodiscard]]
46 inline Handle dispatchOne()
47 {
48 Handle handle;
49 if(m_handles.tryDequeue(&handle))
50 {
51 return handle;
52 }
53
54 // Create new handle if we cannot obtain an existing one
55 constexpr auto initialGeneration = Handle::nextGeneration(Handle::INVALID_GENERATION);
56 const Index newIdx = m_nextNewIdx.fetch_add(1, std::memory_order_relaxed);
57 return Handle(newIdx, initialGeneration);
58 }
59
63 inline void returnOne(const Handle& handle)
64 {
65 PH_ASSERT(handle);
66 m_handles.enqueue(handle);
67 }
68
69 inline friend void swap(TConcurrentHandleDispatcher& first, TConcurrentHandleDispatcher& second) noexcept
70 {
71 // Enable ADL
72 using std::swap;
73
74 swap(first.m_handles, second.m_handles);
75
76 Index firstIdx = first.m_nextNewIdx.exchange(second.m_nextNewIdx.load());
77 second.m_nextNewIdx.store(firstIdx);
78 }
79
80private:
81 TAtomicQuasiQueue<Handle> m_handles;
82 std::atomic<Index> m_nextNewIdx;
83};
84
85}// end namespace ph::editor
Definition TConcurrentHandleDispatcher.h:17
TConcurrentHandleDispatcher()
Definition TConcurrentHandleDispatcher.h:24
TConcurrentHandleDispatcher(TConcurrentHandleDispatcher &&other) noexcept
Definition TConcurrentHandleDispatcher.h:29
TConcurrentHandleDispatcher & operator=(TConcurrentHandleDispatcher &&rhs) noexcept
Definition TConcurrentHandleDispatcher.h:35
void returnOne(const Handle &handle)
Recycle one handle.
Definition TConcurrentHandleDispatcher.h:63
Handle dispatchOne()
Get one handle.
Definition TConcurrentHandleDispatcher.h:46
Handle HandleType
Definition TConcurrentHandleDispatcher.h:22
friend void swap(TConcurrentHandleDispatcher &first, TConcurrentHandleDispatcher &second) noexcept
Definition TConcurrentHandleDispatcher.h:69
Definition ph_editor.h:10