CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
AppleDispatchQueue.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
11
13
15
16#include <dispatch/dispatch.h>
17
18namespace CeresEngine {
19
80
84 friend class AppleDispatchQueue;
85
86 private:
89
91 ExecutorBlocking mBlockingProperty = ExecutorBlocking::never;
92
95
96 private:
105 : mExecutionContext(&executionContext), mBlockingProperty(blockingProperty), mPriority(priority) {}
106
107 public:
108 Executor(const Executor&) = default;
109 Executor& operator=(const Executor&) = default;
110 Executor(Executor&&) = default;
112
113 public:
115 [[nodiscard]] AppleDispatchQueue& context() const noexcept { return *mExecutionContext; }
116
119 CE_ASSERT(mExecutionContext != nullptr);
120 mExecutionContext->onWorkStarted();
121 }
122
125 CE_ASSERT(mExecutionContext != nullptr);
126 mExecutionContext->onWorkFinished();
127 }
128
129 public:
131 [[nodiscard]] friend bool operator==(const Executor& lhs, const Executor& rhs) noexcept {
132 return lhs.mExecutionContext == rhs.mExecutionContext && lhs.mBlockingProperty == rhs.mBlockingProperty;
133 }
134
136 [[nodiscard]] friend bool operator!=(const Executor& lhs, const Executor& rhs) noexcept { return !(lhs == rhs); }
137
138 private:
139 friend struct asio_query_fn::impl;
140 friend struct asio::execution::detail::context_t<0>;
141 friend struct asio::execution::detail::mapping_t<0>;
142 friend struct asio::execution::detail::blocking_t<0>;
143 friend struct asio::execution::detail::relationship_t<0>;
144 friend struct asio::execution::detail::outstanding_work_t<0>;
145 friend struct asio::execution::detail::bulk_guarantee_t<0>;
146 friend struct asio::execution::detail::occupancy_t<0>;
147
149 [[nodiscard]] AppleDispatchQueue& query(ExecutorExecutionContext) const noexcept { return context(); }
150
152 [[nodiscard]] static constexpr ExecutorMapping query(ExecutorMapping) noexcept { return ExecutorMapping::other; }
153
155 [[nodiscard]] ExecutorBlocking query(ExecutorBlocking) const noexcept { return mBlockingProperty; }
156
158 [[nodiscard]] ExecutorPriority query(ExecutorPriority) const noexcept { return mPriority; }
159
161 [[nodiscard]] static constexpr ExecutorRelationship query(ExecutorRelationship) noexcept { return ExecutorRelationship::continuation; }
162
164 [[nodiscard]] static constexpr ExecutorOutstandingWork query(ExecutorOutstandingWork) noexcept { return ExecutorOutstandingWork::tracked; }
165
167 [[nodiscard]] static constexpr ExecutorBulkGuarantee query(ExecutorBulkGuarantee) noexcept { return ExecutorBulkGuarantee::sequenced; }
168
171 [[nodiscard]] std::size_t query(ExecutorOccupancy) const noexcept {
172 CE_ASSERT(mExecutionContext != nullptr);
173 switch(mExecutionContext->mType) {
174 case DispatchQueueType::Serial: return 1;
175 default: return std::thread::hardware_concurrency();
176 }
177 }
178
180 template<typename OtherAllocator> [[nodiscard]] std::allocator<void> query(ExecutorAllocator<OtherAllocator>) const noexcept {
181 static std::allocator<void> allocator;
182 return allocator;
183 }
184
186 [[nodiscard]] std::allocator<void> query(ExecutorAllocator<void>) const noexcept {
187 static std::allocator<void> allocator;
188 return allocator;
189 }
190
191 private: // Prefer
192 friend struct asio_require_fn::impl;
193 friend struct asio_prefer_fn::impl;
194
205 [[nodiscard]] Executor require(ExecutorBlocking blocking) const { return Executor(*mExecutionContext, blocking, mPriority); }
206
217 [[nodiscard]] Executor require(ExecutorPriority newPriority) const { return Executor(*mExecutionContext, mBlockingProperty, newPriority); }
218
219 private:
220 friend struct asio_execution_execute_fn::impl;
221
223 template<typename Function> void execute(ASIO_MOVE_ARG(Function) f) const {
224 CE_ASSERT(mExecutionContext != nullptr);
225 mExecutionContext->schedule(std::move(f), mBlockingProperty, mPriority);
226 }
227 };
228
231
232} // namespace CeresEngine
#define CE_ASSERT(...)
Definition Macros.hpp:323
An executor object that dispatches calls to a libdispatch queue.
Definition AppleDispatchQueue.hpp:83
Executor & operator=(const Executor &)=default
static constexpr ExecutorOutstandingWork query(ExecutorOutstandingWork) noexcept
Query the current value of the outstanding_work property.
Definition AppleDispatchQueue.hpp:164
Executor & operator=(Executor &&)=default
Executor require(ExecutorPriority newPriority) const
Obtain an executor with the given priority property.
Definition AppleDispatchQueue.hpp:217
AppleDispatchQueue & context() const noexcept
Obtain the underlying execution context.
Definition AppleDispatchQueue.hpp:115
Executor(const Executor &)=default
friend bool operator!=(const Executor &lhs, const Executor &rhs) noexcept
Compare two executors for inequality.
Definition AppleDispatchQueue.hpp:136
ExecutorBlocking query(ExecutorBlocking) const noexcept
Query the current value of the blocking property.
Definition AppleDispatchQueue.hpp:155
std::allocator< void > query(ExecutorAllocator< void >) const noexcept
Query the current value of the allocator property.
Definition AppleDispatchQueue.hpp:186
void execute(ASIO_MOVE_ARG(Function) f) const
Execution function.
Definition AppleDispatchQueue.hpp:223
AppleDispatchQueue & query(ExecutorExecutionContext) const noexcept
Query the current value of the context property.
Definition AppleDispatchQueue.hpp:149
Executor require(ExecutorBlocking blocking) const
Obtain an executor with the given blocking property.
Definition AppleDispatchQueue.hpp:205
static constexpr ExecutorRelationship query(ExecutorRelationship) noexcept
Query the current value of the relationship property.
Definition AppleDispatchQueue.hpp:161
static constexpr ExecutorMapping query(ExecutorMapping) noexcept
Query the current value of the mapping property.
Definition AppleDispatchQueue.hpp:152
friend bool operator==(const Executor &lhs, const Executor &rhs) noexcept
Compare two executors for equality.
Definition AppleDispatchQueue.hpp:131
ExecutorPriority query(ExecutorPriority) const noexcept
Query the current value of the priority property.
Definition AppleDispatchQueue.hpp:158
std::allocator< void > query(ExecutorAllocator< OtherAllocator >) const noexcept
Query the current value of the allocator property.
Definition AppleDispatchQueue.hpp:180
void on_work_finished() const noexcept
Inform the executor that some work is no longer outstanding.
Definition AppleDispatchQueue.hpp:124
std::size_t query(ExecutorOccupancy) const noexcept
Query the occupancy (recommended number of work items) for the system context.
Definition AppleDispatchQueue.hpp:171
static constexpr ExecutorBulkGuarantee query(ExecutorBulkGuarantee) noexcept
Query the current value of the bulk_guarantee property.
Definition AppleDispatchQueue.hpp:167
Executor(AppleDispatchQueue &executionContext, ExecutorBlocking blockingProperty=ExecutorBlocking::never, ExecutorPriority priority=ExecutorPriority::Normal)
Creates a new Executor instance.
Definition AppleDispatchQueue.hpp:103
void on_work_started() const noexcept
Inform the executor that it has some outstanding work to do.
Definition AppleDispatchQueue.hpp:118
AppleDispatchQueue * mExecutionContext
The execution context that the executor will use.
Definition AppleDispatchQueue.hpp:88
Abstracts a libdispatch queue as an ASIO execution context.
Definition AppleDispatchQueue.hpp:21
AnyExecutor getExecutor() noexcept final
Returns an executor that run execute scheduled commands on the context.
Definition AppleDispatchQueue.hpp:229
dispatch_queue_t mQueue
The dispatch_queue_t instance to execute commands on.
Definition AppleDispatchQueue.hpp:27
AppleDispatchQueue(const String &name, DispatchQueueType type, AppleDispatchQueue *const queue=nullptr) noexcept
Creates a new Apple queue instance.
void resume() final
Resumes the invocation of block objects on a queue.
void onWorkStarted() noexcept
Inform the executor that it has some outstanding work to do.
dispatch_queue_t getDispatchQueue() const noexcept
The dispatch_queue_t instance to execute commands on.
Definition AppleDispatchQueue.hpp:60
executor_type get_executor() noexcept
Returns an executor that run execute scheduled commands on the context.
Definition AppleDispatchQueue.hpp:230
DispatchQueueType mType
Determines the type of dispatch queue.
Definition AppleDispatchQueue.hpp:30
void suspend() final
Suspends the invocation of function objects on a queue.
void onWorkFinished() noexcept
Inform the executor that some work is no longer outstanding.
UPtr< DispatchQueue > createSubQueue(DispatchQueueType type, StringView name="") final
Resumes the invocation of block objects on a queue.
void schedule(UniqueFunction< void()> function, ExecutorBlocking blockingProperty, ExecutorPriority priority)
Schedules a new function to be run in the run loop.
An object that manages the execution of tasks serially or concurrently on your app's main thread or o...
Definition DispatchQueue.hpp:36
Definition Application.hpp:19
std::unique_ptr< T, Deleter > UPtr
UPtr is a smart pointer that owns and manages another object through a pointer and disposes of that o...
Definition SmartPtr.hpp:28
asio::execution::context_t ExecutorExecutionContext
Definition ExecutionContext.hpp:208
asio::execution::relationship_t ExecutorRelationship
Definition ExecutionContext.hpp:215
asio::execution::mapping_t ExecutorMapping
Definition ExecutionContext.hpp:211
asio::execution::bulk_guarantee_t ExecutorBulkGuarantee
Definition ExecutionContext.hpp:214
asio::execution::occupancy_t ExecutorOccupancy
Definition ExecutionContext.hpp:212
FunctionBase< true, true, fu2::capacity_default, true, false, Signatures... > Function
An owning copyable function wrapper for arbitrary callable types.
Definition Function.hpp:54
DispatchQueueType
Specifies the type of the queue.
Definition DispatchQueue.hpp:17
@ Serial
A dispatch queue that executes blocks serially in FIFO order.
asio::execution::allocator_t< T > ExecutorAllocator
Definition ExecutionContext.hpp:216
asio::execution::blocking_t ExecutorBlocking
Definition ExecutionContext.hpp:210
asio::execution::outstanding_work_t ExecutorOutstandingWork
Definition ExecutionContext.hpp:213
ASIO_MOVE_ARG(CompletionToken) token)
Definition IdleService.hpp:42
FunctionBase< true, false, fu2::capacity_default, true, false, Signatures... > UniqueFunction
An owning non copyable function wrapper for arbitrary callable types.
Definition Function.hpp:59
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Determines the priority to which an executor should schedule its tasks.
Definition ExecutionContext.hpp:220
static const ExecutorPriority Normal
The default value for regular tasks.
Definition ExecutionContext.hpp:250