clean up per realm resources/threads

This commit is contained in:
Ari Lazier 2015-10-19 14:25:35 -07:00
parent 167c3e97b5
commit 8fed61cb8a
5 changed files with 19 additions and 8 deletions

View File

@ -454,7 +454,7 @@ JSValueRef RealmClose(JSContextRef ctx, JSObjectRef function, JSObjectRef thisOb
try { try {
RJSValidateArgumentCount(argumentCount, 0); RJSValidateArgumentCount(argumentCount, 0);
SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject); SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject);
realm->invalidate(); realm->close();
realm::Realm::s_global_cache.remove(realm->config().path, realm->thread_id()); realm::Realm::s_global_cache.remove(realm->config().path, realm->thread_id());
} }

View File

@ -24,7 +24,6 @@
// add realm apis to the given js context // add realm apis to the given js context
+ (void)initializeContext:(JSContextRef)ctx; + (void)initializeContext:(JSContextRef)ctx;
+ (void)disposeContext:(JSContextRef)ctx;
+ (void)clearTestState; + (void)clearTestState;
@end @end

View File

@ -83,7 +83,7 @@ static JSValueRef ClearTestState(JSContextRef ctx, JSObjectRef function, JSObjec
} }
+ (void)clearTestState { + (void)clearTestState {
realm::Realm::s_global_cache.invalidate_all(); realm::Realm::s_global_cache.close_all();
realm::Realm::s_global_cache.clear(); realm::Realm::s_global_cache.clear();
NSFileManager *manager = [NSFileManager defaultManager]; NSFileManager *manager = [NSFileManager defaultManager];

View File

@ -295,6 +295,16 @@ void Realm::invalidate()
m_group = nullptr; m_group = nullptr;
} }
void Realm::close()
{
invalidate();
if (m_notifier) {
m_notifier->remove_realm(this);
m_notifier = nullptr;
}
m_delegate = nullptr;
}
bool Realm::compact() bool Realm::compact()
{ {
verify_thread(); verify_thread();
@ -440,14 +450,15 @@ void RealmCache::cache_realm(SharedRealm &realm, std::thread::id thread_id)
} }
} }
void RealmCache::invalidate_all() void RealmCache::close_all(std::thread::id thread_id)
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
for (auto &path_realms : m_cache) { for (auto &path_realms : m_cache) {
for (auto &realm_iter : path_realms.second) { auto thread_realm = path_realms.second.find(thread_id);
if (auto realm = realm_iter.second.lock()) { if (thread_realm != path_realms.second.end()) {
realm->invalidate(); if (auto realm = thread_realm->second.lock()) {
realm->close();
} }
} }
} }

View File

@ -94,6 +94,7 @@ namespace realm {
void invalidate(); void invalidate();
bool compact(); bool compact();
void close();
std::thread::id thread_id() const { return m_thread_id; } std::thread::id thread_id() const { return m_thread_id; }
void verify_thread() const; void verify_thread() const;
@ -131,7 +132,7 @@ namespace realm {
SharedRealm get_any_realm(const std::string &path); SharedRealm get_any_realm(const std::string &path);
void remove(const std::string &path, std::thread::id thread_id); void remove(const std::string &path, std::thread::id thread_id);
void cache_realm(SharedRealm &realm, std::thread::id thread_id = std::this_thread::get_id()); void cache_realm(SharedRealm &realm, std::thread::id thread_id = std::this_thread::get_id());
void invalidate_all(); void close_all(std::thread::id thread_id = std::this_thread::get_id());
void clear(); void clear();
private: private: