diff --git a/Core/AppRuntime/CMakeLists.txt b/Core/AppRuntime/CMakeLists.txt index 400783488..b7e24b38e 100644 --- a/Core/AppRuntime/CMakeLists.txt +++ b/Core/AppRuntime/CMakeLists.txt @@ -2,10 +2,10 @@ set(SOURCES "Include/Babylon/Dispatchable.h" "Include/Babylon/AppRuntime.h" "Source/AppRuntime.cpp" - "Source/AppRuntime_${NAPI_JAVASCRIPT_ENGINE}.cpp" - "Source/AppRuntime_${BABYLON_NATIVE_PLATFORM}.${BABYLON_NATIVE_PLATFORM_IMPL_EXT}" - "Source/WorkQueue.cpp" - "Source/WorkQueue.h") + "Source/AppRuntimeImpl.h" + "Source/AppRuntimeImpl.cpp" + "Source/AppRuntimeImpl_${NAPI_JAVASCRIPT_ENGINE}.cpp" + "Source/AppRuntimeImpl_${BABYLON_NATIVE_PLATFORM}.${BABYLON_NATIVE_PLATFORM_IMPL_EXT}") add_library(AppRuntime ${SOURCES}) warnings_as_errors(AppRuntime) diff --git a/Core/AppRuntime/Include/Babylon/AppRuntime.h b/Core/AppRuntime/Include/Babylon/AppRuntime.h index 70ddd5921..ab0a9f000 100644 --- a/Core/AppRuntime/Include/Babylon/AppRuntime.h +++ b/Core/AppRuntime/Include/Babylon/AppRuntime.h @@ -1,7 +1,8 @@ #pragma once +#include + #include "Dispatchable.h" -#include #include #include @@ -9,7 +10,7 @@ namespace Babylon { - class WorkQueue; + class AppRuntimeImpl; class AppRuntime final { @@ -18,33 +19,16 @@ namespace Babylon AppRuntime(std::function unhandledExceptionHandler); ~AppRuntime(); + // Move semantics + AppRuntime(AppRuntime&&) noexcept; + AppRuntime& operator=(AppRuntime&&) noexcept; + void Suspend(); void Resume(); void Dispatch(Dispatchable callback); private: - // These three methods are the mechanism by which platform- and JavaScript-specific - // code can be "injected" into the execution of the JavaScript thread. These three - // functions are implemented in separate files, thus allowing implementations to be - // mixed and matched by the build system based on the platform and JavaScript engine - // being targeted, without resorting to virtuality. An important nuance of these - // functions is that they are all intended to call each other: RunPlatformTier MUST - // call RunEnvironmentTier, which MUST create the initial Napi::Env and pass it to - // Run. This arrangement allows not only for an arbitrary assemblage of platforms, - // but it also allows us to respect the requirement by certain platforms (notably V8) - // that certain program state be allocated and stored only on the stack. - void RunPlatformTier(); - void RunEnvironmentTier(const char* executablePath = "."); - void Run(Napi::Env); - - // This method is called from Dispatch to allow platform-specific code to add - // extra logic around the invocation of a dispatched callback. - void Execute(Dispatchable callback); - - static void DefaultUnhandledExceptionHandler(const std::exception& error); - - std::unique_ptr m_workQueue{}; - std::function m_unhandledExceptionHandler{}; + std::unique_ptr m_impl; }; } diff --git a/Core/AppRuntime/Source/AppRuntime.cpp b/Core/AppRuntime/Source/AppRuntime.cpp index 0e6833a2c..ca0ea96f5 100644 --- a/Core/AppRuntime/Source/AppRuntime.cpp +++ b/Core/AppRuntime/Source/AppRuntime.cpp @@ -1,65 +1,36 @@ #include "AppRuntime.h" -#include "WorkQueue.h" -#include +#include "AppRuntimeImpl.h" namespace Babylon { AppRuntime::AppRuntime() - : AppRuntime{DefaultUnhandledExceptionHandler} + : m_impl{std::make_unique()} { } AppRuntime::AppRuntime(std::function unhandledExceptionHandler) - : m_workQueue{std::make_unique([this] { RunPlatformTier(); })} - , m_unhandledExceptionHandler{unhandledExceptionHandler} + : m_impl{std::make_unique(unhandledExceptionHandler)} { - Dispatch([this](Napi::Env env) { - JsRuntime::CreateForJavaScript(env, [this](auto func) { Dispatch(std::move(func)); }); - }); } - AppRuntime::~AppRuntime() - { - // Notify the JsRuntime on the JavaScript thread that the JavaScript - // runtime shutdown sequence has begun. The JsRuntimeScheduler will - // use this signal to gracefully cancel asynchronous operations. - Dispatch([](Napi::Env env) { - JsRuntime::NotifyDisposing(JsRuntime::GetFromJavaScript(env)); - }); - } + AppRuntime::~AppRuntime() = default; - void AppRuntime::Run(Napi::Env env) - { - m_workQueue->Run(env); - } + // Move semantics + AppRuntime::AppRuntime(AppRuntime&&) noexcept = default; + AppRuntime& AppRuntime::operator=(AppRuntime&&) noexcept = default; void AppRuntime::Suspend() { - m_workQueue->Suspend(); + m_impl->Suspend(); } void AppRuntime::Resume() { - m_workQueue->Resume(); + m_impl->Resume(); } - void AppRuntime::Dispatch(Dispatchable func) + void AppRuntime::Dispatch(Dispatchable callback) { - m_workQueue->Append([this, func{std::move(func)}](Napi::Env env) mutable { - Execute([this, env, func{std::move(func)}]() mutable { - try - { - func(env); - } - catch (const std::exception& error) - { - m_unhandledExceptionHandler(error); - } - catch (...) - { - std::abort(); - } - }); - }); + m_impl->Dispatch(std::move(callback)); } } diff --git a/Core/AppRuntime/Source/AppRuntimeImpl.cpp b/Core/AppRuntime/Source/AppRuntimeImpl.cpp new file mode 100644 index 000000000..9e28faf1e --- /dev/null +++ b/Core/AppRuntime/Source/AppRuntimeImpl.cpp @@ -0,0 +1,88 @@ +#include "AppRuntimeImpl.h" +#include + +namespace Babylon +{ + AppRuntimeImpl::AppRuntimeImpl(std::function unhandledExceptionHandler) + : m_unhandledExceptionHandler{std::move(unhandledExceptionHandler)} + , m_thread{[this] { RunPlatformTier(); }} + { + Dispatch([this](Napi::Env env) { + JsRuntime::CreateForJavaScript(env, [this](auto func) { Dispatch(std::move(func)); }); + }); + } + + AppRuntimeImpl::~AppRuntimeImpl() + { + if (m_suspensionLock.has_value()) + { + m_suspensionLock.reset(); + } + + Dispatch([this](Napi::Env env) { + // Notify the JsRuntime on the JavaScript thread that the JavaScript runtime shutdown sequence has + // begun. The JsRuntimeScheduler will use this signal to gracefully cancel asynchronous operations. + JsRuntime::NotifyDisposing(JsRuntime::GetFromJavaScript(env)); + + // Cancel on the JavaScript thread to signal the Run function to gracefully end. It must be + // dispatched and not canceled directly to ensure that existing work is executed and executed in + // the correct order. + m_cancellationSource.cancel(); + }); + + m_thread.join(); + } + + void AppRuntimeImpl::Suspend() + { + auto suspensionMutex = std::make_shared(); + m_suspensionLock.emplace(*suspensionMutex); + Append([suspensionMutex = std::move(suspensionMutex)](Napi::Env) { + std::scoped_lock lock{*suspensionMutex}; + }); + } + + void AppRuntimeImpl::Resume() + { + m_suspensionLock.reset(); + } + + void AppRuntimeImpl::Dispatch(Dispatchable func) + { + Append([this, func{std::move(func)}](Napi::Env env) mutable { + Execute([this, env, func{std::move(func)}]() mutable { + try + { + func(env); + } + catch (const std::exception& error) + { + m_unhandledExceptionHandler(error); + } + catch (...) + { + std::abort(); + } + }); + }); + } + + void AppRuntimeImpl::Run(Napi::Env env) + { + m_env = std::make_optional(env); + + m_dispatcher.set_affinity(std::this_thread::get_id()); + + while (!m_cancellationSource.cancelled()) + { + m_dispatcher.blocking_tick(m_cancellationSource); + } + + // The dispatcher can be non-empty if something is dispatched after cancellation. + // For example, Chakra's JsSetPromiseContinuationCallback may potentially dispatch + // a continuation after cancellation. + m_dispatcher.clear(); + + m_env.reset(); + } +} diff --git a/Core/AppRuntime/Source/AppRuntimeImpl.h b/Core/AppRuntime/Source/AppRuntimeImpl.h new file mode 100644 index 000000000..a3bc12104 --- /dev/null +++ b/Core/AppRuntime/Source/AppRuntimeImpl.h @@ -0,0 +1,68 @@ +#pragma once + +#include "AppRuntime.h" +#include +#include +#include + +namespace Babylon +{ + class AppRuntimeImpl + { + public: + AppRuntimeImpl(std::function unhandledExceptionHandler = DefaultUnhandledExceptionHandler); + ~AppRuntimeImpl(); + + void Suspend(); + void Resume(); + + void Dispatch(Dispatchable func); + + private: + static void DefaultUnhandledExceptionHandler(const std::exception& error); + + template + void Append(CallableT callable) + { + // Manual dispatcher queueing requires a copyable CallableT, we use a shared pointer trick to make a + // copyable callable if necessary. + if constexpr (std::is_copy_constructible::value) + { + m_dispatcher.queue([this, callable = std::move(callable)]() { + callable(m_env.value()); + }); + } + else + { + m_dispatcher.queue([this, callablePtr = std::make_shared(std::move(callable))]() { + (*callablePtr)(m_env.value()); + }); + } + } + + // These three methods are the mechanism by which platform- and JavaScript-specific + // code can be "injected" into the execution of the JavaScript thread. These three + // functions are implemented in separate files, thus allowing implementations to be + // mixed and matched by the build system based on the platform and JavaScript engine + // being targeted, without resorting to virtuality. An important nuance of these + // functions is that they are all intended to call each other: RunPlatformTier MUST + // call RunEnvironmentTier, which MUST create the initial Napi::Env and pass it to + // Run. This arrangement allows not only for an arbitrary assemblage of platforms, + // but it also allows us to respect the requirement by certain platforms (notably V8) + // that certain program state be allocated and stored only on the stack. + void RunPlatformTier(); + void RunEnvironmentTier(const char* executablePath = "."); + void Run(Napi::Env); + + // This method is called from Dispatch to allow platform-specific code to add + // extra logic around the invocation of a dispatched callback. + void Execute(Dispatchable callback); + + std::function m_unhandledExceptionHandler{}; + std::optional m_env{}; + std::optional> m_suspensionLock{}; + arcana::cancellation_source m_cancellationSource{}; + arcana::manual_dispatcher<128> m_dispatcher{}; + std::thread m_thread{}; + }; +} diff --git a/Core/AppRuntime/Source/AppRuntime_Android.cpp b/Core/AppRuntime/Source/AppRuntimeImpl_Android.cpp similarity index 61% rename from Core/AppRuntime/Source/AppRuntime_Android.cpp rename to Core/AppRuntime/Source/AppRuntimeImpl_Android.cpp index a87db5681..6a1092555 100644 --- a/Core/AppRuntime/Source/AppRuntime_Android.cpp +++ b/Core/AppRuntime/Source/AppRuntimeImpl_Android.cpp @@ -1,23 +1,23 @@ -#include "AppRuntime.h" +#include "AppRuntimeImpl.h" #include #include #include namespace Babylon { - void AppRuntime::RunPlatformTier() + void AppRuntimeImpl::RunPlatformTier() { RunEnvironmentTier(); } - void AppRuntime::DefaultUnhandledExceptionHandler(const std::exception& error) + void AppRuntimeImpl::DefaultUnhandledExceptionHandler(const std::exception& error) { std::stringstream ss{}; ss << "Uncaught Error: " << error.what() << std::endl; __android_log_write(ANDROID_LOG_ERROR, "BabylonNative", ss.str().data()); } - void AppRuntime::Execute(Dispatchable callback) + void AppRuntimeImpl::Execute(Dispatchable callback) { callback(); } diff --git a/Core/AppRuntime/Source/AppRuntime_Chakra.cpp b/Core/AppRuntime/Source/AppRuntimeImpl_Chakra.cpp similarity index 66% rename from Core/AppRuntime/Source/AppRuntime_Chakra.cpp rename to Core/AppRuntime/Source/AppRuntimeImpl_Chakra.cpp index d8200afc6..6d829e763 100644 --- a/Core/AppRuntime/Source/AppRuntime_Chakra.cpp +++ b/Core/AppRuntime/Source/AppRuntimeImpl_Chakra.cpp @@ -1,4 +1,4 @@ -#include "AppRuntime.h" +#include "AppRuntimeImpl.h" #include @@ -20,25 +20,23 @@ namespace Babylon } } - void AppRuntime::RunEnvironmentTier(const char*) + void AppRuntimeImpl::RunEnvironmentTier(const char*) { JsRuntimeHandle jsRuntime; ThrowIfFailed(JsCreateRuntime(JsRuntimeAttributeNone, nullptr, &jsRuntime)); JsContextRef context; ThrowIfFailed(JsCreateContext(jsRuntime, &context)); ThrowIfFailed(JsSetCurrentContext(context)); - ThrowIfFailed(JsSetPromiseContinuationCallback( - [](JsValueRef task, void* callbackState) { - auto* pThis = reinterpret_cast(callbackState); - ThrowIfFailed(JsAddRef(task, nullptr)); - pThis->Dispatch([task](auto) { - JsValueRef global; - ThrowIfFailed(JsGetGlobalObject(&global)); - ThrowIfFailed(JsCallFunction(task, &global, 1, nullptr)); - ThrowIfFailed(JsRelease(task, nullptr)); - }); - }, - this)); + ThrowIfFailed(JsSetPromiseContinuationCallback([](JsValueRef task, void* callbackState) { + auto* pThis = reinterpret_cast(callbackState); + ThrowIfFailed(JsAddRef(task, nullptr)); + pThis->Dispatch([task](auto) { + JsValueRef global; + ThrowIfFailed(JsGetGlobalObject(&global)); + ThrowIfFailed(JsCallFunction(task, &global, 1, nullptr)); + ThrowIfFailed(JsRelease(task, nullptr)); + }); + }, this)); ThrowIfFailed(JsProjectWinRTNamespace(L"Windows")); #if defined(_DEBUG) diff --git a/Core/AppRuntime/Source/AppRuntime_JSI.cpp b/Core/AppRuntime/Source/AppRuntimeImpl_JSI.cpp similarity index 76% rename from Core/AppRuntime/Source/AppRuntime_JSI.cpp rename to Core/AppRuntime/Source/AppRuntimeImpl_JSI.cpp index 2b94fcd19..34e880d40 100644 --- a/Core/AppRuntime/Source/AppRuntime_JSI.cpp +++ b/Core/AppRuntime/Source/AppRuntimeImpl_JSI.cpp @@ -1,5 +1,4 @@ -#include "AppRuntime.h" -#include "WorkQueue.h" +#include "AppRuntimeImpl.h" #include @@ -11,15 +10,15 @@ namespace class TaskRunnerAdapter : public v8runtime::JSITaskRunner { public: - TaskRunnerAdapter(Babylon::WorkQueue& workQueue) - : m_workQueue(workQueue) + TaskRunnerAdapter(Babylon::AppRuntimeImpl& appRuntimeImpl) + : m_appRuntimeImpl(appRuntimeImpl) { } void postTask(std::unique_ptr task) override { std::shared_ptr shared_task(task.release()); - m_workQueue.Append([shared_task2 = std::move(shared_task)](Napi::Env) { + m_appRuntimeImpl.Dispatch([shared_task2 = std::move(shared_task)](Napi::Env) { shared_task2->run(); }); } @@ -28,17 +27,17 @@ namespace TaskRunnerAdapter(const TaskRunnerAdapter&) = delete; TaskRunnerAdapter& operator=(const TaskRunnerAdapter&) = delete; - Babylon::WorkQueue& m_workQueue; + Babylon::AppRuntimeImpl& m_appRuntimeImpl; }; } namespace Babylon { - void AppRuntime::RunEnvironmentTier(const char*) + void AppRuntimeImpl::RunEnvironmentTier(const char*) { v8runtime::V8RuntimeArgs args{}; args.inspectorPort = 5643; - args.foreground_task_runner = std::make_shared(*m_workQueue); + args.foreground_task_runner = std::make_shared(*this); const auto runtime{v8runtime::makeV8Runtime(std::move(args))}; const auto env{Napi::Attach(*runtime)}; diff --git a/Core/AppRuntime/Source/AppRuntime_JavaScriptCore.cpp b/Core/AppRuntime/Source/AppRuntimeImpl_JavaScriptCore.cpp similarity index 83% rename from Core/AppRuntime/Source/AppRuntime_JavaScriptCore.cpp rename to Core/AppRuntime/Source/AppRuntimeImpl_JavaScriptCore.cpp index bcc224d29..48f42ca18 100644 --- a/Core/AppRuntime/Source/AppRuntime_JavaScriptCore.cpp +++ b/Core/AppRuntime/Source/AppRuntimeImpl_JavaScriptCore.cpp @@ -1,11 +1,11 @@ -#include "AppRuntime.h" +#include "AppRuntimeImpl.h" #include #include namespace Babylon { - void AppRuntime::RunEnvironmentTier(const char*) + void AppRuntimeImpl::RunEnvironmentTier(const char*) { auto globalContext = JSGlobalContextCreateInGroup(nullptr, nullptr); Napi::Env env = Napi::Attach(globalContext); diff --git a/Core/AppRuntime/Source/AppRuntimeImpl_Unix.cpp b/Core/AppRuntime/Source/AppRuntimeImpl_Unix.cpp new file mode 100644 index 000000000..fcc67b22f --- /dev/null +++ b/Core/AppRuntime/Source/AppRuntimeImpl_Unix.cpp @@ -0,0 +1,21 @@ +#include "AppRuntimeImpl.h" +#include +#include + +namespace Babylon +{ + void AppRuntimeImpl::RunPlatformTier() + { + RunEnvironmentTier(); + } + + void AppRuntimeImpl::DefaultUnhandledExceptionHandler(const std::exception& error) + { + std::cerr << "Uncaught Error: " << error.what() << std::endl; + } + + void AppRuntimeImpl::Execute(Dispatchable callback) + { + callback(); + } +} diff --git a/Core/AppRuntime/Source/AppRuntime_V8.cpp b/Core/AppRuntime/Source/AppRuntimeImpl_V8.cpp similarity index 96% rename from Core/AppRuntime/Source/AppRuntime_V8.cpp rename to Core/AppRuntime/Source/AppRuntimeImpl_V8.cpp index a08abdeff..445d220e9 100644 --- a/Core/AppRuntime/Source/AppRuntime_V8.cpp +++ b/Core/AppRuntime/Source/AppRuntimeImpl_V8.cpp @@ -1,4 +1,4 @@ -#include "AppRuntime.h" +#include "AppRuntimeImpl.h" #include @@ -64,7 +64,7 @@ namespace Babylon std::unique_ptr Module::s_module; } - void AppRuntime::RunEnvironmentTier(const char* executablePath) + void AppRuntimeImpl::RunEnvironmentTier(const char* executablePath) { // Create the isolate. Module::Initialize(executablePath); diff --git a/Core/AppRuntime/Source/AppRuntime_Win32.cpp b/Core/AppRuntime/Source/AppRuntimeImpl_Win32.cpp similarity index 73% rename from Core/AppRuntime/Source/AppRuntime_Win32.cpp rename to Core/AppRuntime/Source/AppRuntimeImpl_Win32.cpp index 5fcee5698..753de97d9 100644 --- a/Core/AppRuntime/Source/AppRuntime_Win32.cpp +++ b/Core/AppRuntime/Source/AppRuntimeImpl_Win32.cpp @@ -1,4 +1,4 @@ -#include "AppRuntime.h" +#include "AppRuntimeImpl.h" #include #include @@ -10,7 +10,7 @@ namespace Babylon { - void AppRuntime::RunPlatformTier() + void AppRuntimeImpl::RunPlatformTier() { winrt::check_hresult(Windows::Foundation::Initialize(RO_INIT_MULTITHREADED)); @@ -20,14 +20,14 @@ namespace Babylon RunEnvironmentTier(executablePath); } - void AppRuntime::DefaultUnhandledExceptionHandler(const std::exception& error) + void AppRuntimeImpl::DefaultUnhandledExceptionHandler(const std::exception& error) { std::stringstream ss{}; ss << "Uncaught Error: " << error.what() << std::endl; OutputDebugStringA(ss.str().data()); } - void AppRuntime::Execute(Dispatchable callback) + void AppRuntimeImpl::Execute(Dispatchable callback) { callback(); } diff --git a/Core/AppRuntime/Source/AppRuntime_WinRT.cpp b/Core/AppRuntime/Source/AppRuntimeImpl_WinRT.cpp similarity index 65% rename from Core/AppRuntime/Source/AppRuntime_WinRT.cpp rename to Core/AppRuntime/Source/AppRuntimeImpl_WinRT.cpp index b98a9fe28..266d85c9f 100644 --- a/Core/AppRuntime/Source/AppRuntime_WinRT.cpp +++ b/Core/AppRuntime/Source/AppRuntimeImpl_WinRT.cpp @@ -1,4 +1,4 @@ -#include "AppRuntime.h" +#include "AppRuntimeImpl.h" #include #include @@ -8,21 +8,21 @@ namespace Babylon { - void AppRuntime::RunPlatformTier() + void AppRuntimeImpl::RunPlatformTier() { winrt::check_hresult(Windows::Foundation::Initialize(RO_INIT_MULTITHREADED)); RunEnvironmentTier(); } - void AppRuntime::DefaultUnhandledExceptionHandler(const std::exception& error) + void AppRuntimeImpl::DefaultUnhandledExceptionHandler(const std::exception& error) { std::stringstream ss{}; ss << "Uncaught Error: " << error.what() << std::endl; OutputDebugStringA(ss.str().data()); } - void AppRuntime::Execute(Dispatchable callback) + void AppRuntimeImpl::Execute(Dispatchable callback) { callback(); } diff --git a/Core/AppRuntime/Source/AppRuntime_iOS.mm b/Core/AppRuntime/Source/AppRuntimeImpl_iOS.mm similarity index 54% rename from Core/AppRuntime/Source/AppRuntime_iOS.mm rename to Core/AppRuntime/Source/AppRuntimeImpl_iOS.mm index e1528cf8e..49f764b61 100644 --- a/Core/AppRuntime/Source/AppRuntime_iOS.mm +++ b/Core/AppRuntime/Source/AppRuntimeImpl_iOS.mm @@ -1,21 +1,21 @@ -#include "AppRuntime.h" +#include "AppRuntimeImpl.h" #include #import namespace Babylon { - void AppRuntime::RunPlatformTier() + void AppRuntimeImpl::RunPlatformTier() { RunEnvironmentTier(); } - void AppRuntime::DefaultUnhandledExceptionHandler(const std::exception& error) + void AppRuntimeImpl::DefaultUnhandledExceptionHandler(const std::exception& error) { NSLog(@"Uncaught Error: %s", error.what()); } - void AppRuntime::Execute(Dispatchable callback) + void AppRuntimeImpl::Execute(Dispatchable callback) { @autoreleasepool { diff --git a/Core/AppRuntime/Source/AppRuntime_macOS.mm b/Core/AppRuntime/Source/AppRuntimeImpl_macOS.mm similarity index 54% rename from Core/AppRuntime/Source/AppRuntime_macOS.mm rename to Core/AppRuntime/Source/AppRuntimeImpl_macOS.mm index e1528cf8e..49f764b61 100644 --- a/Core/AppRuntime/Source/AppRuntime_macOS.mm +++ b/Core/AppRuntime/Source/AppRuntimeImpl_macOS.mm @@ -1,21 +1,21 @@ -#include "AppRuntime.h" +#include "AppRuntimeImpl.h" #include #import namespace Babylon { - void AppRuntime::RunPlatformTier() + void AppRuntimeImpl::RunPlatformTier() { RunEnvironmentTier(); } - void AppRuntime::DefaultUnhandledExceptionHandler(const std::exception& error) + void AppRuntimeImpl::DefaultUnhandledExceptionHandler(const std::exception& error) { NSLog(@"Uncaught Error: %s", error.what()); } - void AppRuntime::Execute(Dispatchable callback) + void AppRuntimeImpl::Execute(Dispatchable callback) { @autoreleasepool { diff --git a/Core/AppRuntime/Source/AppRuntime_Unix.cpp b/Core/AppRuntime/Source/AppRuntime_Unix.cpp deleted file mode 100644 index 69585c398..000000000 --- a/Core/AppRuntime/Source/AppRuntime_Unix.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "WorkQueue.h" -#include "AppRuntime.h" -#include -#include - -namespace Babylon -{ - void AppRuntime::RunPlatformTier() - { - RunEnvironmentTier(); - } - - void AppRuntime::DefaultUnhandledExceptionHandler(const std::exception& error) - { - std::cerr << "Uncaught Error: " << error.what() << std::endl; - } - - void AppRuntime::Execute(Dispatchable callback) - { - callback(); - } -} diff --git a/Core/AppRuntime/Source/WorkQueue.cpp b/Core/AppRuntime/Source/WorkQueue.cpp deleted file mode 100644 index 9b3b2cc00..000000000 --- a/Core/AppRuntime/Source/WorkQueue.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "WorkQueue.h" - -namespace Babylon -{ - WorkQueue::WorkQueue(std::function threadProcedure) - : m_thread{std::move(threadProcedure)} - { - } - - WorkQueue::~WorkQueue() - { - if (m_suspensionLock.has_value()) - { - Resume(); - } - - // Dispatch a cancel to signal the Run function to gracefully end. - // It must be dispatched and not canceled directly to ensure that - // existing work is executed and executed in the correct order. - m_dispatcher([this]() { - m_cancellationSource.cancel(); - }); - - m_thread.join(); - } - - void WorkQueue::Suspend() - { - auto suspensionMutex = std::make_shared(); - m_suspensionLock.emplace(*suspensionMutex); - Append([suspensionMutex{std::move(suspensionMutex)}](Napi::Env) { - std::scoped_lock lock{*suspensionMutex}; - }); - } - - void WorkQueue::Resume() - { - m_suspensionLock.reset(); - } - - void WorkQueue::Run(Napi::Env env) - { - m_env = std::make_optional(env); - - m_dispatcher.set_affinity(std::this_thread::get_id()); - - while (!m_cancellationSource.cancelled()) - { - m_dispatcher.blocking_tick(m_cancellationSource); - } - - // The dispatcher can be non-empty if something is dispatched after cancellation. - // For example, Chakra's JsSetPromiseContinuationCallback may potentially dispatch - // a continuation after cancellation. - m_dispatcher.clear(); - - m_env.reset(); - } -} diff --git a/Core/AppRuntime/Source/WorkQueue.h b/Core/AppRuntime/Source/WorkQueue.h deleted file mode 100644 index a731c1ee9..000000000 --- a/Core/AppRuntime/Source/WorkQueue.h +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once - -#include -#include -#include - -#include -#include -#include - -namespace Babylon -{ - class WorkQueue - { - public: - WorkQueue(std::function threadProcedure); - ~WorkQueue(); - - template - void Append(CallableT callable) - { - // Manual dispatcher queueing requires a copyable CallableT, we use a shared pointer trick to make a - // copyable callable if necessary. - if constexpr (std::is_copy_constructible::value) - { - m_dispatcher.queue([this, callable = std::move(callable)]() { - callable(m_env.value()); - }); - } - else - { - m_dispatcher.queue([this, callablePtr = std::make_shared(std::move(callable))]() { - (*callablePtr)(m_env.value()); - }); - } - } - - void Suspend(); - void Resume(); - void Run(Napi::Env); - - private: - std::optional m_env{}; - std::optional> m_suspensionLock{}; - arcana::cancellation_source m_cancellationSource{}; - arcana::manual_dispatcher<128> m_dispatcher{}; - std::thread m_thread{}; - }; -}