From 632d757014c785f4f791a04c566f4b9fa0d2e5aa Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Mon, 25 Apr 2016 12:02:59 -0700 Subject: [PATCH] Always deliver results to the correct SharedGroup If there are multiple Realm instances for a single file on a single thread due to disabling caching we need to actually deliver the results to the appropriate SharedGroup for each notifier rather than delivering them all to the first one. --- src/impl/collection_notifier.cpp | 9 ++++++--- src/impl/collection_notifier.hpp | 6 +----- src/impl/realm_coordinator.cpp | 4 ++-- tests/list.cpp | 6 ++++-- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/impl/collection_notifier.cpp b/src/impl/collection_notifier.cpp index eb92e6fa..249775da 100644 --- a/src/impl/collection_notifier.cpp +++ b/src/impl/collection_notifier.cpp @@ -180,10 +180,13 @@ void CollectionNotifier::prepare_handover() do_prepare_handover(*m_sg); } -bool CollectionNotifier::deliver(SharedGroup& sg, std::exception_ptr err) +bool CollectionNotifier::deliver(Realm& realm, SharedGroup& sg, std::exception_ptr err) { - if (!is_for_current_thread()) { - return false; + { + std::lock_guard lock(m_realm_mutex); + if (m_realm.get() != &realm) { + return false; + } } if (err) { diff --git a/src/impl/collection_notifier.hpp b/src/impl/collection_notifier.hpp index 1d98efc8..383984d4 100644 --- a/src/impl/collection_notifier.hpp +++ b/src/impl/collection_notifier.hpp @@ -27,7 +27,6 @@ #include #include #include -#include #include namespace realm { @@ -109,7 +108,7 @@ public: virtual void run() = 0; void prepare_handover(); - bool deliver(SharedGroup&, std::exception_ptr); + bool deliver(Realm&, SharedGroup&, std::exception_ptr); protected: bool have_callbacks() const noexcept { return m_have_callbacks; } @@ -124,9 +123,6 @@ private: virtual bool do_deliver(SharedGroup&) { return true; } virtual bool do_add_required_change_info(TransactionChangeInfo&) = 0; - const std::thread::id m_thread_id = std::this_thread::get_id(); - bool is_for_current_thread() const { return m_thread_id == std::this_thread::get_id(); } - mutable std::mutex m_realm_mutex; std::shared_ptr m_realm; diff --git a/src/impl/realm_coordinator.cpp b/src/impl/realm_coordinator.cpp index edb0df09..09693088 100644 --- a/src/impl/realm_coordinator.cpp +++ b/src/impl/realm_coordinator.cpp @@ -567,7 +567,7 @@ void RealmCoordinator::advance_to_ready(Realm& realm) // Query version now matches the SG version, so we can deliver them for (auto& notifier : m_notifiers) { - if (notifier->deliver(sg, m_async_error)) { + if (notifier->deliver(realm, sg, m_async_error)) { notifiers.push_back(notifier); } } @@ -586,7 +586,7 @@ void RealmCoordinator::process_available_async(Realm& realm) { std::lock_guard lock(m_notifier_mutex); for (auto& notifier : m_notifiers) { - if (notifier->deliver(sg, m_async_error)) { + if (notifier->deliver(realm, sg, m_async_error)) { notifiers.push_back(notifier); } } diff --git a/tests/list.cpp b/tests/list.cpp index 79153e7a..4d079b03 100644 --- a/tests/list.cpp +++ b/tests/list.cpp @@ -211,7 +211,8 @@ TEST_CASE("list") { // Each of the Lists now has a different source version and state at // that version, so they should all see different changes despite // being for the same LinkList - advance_and_notify(*r); + for (auto& list : lists) + advance_and_notify(*list.get_realm()); REQUIRE_INDICES(changes[0].insertions, 0, 1, 2); REQUIRE(changes[0].modifications.empty()); @@ -224,7 +225,8 @@ TEST_CASE("list") { // After making another change, they should all get the same notification change_list(); - advance_and_notify(*r); + for (auto& list : lists) + advance_and_notify(*list.get_realm()); for (int i = 0; i < 3; ++i) { REQUIRE_INDICES(changes[i].insertions, 3);