From 521fd129373bb9f0585f38246df267b5296c8c0c Mon Sep 17 00:00:00 2001 From: blagoev Date: Mon, 13 Nov 2017 09:26:13 +0200 Subject: [PATCH] fix iOS debugger --- src/rpc.cpp | 42 +++++++++++++++++++++++++++++++----------- src/rpc.hpp | 8 +++++--- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/rpc.cpp b/src/rpc.cpp index 33f2105d..b3b2ef42 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -28,7 +28,6 @@ #include "object_accessor.hpp" #include "shared_realm.hpp" #include "results.hpp" -#include using namespace realm; using namespace realm::rpc; @@ -83,15 +82,30 @@ RPCServer*& get_rpc_server(JSGlobalContextRef ctx) { } } -RPCWorker::RPCWorker() { - //m_thread = std::thread([this]() { - //m_looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); +#ifdef REALM_PLATFORM_APPLE +void runLoopFunc(CFRunLoopRef loop, RPCWorker* rpcWorker) { + auto m_stop = false; + CFRunLoopPerformBlock(loop, kCFRunLoopDefaultMode, + ^{ + rpcWorker->try_run_task(); + if (rpcWorker->should_stop()) { + CFRunLoopStop(CFRunLoopGetCurrent()); + } else { + runLoopFunc(loop, rpcWorker); + } + }); + CFRunLoopWakeUp(loop); +} +#endif - // TODO: Create ALooper/CFRunLoop to support async calls. - //while (!m_stop) { - //try_run_task(); - //} - //}); +RPCWorker::RPCWorker() { + #ifdef REALM_PLATFORM_APPLE + m_thread = std::thread([this]() { + m_loop = CFRunLoopGetCurrent(); + runLoopFunc(m_loop, this); + CFRunLoopRun(); + }); + #endif } RPCWorker::~RPCWorker() { @@ -139,11 +153,17 @@ bool RPCWorker::try_run_task() { return m_stop; } +bool RPCWorker::should_stop() { + return m_stop; +} + void RPCWorker::stop() { if (!m_stop) { m_stop = true; - //m_thread.join(); - //m_looper = nullptr; +#if REALM_PLATFORM_APPLE + m_thread.join(); + m_loop = nullptr; +#endif } } diff --git a/src/rpc.hpp b/src/rpc.hpp index 0f1c85be..4fd434de 100644 --- a/src/rpc.hpp +++ b/src/rpc.hpp @@ -26,7 +26,6 @@ #include "json.hpp" #include "jsc_types.hpp" #include "jsc_protected.hpp" -#include namespace realm { @@ -49,11 +48,14 @@ class RPCWorker { bool try_run_task(); void stop(); json try_pop_task_result(); + bool should_stop(); private: bool m_stop = false; - //ALooper* m_looper; - //std::thread m_thread; +#if REALM_PLATFORM_APPLE + std::thread m_thread; + CFRunLoopRef m_loop; +#endif ConcurrentDeque> m_tasks; ConcurrentDeque> m_futures; };