Improve cyclic collector turning off machinery

Ensure collector doesn't actually run when turned off.
This commit is contained in:
Svyatoslav Scherbina
2020-02-20 11:44:44 +03:00
committed by SvyatoslavScherbina
parent 589cf8edda
commit dc8c9d70c1
3 changed files with 17 additions and 14 deletions
+11 -9
View File
@@ -140,11 +140,11 @@ class CyclicCollector {
toRelease_.clear(); toRelease_.clear();
} }
void terminate() { void terminate(bool enabled) {
{ {
Locker locker(&lock_); Locker locker(&lock_);
terminateCollector_ = true; terminateCollector_ = true;
shallRunCollector_ = true; if (enabled) shallRunCollector_ = true;
CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector") CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector")
} }
// TODO: improve waiting for collector termination. // TODO: improve waiting for collector termination.
@@ -322,12 +322,14 @@ class CyclicCollector {
if (mainWorker_ == nullptr) mainWorker_ = worker; if (mainWorker_ == nullptr) mainWorker_ = worker;
} }
void removeWorker(void* worker) { void removeWorker(void* worker, bool enabled) {
suggestLockRelease(); suggestLockRelease();
Locker lock(&lock_); Locker lock(&lock_);
// When exiting the worker - we shall collect the cyclic garbage here. // When exiting the worker - we shall collect the cyclic garbage here.
shallRunCollector_ = true; if (enabled) {
CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector") shallRunCollector_ = true;
CHECK_CALL(pthread_cond_signal(&cond_), "Cannot signal collector")
}
currentAliveWorkers_--; currentAliveWorkers_--;
} }
@@ -436,11 +438,11 @@ void cyclicInit() {
#endif #endif
} }
void cyclicDeinit() { void cyclicDeinit(bool enabled) {
#if WITH_WORKERS #if WITH_WORKERS
RuntimeAssert(cyclicCollector != nullptr, "Must be inited"); RuntimeAssert(cyclicCollector != nullptr, "Must be inited");
auto* local = cyclicCollector; auto* local = cyclicCollector;
local->terminate(); local->terminate(enabled);
cyclicCollector = nullptr; cyclicCollector = nullptr;
// Workaround data race with threads non-atomically reading and then using [cyclicCollector]. // Workaround data race with threads non-atomically reading and then using [cyclicCollector].
// konanDestructInstance(local); // konanDestructInstance(local);
@@ -458,11 +460,11 @@ void cyclicAddWorker(void* worker) {
#endif // WITH_WORKERS #endif // WITH_WORKERS
} }
void cyclicRemoveWorker(void* worker) { void cyclicRemoveWorker(void* worker, bool enabled) {
#if WITH_WORKERS #if WITH_WORKERS
auto* local = cyclicCollector; auto* local = cyclicCollector;
if (local) if (local)
local->removeWorker(worker); local->removeWorker(worker, enabled);
#endif // WITH_WORKERS #endif // WITH_WORKERS
} }
+2 -2
View File
@@ -4,9 +4,9 @@
struct ObjHeader; struct ObjHeader;
void cyclicInit(); void cyclicInit();
void cyclicDeinit(); void cyclicDeinit(bool enabled);
void cyclicAddWorker(void* worker); void cyclicAddWorker(void* worker);
void cyclicRemoveWorker(void* worker); void cyclicRemoveWorker(void* worker, bool enabled);
void cyclicAddAtomicRoot(ObjHeader* obj); void cyclicAddAtomicRoot(ObjHeader* obj);
void cyclicRemoveAtomicRoot(ObjHeader* obj); void cyclicRemoveAtomicRoot(ObjHeader* obj);
void cyclicMutateAtomicRoot(ObjHeader* newValue); void cyclicMutateAtomicRoot(ObjHeader* newValue);
+4 -3
View File
@@ -1760,7 +1760,7 @@ void deinitMemory(MemoryState* memoryState) {
if (atomicGet(&pendingDeinit) > 1) { if (atomicGet(&pendingDeinit) > 1) {
checkLeaks = false; checkLeaks = false;
} }
cyclicDeinit(); cyclicDeinit(g_hasCyclicCollector);
#endif // USE_CYCLIC_GC #endif // USE_CYCLIC_GC
} }
// Actual GC only implemented in strict memory model at the moment. // Actual GC only implemented in strict memory model at the moment.
@@ -2897,7 +2897,8 @@ void Kotlin_native_internal_GC_collect(KRef) {
void Kotlin_native_internal_GC_collectCyclic(KRef) { void Kotlin_native_internal_GC_collectCyclic(KRef) {
#if USE_CYCLIC_GC #if USE_CYCLIC_GC
cyclicScheduleGarbageCollect(); if (g_hasCyclicCollector)
cyclicScheduleGarbageCollect();
#else #else
ThrowIllegalArgumentException(); ThrowIllegalArgumentException();
#endif #endif
@@ -3067,7 +3068,7 @@ void GC_RegisterWorker(void* worker) {
void GC_UnregisterWorker(void* worker) { void GC_UnregisterWorker(void* worker) {
#if USE_CYCLIC_GC #if USE_CYCLIC_GC
cyclicRemoveWorker(worker); cyclicRemoveWorker(worker, g_hasCyclicCollector);
#endif // USE_CYCLIC_GC #endif // USE_CYCLIC_GC
} }