Rename CachedRealm to WeakRealmNotfier

This commit is contained in:
Thomas Goyne 2016-02-23 14:31:59 -08:00
parent 143cc3b696
commit cfc88b6fd5
8 changed files with 55 additions and 55 deletions

View File

@ -20,8 +20,8 @@ set(HEADERS
results.hpp
schema.hpp
shared_realm.hpp
impl/cached_realm.hpp
impl/cached_realm_base.hpp
impl/weak_realm_notifier.hpp
impl/weak_realm_notifier_base.hpp
impl/external_commit_helper.hpp
impl/transact_log_handler.hpp
parser/parser.hpp
@ -30,17 +30,17 @@ set(HEADERS
if(APPLE)
list(APPEND SOURCES
impl/apple/cached_realm.cpp
impl/apple/weak_realm_notifier.cpp
impl/apple/external_commit_helper.cpp)
list(APPEND HEADERS
impl/apple/cached_realm.hpp
impl/apple/weak_realm_notifier.hpp
impl/apple/external_commit_helper.hpp)
find_library(CF_LIBRARY CoreFoundation)
else()
list(APPEND SOURCES
impl/generic/external_commit_helper.cpp)
list(APPEND HEADERS
impl/generic/cached_realm.hpp
impl/generic/weak_realm_notifier.hpp
impl/generic/external_commit_helper.hpp)
endif()

View File

@ -16,7 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////
#include "impl/cached_realm.hpp"
#include "impl/weak_realm_notifier.hpp"
#include "shared_realm.hpp"
@ -25,8 +25,8 @@
using namespace realm;
using namespace realm::_impl;
CachedRealm::CachedRealm(const std::shared_ptr<Realm>& realm, bool cache)
: CachedRealmBase(realm, cache)
WeakRealmNotifier::WeakRealmNotifier(const std::shared_ptr<Realm>& realm, bool cache)
: WeakRealmNotifierBase(realm, cache)
{
struct RefCountedWeakPointer {
std::weak_ptr<Realm> realm;
@ -57,8 +57,8 @@ CachedRealm::CachedRealm(const std::shared_ptr<Realm>& realm, bool cache)
CFRunLoopAddSource(m_runloop, m_signal, kCFRunLoopDefaultMode);
}
CachedRealm::CachedRealm(CachedRealm&& rgt)
: CachedRealmBase(std::move(rgt))
WeakRealmNotifier::WeakRealmNotifier(WeakRealmNotifier&& rgt)
: WeakRealmNotifierBase(std::move(rgt))
, m_runloop(rgt.m_runloop)
, m_signal(rgt.m_signal)
{
@ -66,9 +66,9 @@ CachedRealm::CachedRealm(CachedRealm&& rgt)
rgt.m_signal = nullptr;
}
CachedRealm& CachedRealm::operator=(CachedRealm&& rgt)
WeakRealmNotifier& WeakRealmNotifier::operator=(WeakRealmNotifier&& rgt)
{
CachedRealmBase::operator=(std::move(rgt));
WeakRealmNotifierBase::operator=(std::move(rgt));
m_runloop = rgt.m_runloop;
m_signal = rgt.m_signal;
rgt.m_runloop = nullptr;
@ -77,7 +77,7 @@ CachedRealm& CachedRealm::operator=(CachedRealm&& rgt)
return *this;
}
CachedRealm::~CachedRealm()
WeakRealmNotifier::~WeakRealmNotifier()
{
if (m_signal) {
CFRunLoopSourceInvalidate(m_signal);
@ -86,7 +86,7 @@ CachedRealm::~CachedRealm()
}
}
void CachedRealm::notify()
void WeakRealmNotifier::notify()
{
CFRunLoopSourceSignal(m_signal);
// Signalling the source makes it run the next time the runloop gets

View File

@ -16,7 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////
#include "impl/cached_realm_base.hpp"
#include "impl/weak_realm_notifier_base.hpp"
#include <CoreFoundation/CFRunLoop.h>
@ -25,16 +25,16 @@ class Realm;
namespace _impl {
class CachedRealm : public CachedRealmBase {
class WeakRealmNotifier : public WeakRealmNotifierBase {
public:
CachedRealm(const std::shared_ptr<Realm>& realm, bool cache);
~CachedRealm();
WeakRealmNotifier(const std::shared_ptr<Realm>& realm, bool cache);
~WeakRealmNotifier();
CachedRealm(CachedRealm&&);
CachedRealm& operator=(CachedRealm&&);
WeakRealmNotifier(WeakRealmNotifier&&);
WeakRealmNotifier& operator=(WeakRealmNotifier&&);
CachedRealm(const CachedRealm&) = delete;
CachedRealm& operator=(const CachedRealm&) = delete;
WeakRealmNotifier(const WeakRealmNotifier&) = delete;
WeakRealmNotifier& operator=(const WeakRealmNotifier&) = delete;
// Asynchronously call notify() on the Realm on the appropriate thread
void notify();

View File

@ -16,16 +16,16 @@
//
////////////////////////////////////////////////////////////////////////////
#include "impl/cached_realm_base.hpp"
#include "impl/weak_realm_notifier_base.hpp"
namespace realm {
class Realm;
namespace _impl {
class CachedRealm : public CachedRealmBase {
class WeakRealmNotifier : public WeakRealmNotifierBase {
public:
using CachedRealmBase::CachedRealmBase;
using WeakRealmNotifierBase::WeakRealmNotifierBase;
// Do nothing, as this can't be implemented portably
void notify() { }

View File

@ -19,7 +19,7 @@
#include "impl/realm_coordinator.hpp"
#include "impl/async_query.hpp"
#include "impl/cached_realm.hpp"
#include "impl/weak_realm_notifier.hpp"
#include "impl/external_commit_helper.hpp"
#include "impl/transact_log_handler.hpp"
#include "object_store.hpp"
@ -64,7 +64,7 @@ std::shared_ptr<RealmCoordinator> RealmCoordinator::get_existing_coordinator(Str
std::shared_ptr<Realm> RealmCoordinator::get_realm(Realm::Config config)
{
std::lock_guard<std::mutex> lock(m_realm_mutex);
if ((!m_config.read_only && !m_notifier) || (m_config.read_only && m_cached_realms.empty())) {
if ((!m_config.read_only && !m_notifier) || (m_config.read_only && m_weak_realm_notifiers.empty())) {
m_config = config;
if (!config.read_only && !m_notifier) {
try {
@ -100,7 +100,7 @@ std::shared_ptr<Realm> RealmCoordinator::get_realm(Realm::Config config)
}
if (config.cache) {
for (auto& cachedRealm : m_cached_realms) {
for (auto& cachedRealm : m_weak_realm_notifiers) {
if (cachedRealm.is_cached_for_current_thread()) {
// can be null if we jumped in between ref count hitting zero and
// unregister_realm() getting the lock
@ -113,7 +113,7 @@ std::shared_ptr<Realm> RealmCoordinator::get_realm(Realm::Config config)
auto realm = std::make_shared<Realm>(std::move(config));
realm->init(shared_from_this());
m_cached_realms.emplace_back(realm, m_config.cache);
m_weak_realm_notifiers.emplace_back(realm, m_config.cache);
return realm;
}
@ -124,7 +124,7 @@ std::shared_ptr<Realm> RealmCoordinator::get_realm()
const Schema* RealmCoordinator::get_schema() const noexcept
{
return m_cached_realms.empty() ? nullptr : m_config.schema.get();
return m_weak_realm_notifiers.empty() ? nullptr : m_config.schema.get();
}
void RealmCoordinator::update_schema(Schema const& schema)
@ -152,16 +152,16 @@ RealmCoordinator::~RealmCoordinator()
void RealmCoordinator::unregister_realm(Realm* realm)
{
std::lock_guard<std::mutex> lock(m_realm_mutex);
for (size_t i = 0; i < m_cached_realms.size(); ++i) {
auto& cached_realm = m_cached_realms[i];
if (!cached_realm.expired() && !cached_realm.is_for_realm(realm)) {
for (size_t i = 0; i < m_weak_realm_notifiers.size(); ++i) {
auto& weak_realm_notifier = m_weak_realm_notifiers[i];
if (!weak_realm_notifier.expired() && !weak_realm_notifier.is_for_realm(realm)) {
continue;
}
if (i + 1 < m_cached_realms.size()) {
cached_realm = std::move(m_cached_realms.back());
if (i + 1 < m_weak_realm_notifiers.size()) {
weak_realm_notifier = std::move(m_weak_realm_notifiers.back());
}
m_cached_realms.pop_back();
m_weak_realm_notifiers.pop_back();
}
}
@ -180,8 +180,8 @@ void RealmCoordinator::clear_cache()
coordinator->m_notifier = nullptr;
// Gather a list of all of the realms which will be removed
for (auto& cached_realm : coordinator->m_cached_realms) {
if (auto realm = cached_realm.realm()) {
for (auto& weak_realm_notifier : coordinator->m_weak_realm_notifiers) {
if (auto realm = weak_realm_notifier.realm()) {
realms_to_close.push_back(realm);
}
}
@ -289,7 +289,7 @@ void RealmCoordinator::on_change()
run_async_queries();
std::lock_guard<std::mutex> lock(m_realm_mutex);
for (auto& realm : m_cached_realms) {
for (auto& realm : m_weak_realm_notifiers) {
realm.notify();
}
}

View File

@ -33,7 +33,7 @@ struct AsyncQueryCancelationToken;
namespace _impl {
class AsyncQuery;
class CachedRealm;
class WeakRealmNotifier;
class ExternalCommitHelper;
// RealmCoordinator manages the weak cache of Realm instances and communication
@ -91,7 +91,7 @@ private:
Realm::Config m_config;
std::mutex m_realm_mutex;
std::vector<CachedRealm> m_cached_realms;
std::vector<WeakRealmNotifier> m_weak_realm_notifiers;
std::mutex m_query_mutex;
std::vector<std::shared_ptr<_impl::AsyncQuery>> m_new_queries;

View File

@ -16,15 +16,15 @@
//
////////////////////////////////////////////////////////////////////////////
#ifndef REALM_CACHED_REALM_HPP
#define REALM_CACHED_REALM_HPP
#ifndef REALM_WEAK_REALM_NOTIFIER_HPP
#define REALM_WEAK_REALM_NOTIFIER_HPP
#include <realm/util/features.h>
#if REALM_PLATFORM_APPLE
#include "impl/apple/cached_realm.hpp"
#include "impl/apple/weak_realm_notifier.hpp"
#else
#include "impl/generic/cached_realm.hpp"
#include "impl/generic/weak_realm_notifier.hpp"
#endif
#endif // REALM_CACHED_REALM_HPP
#endif // REALM_WEAK_REALM_NOTIFIER_HPP

View File

@ -16,8 +16,8 @@
//
////////////////////////////////////////////////////////////////////////////
#ifndef REALM_CACHED_REALM_BASE_HPP
#define REALM_CACHED_REALM_BASE_HPP
#ifndef REALM_WEAK_REALM_NOTIFIER_BASE_HPP
#define REALM_WEAK_REALM_NOTIFIER_BASE_HPP
#include <memory>
#include <thread>
@ -27,25 +27,25 @@ class Realm;
namespace _impl {
// CachedRealmBase stores a weak reference to a Realm instance, along with all of
// WeakRealmNotifierBase stores a weak reference to a Realm instance, along with all of
// the information about a Realm that needs to be accessed from other threads.
// This is needed to avoid forming strong references to the Realm instances on
// other threads, which can produce deadlocks when the last strong reference to
// a Realm instance is released from within a function holding the cache lock.
class CachedRealmBase {
class WeakRealmNotifierBase {
public:
CachedRealmBase(const std::shared_ptr<Realm>& realm, bool cache);
WeakRealmNotifierBase(const std::shared_ptr<Realm>& realm, bool cache);
// Get a strong reference to the cached realm
std::shared_ptr<Realm> realm() const { return m_realm.lock(); }
// Does this CachedRealmBase store a Realm instance that should be used on the current thread?
// Does this WeakRealmNotifierBase store a Realm instance that should be used on the current thread?
bool is_cached_for_current_thread() const { return m_cache && m_thread_id == std::this_thread::get_id(); }
// Has the Realm instance been destroyed?
bool expired() const { return m_realm.expired(); }
// Is this a CachedRealmBase for the given Realm instance?
// Is this a WeakRealmNotifierBase for the given Realm instance?
bool is_for_realm(Realm* realm) const { return realm == m_realm_key; }
private:
@ -55,7 +55,7 @@ private:
bool m_cache = false;
};
inline CachedRealmBase::CachedRealmBase(const std::shared_ptr<Realm>& realm, bool cache)
inline WeakRealmNotifierBase::WeakRealmNotifierBase(const std::shared_ptr<Realm>& realm, bool cache)
: m_realm(realm)
, m_realm_key(realm.get())
, m_cache(cache)
@ -65,4 +65,4 @@ inline CachedRealmBase::CachedRealmBase(const std::shared_ptr<Realm>& realm, boo
} // namespace _impl
} // namespace realm
#endif // REALM_CACHED_REALM_BASE_HPP
#endif // REALM_WEAK_REALM_NOTIFIER_BASE_HPP