diff --git a/CHANGELOG.md b/CHANGELOG.md index 81045741c7d..bd982d256bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,9 @@ ### Internals * Update TestAppSession to allow scope-based usage for restarting the local app resources. ([PR #7672](https://github.com/realm/realm-core/pull/7672)) +* 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)) ---------------------------------------------- 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: 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)...)); 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