diff --git a/ecmascript/runtime.cpp b/ecmascript/runtime.cpp index 1f5a2376c1..f77165eca6 100644 --- a/ecmascript/runtime.cpp +++ b/ecmascript/runtime.cpp @@ -166,6 +166,11 @@ void Runtime::ResumeAll(JSThread *current) void Runtime::SuspendAllThreadsImpl(JSThread *current) { LockHolder lock(threadsLock_); + while (suspendNewCount_ != 0) { + // Someone has already suspended all threads. + // Wait until it finishes. + threadSuspendCondVar_.Wait(&threadsLock_); + } suspendNewCount_++; for (auto i : threads_) { if (i != current) { @@ -180,6 +185,10 @@ void Runtime::ResumeAllThreadsImpl(JSThread *current) if (suspendNewCount_ > 0) { suspendNewCount_--; } + if (suspendNewCount_ == 0) { + // Signal to waiting to suspend threads + threadSuspendCondVar_.Signal(); + } for (auto i : threads_) { if (i != current) { i->ResumeThread(true); diff --git a/ecmascript/runtime.h b/ecmascript/runtime.h index a0dbcc9045..3fb1b29c66 100644 --- a/ecmascript/runtime.h +++ b/ecmascript/runtime.h @@ -161,6 +161,7 @@ private: } Mutex threadsLock_; + ConditionVariable threadSuspendCondVar_; Mutex serializeLock_; std::list threads_; uint32_t suspendNewCount_ {0};