From 5c88f51c19cd00b4b5ba41e8c2f6c8d43ed62edc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Thu, 18 Jul 2024 21:06:04 +0200 Subject: [PATCH 1/4] Adding scheduler as second optional argument to EventLoopDispatcher constructor --- .../object-store/util/event_loop_dispatcher.hpp | 15 +++++++++++---- test/object-store/util/event_loop.cpp | 5 +++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/realm/object-store/util/event_loop_dispatcher.hpp b/src/realm/object-store/util/event_loop_dispatcher.hpp index a194a8ea6f5..6a2312ea900 100644 --- a/src/realm/object-store/util/event_loop_dispatcher.hpp +++ b/src/realm/object-store/util/event_loop_dispatcher.hpp @@ -19,6 +19,7 @@ #ifndef REALM_OS_UTIL_EVENT_LOOP_DISPATCHER_HPP #define REALM_OS_UTIL_EVENT_LOOP_DISPATCHER_HPP +#include #include #include @@ -35,11 +36,13 @@ class EventLoopDispatcher { private: const std::shared_ptr> m_func; - const std::shared_ptr m_scheduler = util::Scheduler::make_default(); + const std::shared_ptr m_scheduler; public: - EventLoopDispatcher(util::UniqueFunction func) + EventLoopDispatcher(util::UniqueFunction func, + std::shared_ptr scheduler = util::Scheduler::make_default()) : m_func(std::make_shared>(std::move(func))) + , m_scheduler(scheduler) { } @@ -93,11 +96,11 @@ struct ExtractSignatureImpl { using signature = void(Args...); }; template -struct ExtractSignatureImpl { +struct ExtractSignatureImpl { using signature = void(Args...); }; template -struct ExtractSignatureImpl { +struct ExtractSignatureImpl { using signature = void(Args...); }; // Note: no && specializations since util::UniqueFunction doesn't support them, so you can't construct an @@ -112,11 +115,15 @@ namespace util { // Deduction guide for function pointers. template EventLoopDispatcher(void (*)(Args...)) -> EventLoopDispatcher; +template +EventLoopDispatcher(void (*)(Args...), std::shared_ptr) -> EventLoopDispatcher; // Deduction guide for callable objects, such as lambdas. Only supports types with a non-overloaded, non-templated // call operator, so no polymorphic (auto argument) lambdas. template > EventLoopDispatcher(const T&) -> EventLoopDispatcher; +template > +EventLoopDispatcher(const T&, std::shared_ptr) -> EventLoopDispatcher; } // namespace util diff --git a/test/object-store/util/event_loop.cpp b/test/object-store/util/event_loop.cpp index 0f9914dcd27..e94c15c433b 100644 --- a/test/object-store/util/event_loop.cpp +++ b/test/object-store/util/event_loop.cpp @@ -18,6 +18,7 @@ #include "util/event_loop.hpp" +#include #include #include #include @@ -70,6 +71,10 @@ void static_assert_EventLoopDispatcher_guide(const EventLoopDispatcher&) void operator()(int) const& noexcept {} }; static_assert_EventLoopDispatcher_guide(EventLoopDispatcher(Funcy())); + + // Passing a scheduler as second argument + auto scheduler = Scheduler::make_dummy(); + static_assert_EventLoopDispatcher_guide(EventLoopDispatcher([] {}, scheduler)); } } // namespace From c32ae3f0c30b6eb8e2fb1aa395a3660757fc575d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Thu, 18 Jul 2024 21:06:50 +0200 Subject: [PATCH 2/4] Adding scheduler as second optional argument to schedulerWrapBlockingFunction --- bindgen/src/realm_helpers.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bindgen/src/realm_helpers.h b/bindgen/src/realm_helpers.h index e302e0091a8..b0242872ea7 100644 --- a/bindgen/src/realm_helpers.h +++ b/bindgen/src/realm_helpers.h @@ -352,12 +352,12 @@ class ThreadConfinementChecker { }; template -auto schedulerWrapBlockingFunction(F&& f) +auto schedulerWrapBlockingFunction(F&& f, + std::shared_ptr scheduler = util::Scheduler::make_default()) { - return [f = FWD(f), sched = util::Scheduler::make_default()]( - auto&&... args) -> std::decay_t> { + return [f = FWD(f), scheduler](auto&&... args) -> std::decay_t> { using Ret = std::decay_t>; - if (sched->is_on_thread()) + if (scheduler->is_on_thread()) return f(FWD(args)...); // TODO in C++20, can use std::atomic::wait() and notify() rather than mutex and condvar. @@ -366,7 +366,7 @@ auto schedulerWrapBlockingFunction(F&& f) std::optional ret; std::exception_ptr ex; - sched->invoke([&]() noexcept { + scheduler->invoke([&]() noexcept { auto lk = std::lock_guard(mx); try { ret.emplace(f(FWD(args)...)); From 7855eaef6f82ea5e4fd68e7294b77b3844209f4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Thu, 18 Jul 2024 21:07:14 +0200 Subject: [PATCH 3/4] Expose passing scheduler via RealmConfig --- bindgen/spec.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bindgen/spec.yml b/bindgen/spec.yml index 91fac6394fa..e79a7499643 100644 --- a/bindgen/spec.yml +++ b/bindgen/spec.yml @@ -443,6 +443,9 @@ records: disable_format_upgrade: type: bool default: false + scheduler: + type: SharedScheduler + default: {} sync_config: Nullable> # Used internally as a flag for opening a synced realm locally. force_sync_history: From 0f0e1ed63863d920410afb5d9655c49c497a7e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Fri, 19 Jul 2024 15:00:08 +0200 Subject: [PATCH 4/4] Adding a note to the changelog --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7c32b9cbfd..64feb8f8796 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,9 @@ ----------- ### Internals -* None. +* Added second optional `scheduler` parameter to `EventLoopDispatcher`. ([PR #7903](https://github.com/realm/realm-core/pull/7903)) +* (bindgen) Added second optional `scheduler` parameter to `schedulerWrapBlockingFunction`. ([PR #7903](https://github.com/realm/realm-core/pull/7903)) +* (bindgen) Exposing optional `scheduler` property on `RealmConfig`. ([PR #7903](https://github.com/realm/realm-core/pull/7903)) ----------------------------------------------