[K/N][runtime] Add a lock on finalizer thread creation

The finalizer thread can be created from the GC thread or from
any application thread during runtime shutdown. This may cause
a rare race when the GC starts the finalizer thread during runtime
shutdown. In this case two finalizer threads may be created and
the app will crash on attempt to assign the finalizerThread_ field
in FinalizerProcessor.
This commit is contained in:
Ilya Matveev
2022-03-28 14:23:23 +07:00
committed by Space
parent 78d179e9ab
commit 8493b955cc
2 changed files with 4 additions and 0 deletions
@@ -9,7 +9,9 @@
void kotlin::gc::FinalizerProcessor::StartFinalizerThreadIfNone() noexcept {
std::unique_lock guard(threadCreatingMutex_);
if (finalizerThread_.joinable()) return;
finalizerThread_ = ScopedThread(ScopedThread::attributes().name("GC finalizer processor"), [this] {
Kotlin_initRuntimeIfNeeded();
{
@@ -38,6 +38,8 @@ private:
std::mutex initializedMutex_;
std::condition_variable initializedCondVar_;
bool initialized_ = false;
std::mutex threadCreatingMutex_;
};
} // namespace kotlin::gc