|
| 1 | +#include "AppRuntimeImpl.h" |
| 2 | +#include <Babylon/JsRuntime.h> |
| 3 | + |
| 4 | +namespace Babylon |
| 5 | +{ |
| 6 | + AppRuntimeImpl::AppRuntimeImpl(std::function<void(const std::exception&)> unhandledExceptionHandler) |
| 7 | + : m_unhandledExceptionHandler{std::move(unhandledExceptionHandler)} |
| 8 | + , m_thread{[this] { RunPlatformTier(); }} |
| 9 | + { |
| 10 | + Dispatch([this](Napi::Env env) { |
| 11 | + JsRuntime::CreateForJavaScript(env, [this](auto func) { Dispatch(std::move(func)); }); |
| 12 | + }); |
| 13 | + } |
| 14 | + |
| 15 | + AppRuntimeImpl::~AppRuntimeImpl() |
| 16 | + { |
| 17 | + if (m_suspensionLock.has_value()) |
| 18 | + { |
| 19 | + m_suspensionLock.reset(); |
| 20 | + } |
| 21 | + |
| 22 | + Dispatch([this](Napi::Env env) { |
| 23 | + // Notify the JsRuntime on the JavaScript thread that the JavaScript runtime shutdown sequence has |
| 24 | + // begun. The JsRuntimeScheduler will use this signal to gracefully cancel asynchronous operations. |
| 25 | + JsRuntime::NotifyDisposing(JsRuntime::GetFromJavaScript(env)); |
| 26 | + |
| 27 | + // Cancel on the JavaScript thread to signal the Run function to gracefully end. It must be |
| 28 | + // dispatched and not canceled directly to ensure that existing work is executed and executed in |
| 29 | + // the correct order. |
| 30 | + m_cancellationSource.cancel(); |
| 31 | + }); |
| 32 | + |
| 33 | + m_thread.join(); |
| 34 | + } |
| 35 | + |
| 36 | + void AppRuntimeImpl::Suspend() |
| 37 | + { |
| 38 | + auto suspensionMutex = std::make_shared<std::mutex>(); |
| 39 | + m_suspensionLock.emplace(*suspensionMutex); |
| 40 | + Append([suspensionMutex = std::move(suspensionMutex)](Napi::Env) { |
| 41 | + std::scoped_lock lock{*suspensionMutex}; |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + void AppRuntimeImpl::Resume() |
| 46 | + { |
| 47 | + m_suspensionLock.reset(); |
| 48 | + } |
| 49 | + |
| 50 | + void AppRuntimeImpl::Dispatch(Dispatchable<void(Napi::Env)> func) |
| 51 | + { |
| 52 | + Append([this, func{std::move(func)}](Napi::Env env) mutable { |
| 53 | + Execute([this, env, func{std::move(func)}]() mutable { |
| 54 | + try |
| 55 | + { |
| 56 | + func(env); |
| 57 | + } |
| 58 | + catch (const std::exception& error) |
| 59 | + { |
| 60 | + m_unhandledExceptionHandler(error); |
| 61 | + } |
| 62 | + catch (...) |
| 63 | + { |
| 64 | + std::abort(); |
| 65 | + } |
| 66 | + }); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + void AppRuntimeImpl::Run(Napi::Env env) |
| 71 | + { |
| 72 | + m_env = std::make_optional(env); |
| 73 | + |
| 74 | + m_dispatcher.set_affinity(std::this_thread::get_id()); |
| 75 | + |
| 76 | + while (!m_cancellationSource.cancelled()) |
| 77 | + { |
| 78 | + m_dispatcher.blocking_tick(m_cancellationSource); |
| 79 | + } |
| 80 | + |
| 81 | + // The dispatcher can be non-empty if something is dispatched after cancellation. |
| 82 | + // For example, Chakra's JsSetPromiseContinuationCallback may potentially dispatch |
| 83 | + // a continuation after cancellation. |
| 84 | + m_dispatcher.clear(); |
| 85 | + |
| 86 | + m_env.reset(); |
| 87 | + } |
| 88 | +} |
0 commit comments