Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing scheduler to EventLoopDispatcher, schedulerWrapBlockingFunction and through RealmConfig #7903

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

----------------------------------------------

Expand Down
3 changes: 3 additions & 0 deletions bindgen/spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ records:
disable_format_upgrade:
type: bool
default: false
scheduler:
type: SharedScheduler
default: {}
sync_config: Nullable<std::shared_ptr<SyncConfig>>
# Used internally as a flag for opening a synced realm locally.
force_sync_history:
Expand Down
10 changes: 5 additions & 5 deletions bindgen/src/realm_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,12 @@ class ThreadConfinementChecker {
};

template <typename F>
auto schedulerWrapBlockingFunction(F&& f)
auto schedulerWrapBlockingFunction(F&& f,
std::shared_ptr<util::Scheduler> scheduler = util::Scheduler::make_default())
{
return [f = FWD(f), sched = util::Scheduler::make_default()](
auto&&... args) -> std::decay_t<std::invoke_result_t<F, decltype(args)...>> {
return [f = FWD(f), scheduler](auto&&... args) -> std::decay_t<std::invoke_result_t<F, decltype(args)...>> {
using Ret = std::decay_t<std::invoke_result_t<F, decltype(args)...>>;
if (sched->is_on_thread())
if (scheduler->is_on_thread())
return f(FWD(args)...);

// TODO in C++20, can use std::atomic<bool>::wait() and notify() rather than mutex and condvar.
Expand All @@ -366,7 +366,7 @@ auto schedulerWrapBlockingFunction(F&& f)
std::optional<Ret> ret;
std::exception_ptr ex;

sched->invoke([&]() noexcept {
scheduler->invoke([&]() noexcept {
auto lk = std::lock_guard(mx);
try {
ret.emplace(f(FWD(args)...));
Expand Down
15 changes: 11 additions & 4 deletions src/realm/object-store/util/event_loop_dispatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef REALM_OS_UTIL_EVENT_LOOP_DISPATCHER_HPP
#define REALM_OS_UTIL_EVENT_LOOP_DISPATCHER_HPP

#include <memory>
#include <realm/object-store/util/scheduler.hpp>

#include <tuple>
Expand All @@ -35,11 +36,13 @@ class EventLoopDispatcher<void(Args...)> {

private:
const std::shared_ptr<util::UniqueFunction<void(Args...)>> m_func;
const std::shared_ptr<util::Scheduler> m_scheduler = util::Scheduler::make_default();
const std::shared_ptr<util::Scheduler> m_scheduler;

public:
EventLoopDispatcher(util::UniqueFunction<void(Args...)> func)
EventLoopDispatcher(util::UniqueFunction<void(Args...)> func,
std::shared_ptr<util::Scheduler> scheduler = util::Scheduler::make_default())
: m_func(std::make_shared<util::UniqueFunction<void(Args...)>>(std::move(func)))
, m_scheduler(scheduler)
{
}

Expand Down Expand Up @@ -93,11 +96,11 @@ struct ExtractSignatureImpl<void (T::*)(Args...) const noexcept> {
using signature = void(Args...);
};
template <typename T, typename... Args>
struct ExtractSignatureImpl<void (T::*)(Args...)& noexcept> {
struct ExtractSignatureImpl<void (T::*)(Args...) & noexcept> {
using signature = void(Args...);
};
template <typename T, typename... Args>
struct ExtractSignatureImpl<void (T::*)(Args...) const& noexcept> {
struct ExtractSignatureImpl<void (T::*)(Args...) const & noexcept> {
using signature = void(Args...);
};
// Note: no && specializations since util::UniqueFunction doesn't support them, so you can't construct an
Expand All @@ -112,11 +115,15 @@ namespace util {
// Deduction guide for function pointers.
template <typename... Args>
EventLoopDispatcher(void (*)(Args...)) -> EventLoopDispatcher<void(Args...)>;
template <typename... Args>
EventLoopDispatcher(void (*)(Args...), std::shared_ptr<Scheduler>) -> EventLoopDispatcher<void(Args...)>;

// 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 <typename T, typename Sig = _impl::ForEventLoopDispatcher::ExtractSignature<decltype(&T::operator())>>
EventLoopDispatcher(const T&) -> EventLoopDispatcher<Sig>;
template <typename T, typename Sig = _impl::ForEventLoopDispatcher::ExtractSignature<decltype(&T::operator())>>
EventLoopDispatcher(const T&, std::shared_ptr<Scheduler>) -> EventLoopDispatcher<Sig>;


} // namespace util
Expand Down
5 changes: 5 additions & 0 deletions test/object-store/util/event_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "util/event_loop.hpp"

#include <realm/object-store/util/scheduler.hpp>
#include <realm/object-store/util/event_loop_dispatcher.hpp>
#include <realm/util/scope_exit.hpp>
#include <realm/util/features.h>
Expand Down Expand Up @@ -70,6 +71,10 @@ void static_assert_EventLoopDispatcher_guide(const EventLoopDispatcher<Actual>&)
void operator()(int) const& noexcept {}
};
static_assert_EventLoopDispatcher_guide<void(int)>(EventLoopDispatcher(Funcy()));

// Passing a scheduler as second argument
auto scheduler = Scheduler::make_dummy();
static_assert_EventLoopDispatcher_guide<void()>(EventLoopDispatcher([] {}, scheduler));
}
} // namespace

Expand Down
Loading