CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
EmscriptenRunLoop.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
14
15#include <asio/execution.hpp>
16
17namespace CeresEngine {
18
20 class EmscriptenRunLoop : public RunLoop {
21 public:
24 class Executor {
25 friend class EmscriptenRunLoop;
26
27 private:
30
32 ExecutorBlocking mBlockingProperty = ExecutorBlocking::never;
33
34 private:
41
42 public:
43 Executor(const Executor&) = default;
44 Executor& operator=(const Executor&) = default;
45 Executor(Executor&&) = default;
47
48 public:
51
57
63
64 public:
66 [[nodiscard]] friend bool operator==(const Executor&, const Executor&) noexcept { return true; }
67
69 [[nodiscard]] friend bool operator!=(const Executor&, const Executor&) noexcept { return false; }
70
71 private:
72 friend struct asio_query_fn::impl;
73 friend struct asio::execution::detail::context_t<0>;
74 friend struct asio::execution::detail::mapping_t<0>;
75 friend struct asio::execution::detail::blocking_t<0>;
76 friend struct asio::execution::detail::relationship_t<0>;
77 friend struct asio::execution::detail::outstanding_work_t<0>;
78 friend struct asio::execution::detail::bulk_guarantee_t<0>;
79 friend struct asio::execution::detail::occupancy_t<0>;
80
83
85 [[nodiscard]] static constexpr ExecutorMapping query(ExecutorMapping) noexcept { return ExecutorMapping::other; }
86
89
91 [[nodiscard]] static constexpr ExecutorRelationship query(ExecutorRelationship) noexcept { return ExecutorRelationship::continuation; }
92
94 [[nodiscard]] static constexpr ExecutorOutstandingWork query(ExecutorOutstandingWork) noexcept { return ExecutorOutstandingWork::tracked; }
95
97 [[nodiscard]] static constexpr ExecutorBulkGuarantee query(ExecutorBulkGuarantee) noexcept { return ExecutorBulkGuarantee::sequenced; }
98
101 [[nodiscard]] std::size_t query(ExecutorOccupancy) const noexcept { return 1; }
102
104 template<typename OtherAllocator> [[nodiscard]] std::allocator<void> query(ExecutorAllocator<OtherAllocator>) const noexcept {
105 static std::allocator<void> allocator;
106 return allocator;
107 }
108
110 [[nodiscard]] std::allocator<void> query(ExecutorAllocator<void>) const noexcept {
111 static std::allocator<void> allocator;
112 return allocator;
113 }
114
115 private: // Prefer
117 friend struct asio_prefer_fn::impl;
118
130
131 private:
133
135 template<typename Function> void execute(ASIO_MOVE_ARG(Function) f) const {
136 CE_ASSERT(mExecutionContext != nullptr);
138 }
139 };
140
141 private:
144
147
148 public:
151
154
155 public:
158
160 void stop() final;
161
163 void run() final;
164
165 private: // Executor interface
168
171
175
176 public: // ASIO ExecutionContext concept
179
182 };
183
184} // namespace CeresEngine
#define CE_ASSERT(...)
Definition Macros.hpp:323
An executor object that dispatches calls to the CoreFoundation CFRunLoop.
Definition EmscriptenRunLoop.hpp:24
ExecutorBlocking query(ExecutorBlocking) const noexcept
Query the current value of the blocking property.
Definition EmscriptenRunLoop.hpp:88
Executor(const Executor &)=default
friend struct asio_prefer_fn::impl
Definition EmscriptenRunLoop.hpp:117
EmscriptenRunLoop * mExecutionContext
The execution context that the executor will use.
Definition EmscriptenRunLoop.hpp:29
static constexpr ExecutorRelationship query(ExecutorRelationship) noexcept
Query the current value of the relationship property.
Definition EmscriptenRunLoop.hpp:91
void on_work_started() const noexcept
Inform the executor that it has some outstanding work to do.
Definition EmscriptenRunLoop.hpp:53
Executor(EmscriptenRunLoop *const executionContext, const ExecutorBlocking blockingProperty=ExecutorBlocking::never)
Creates a new Executor instance.
Definition EmscriptenRunLoop.hpp:39
static constexpr ExecutorBulkGuarantee query(ExecutorBulkGuarantee) noexcept
Query the current value of the bulk_guarantee property.
Definition EmscriptenRunLoop.hpp:97
void execute(ASIO_MOVE_ARG(Function) f) const
Execution function.
Definition EmscriptenRunLoop.hpp:135
friend struct asio_execution_execute_fn::impl
Definition EmscriptenRunLoop.hpp:132
EmscriptenRunLoop & query(ExecutorExecutionContext) const noexcept
Query the current value of the context property.
Definition EmscriptenRunLoop.hpp:82
static constexpr ExecutorMapping query(ExecutorMapping) noexcept
Query the current value of the mapping property.
Definition EmscriptenRunLoop.hpp:85
Executor require(const ExecutorBlocking blocking) const
Obtain an executor with the given blocking property.
Definition EmscriptenRunLoop.hpp:129
friend struct asio_require_fn::impl
Definition EmscriptenRunLoop.hpp:116
friend bool operator!=(const Executor &, const Executor &) noexcept
Compare two executors for inequality.
Definition EmscriptenRunLoop.hpp:69
friend bool operator==(const Executor &, const Executor &) noexcept
Compare two executors for equality.
Definition EmscriptenRunLoop.hpp:66
Executor & operator=(const Executor &)=default
ExecutorBlocking mBlockingProperty
A property that customizes the blocking behavior of the executor.
Definition EmscriptenRunLoop.hpp:32
static constexpr ExecutorOutstandingWork query(ExecutorOutstandingWork) noexcept
Query the current value of the outstanding_work property.
Definition EmscriptenRunLoop.hpp:94
Executor & operator=(Executor &&)=default
void on_work_finished() const noexcept
Inform the executor that some work is no longer outstanding.
Definition EmscriptenRunLoop.hpp:59
std::allocator< void > query(ExecutorAllocator< void >) const noexcept
Query the current value of the allocator property.
Definition EmscriptenRunLoop.hpp:110
std::size_t query(ExecutorOccupancy) const noexcept
Query the occupancy (recommended number of work items) for the system context.
Definition EmscriptenRunLoop.hpp:101
friend struct asio_query_fn::impl
Definition EmscriptenRunLoop.hpp:72
EmscriptenRunLoop & context() const noexcept
Obtain the underlying execution context.
Definition EmscriptenRunLoop.hpp:50
std::allocator< void > query(ExecutorAllocator< OtherAllocator >) const noexcept
Query the current value of the allocator property.
Definition EmscriptenRunLoop.hpp:104
Abstracts a CoreFoundation CFRunLoop as an ASIO execution context.
Definition EmscriptenRunLoop.hpp:20
void onWorkFinished() noexcept
Inform the executor that some work is no longer outstanding.
executor_type get_executor() noexcept
Returns an executor that run execute scheduled commands on the context.
Definition EmscriptenRunLoop.hpp:181
EmscriptenRunLoop() noexcept
Creates a new emscripten run loop instance for the current thread.
void schedule(UniqueFunction< void()> function, ExecutorBlocking blockingProperty)
Schedules a new function to be run in the run loop.
void stop() final
Stops the execution of the run loop.
AnyExecutor getExecutor() noexcept final
Returns an executor that run execute scheduled commands on the context.
void onWorkStarted() noexcept
Inform the executor that it has some outstanding work to do.
Scope mScope
A scope for the run loop object.
Definition EmscriptenRunLoop.hpp:146
void run() final
Starts running the run loop.
pthread_t mThread
The CoreFoundation CFRunLoop instance to execute commands on.
Definition EmscriptenRunLoop.hpp:143
Scope object that sets the current execution context as current.
Definition ExecutionContext.hpp:180
An ASIO execution context that wraps the operating system run loop.
Definition RunLoop.hpp:17
Definition Application.hpp:19
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
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