[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:
@@ -9,7 +9,9 @@
|
|||||||
|
|
||||||
|
|
||||||
void kotlin::gc::FinalizerProcessor::StartFinalizerThreadIfNone() noexcept {
|
void kotlin::gc::FinalizerProcessor::StartFinalizerThreadIfNone() noexcept {
|
||||||
|
std::unique_lock guard(threadCreatingMutex_);
|
||||||
if (finalizerThread_.joinable()) return;
|
if (finalizerThread_.joinable()) return;
|
||||||
|
|
||||||
finalizerThread_ = ScopedThread(ScopedThread::attributes().name("GC finalizer processor"), [this] {
|
finalizerThread_ = ScopedThread(ScopedThread::attributes().name("GC finalizer processor"), [this] {
|
||||||
Kotlin_initRuntimeIfNeeded();
|
Kotlin_initRuntimeIfNeeded();
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -38,6 +38,8 @@ private:
|
|||||||
std::mutex initializedMutex_;
|
std::mutex initializedMutex_;
|
||||||
std::condition_variable initializedCondVar_;
|
std::condition_variable initializedCondVar_;
|
||||||
bool initialized_ = false;
|
bool initialized_ = false;
|
||||||
|
|
||||||
|
std::mutex threadCreatingMutex_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace kotlin::gc
|
} // namespace kotlin::gc
|
||||||
|
|||||||
Reference in New Issue
Block a user