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 {
RJSValidateArgumentCount(argumentCount, 0);
SharedRealm realm = *RJSGetInternal<SharedRealm *>(thisObject);
realm->invalidate();
realm->close();
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
+ (void)initializeContext:(JSContextRef)ctx;
+ (void)disposeContext:(JSContextRef)ctx;
+ (void)clearTestState;
@end

View File

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

View File

@ -295,6 +295,16 @@ void Realm::invalidate()
m_group = nullptr;
}
void Realm::close()
{
invalidate();
if (m_notifier) {
m_notifier->remove_realm(this);
m_notifier = nullptr;
}
m_delegate = nullptr;
}
bool Realm::compact()
{
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);
for (auto &path_realms : m_cache) {
for (auto &realm_iter : path_realms.second) {
if (auto realm = realm_iter.second.lock()) {
realm->invalidate();
auto thread_realm = path_realms.second.find(thread_id);
if (thread_realm != path_realms.second.end()) {
if (auto realm = thread_realm->second.lock()) {
realm->close();
}
}
}

View File

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