mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 00:13:50 +00:00
![Martin Kustermann](/assets/img/avatar_default.png)
This extends the existing safepoint operation mechanism by allowing to perform two different operations: * "gc safepoint operations": All mutators are stopped at places where it's safe to GC. It therefore requires stackmaps to be available for all optimized mutator frames. * "deopt safepoint operations": All mutators are stopped at places where it's safe to GC, but also safe to lazy-deopt mutator frames. It therefore requires deopt-id/deopt-info to be available for all optimized mutator frames. Mutators can be asked to block for any of those two safepoint operations. If a mutator is at a place where its safe to GC it will respond to "gc safepoint operations" requests, if a mutator is additionally at a place where it's also safe to lazy-deopt it will respond to "deopt safepoint operation" requests. Depending on how the runtime was entered (which is tracked via the [Thread::runtime_call_deopt_ability_] value) - the mutator might participate in both or only in gc safepoint operations. During the start of a "deopt safepoint operation", the safepoint handler will request all threads to stop at a "deopt safepoint". Some threads might first want to initiate their own "gc safepoint operation" (e.g. due to allocation failure) before they reach a "deopt safepoint". We do allow this by letting the safepoint handler own a "deopt safepoint operation" but still participate in other thread's "gc safepoint operation" requests until all mutators are checked into places where it's safe to lazy-deopt at which point the "deopt safepoint operation" also owns a "gc safepoint operation". In order to facilitate this, the Thread's safepoint_state will be extended to consist of the following bits: * AtSafepoint * SafepointRequested * AtDeoptSafepoint * DeoptSafepointRequested * BlockedForSafepoint Issue https://github.com/dart-lang/sdk/issues/45213 TEST=vm/cc/SafepointOperation_* Change-Id: Icdc2827718f6780818f99b829a5e806d6bb5b130 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/196927 Commit-Queue: Martin Kustermann <kustermann@google.com> Reviewed-by: Vyacheslav Egorov <vegorov@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com>
214 lines
5.6 KiB
C++
214 lines
5.6 KiB
C++
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
#include "vm/lockers.h"
|
|
#include "platform/assert.h"
|
|
#include "vm/heap/safepoint.h"
|
|
#include "vm/isolate.h"
|
|
|
|
namespace dart {
|
|
|
|
Monitor::WaitResult MonitorLocker::WaitWithSafepointCheck(Thread* thread,
|
|
int64_t millis) {
|
|
ASSERT(thread == Thread::Current());
|
|
ASSERT(thread->execution_state() == Thread::kThreadInVM);
|
|
#if defined(DEBUG)
|
|
if (no_safepoint_scope_) {
|
|
thread->DecrementNoSafepointScopeDepth();
|
|
}
|
|
#endif
|
|
thread->set_execution_state(Thread::kThreadInBlockedState);
|
|
thread->EnterSafepoint();
|
|
Monitor::WaitResult result = monitor_->Wait(millis);
|
|
// First try a fast update of the thread state to indicate it is not at a
|
|
// safepoint anymore.
|
|
if (!thread->TryExitSafepoint()) {
|
|
// Fast update failed which means we could potentially be in the middle
|
|
// of a safepoint operation and need to block for it.
|
|
monitor_->Exit();
|
|
thread->ExitSafepointUsingLock();
|
|
monitor_->Enter();
|
|
}
|
|
thread->set_execution_state(Thread::kThreadInVM);
|
|
#if defined(DEBUG)
|
|
if (no_safepoint_scope_) {
|
|
thread->IncrementNoSafepointScopeDepth();
|
|
}
|
|
#endif
|
|
return result;
|
|
}
|
|
|
|
SafepointMutexLocker::SafepointMutexLocker(ThreadState* thread, Mutex* mutex)
|
|
: StackResource(thread), mutex_(mutex) {
|
|
ASSERT(mutex != NULL);
|
|
if (!mutex_->TryLock()) {
|
|
// We did not get the lock and could potentially block, so transition
|
|
// accordingly.
|
|
Thread* thread = Thread::Current();
|
|
if (thread != NULL) {
|
|
TransitionVMToBlocked transition(thread);
|
|
mutex->Lock();
|
|
} else {
|
|
mutex->Lock();
|
|
}
|
|
}
|
|
}
|
|
|
|
void SafepointMonitorLocker::AcquireLock() {
|
|
ASSERT(monitor_ != NULL);
|
|
if (!monitor_->TryEnter()) {
|
|
// We did not get the lock and could potentially block, so transition
|
|
// accordingly.
|
|
Thread* thread = Thread::Current();
|
|
if (thread != NULL) {
|
|
TransitionVMToBlocked transition(thread);
|
|
monitor_->Enter();
|
|
} else {
|
|
monitor_->Enter();
|
|
}
|
|
}
|
|
}
|
|
|
|
void SafepointMonitorLocker::ReleaseLock() {
|
|
monitor_->Exit();
|
|
}
|
|
|
|
Monitor::WaitResult SafepointMonitorLocker::Wait(int64_t millis) {
|
|
Thread* thread = Thread::Current();
|
|
if (thread != NULL) {
|
|
Monitor::WaitResult result;
|
|
{
|
|
TransitionVMToBlocked transition(thread);
|
|
result = monitor_->Wait(millis);
|
|
}
|
|
return result;
|
|
} else {
|
|
return monitor_->Wait(millis);
|
|
}
|
|
}
|
|
|
|
#if defined(DEBUG)
|
|
bool SafepointRwLock::IsCurrentThreadReader() {
|
|
ThreadId id = OSThread::GetCurrentThreadId();
|
|
if (IsCurrentThreadWriter()) {
|
|
return true;
|
|
}
|
|
MonitorLocker ml(&monitor_);
|
|
for (intptr_t i = readers_ids_.length() - 1; i >= 0; i--) {
|
|
if (readers_ids_.At(i) == id) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
#endif // defined(DEBUG)
|
|
|
|
bool SafepointRwLock::EnterRead() {
|
|
// No need to safepoint if the current thread is not attached.
|
|
auto thread = Thread::Current();
|
|
// Attempt to acquire a lock while owning a safepoint could lead to a deadlock
|
|
// (some other thread might be forced to a safepoint while holding this lock).
|
|
ASSERT(thread == nullptr ||
|
|
!thread->isolate_group()->safepoint_handler()->IsOwnedByTheThread(
|
|
thread));
|
|
|
|
const bool can_block_without_safepoint = thread == nullptr;
|
|
|
|
bool acquired_read_lock = false;
|
|
if (!TryEnterRead(can_block_without_safepoint, &acquired_read_lock)) {
|
|
// Important: must never hold monitor_ when blocking for safepoint.
|
|
TransitionVMToBlocked transition(thread);
|
|
const bool ok = TryEnterRead(/*can_block=*/true, &acquired_read_lock);
|
|
RELEASE_ASSERT(ok);
|
|
RELEASE_ASSERT(acquired_read_lock);
|
|
}
|
|
return acquired_read_lock;
|
|
}
|
|
|
|
bool SafepointRwLock::TryEnterRead(bool can_block, bool* acquired_read_lock) {
|
|
MonitorLocker ml(&monitor_);
|
|
if (IsCurrentThreadWriter()) {
|
|
*acquired_read_lock = false;
|
|
return true;
|
|
}
|
|
if (can_block) {
|
|
while (state_ < 0) {
|
|
ml.Wait();
|
|
}
|
|
}
|
|
if (state_ >= 0) {
|
|
++state_;
|
|
DEBUG_ONLY(readers_ids_.Add(OSThread::GetCurrentThreadId()));
|
|
*acquired_read_lock = true;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void SafepointRwLock::LeaveRead() {
|
|
MonitorLocker ml(&monitor_);
|
|
ASSERT(state_ > 0);
|
|
#if defined(DEBUG)
|
|
{
|
|
intptr_t i = readers_ids_.length() - 1;
|
|
ThreadId id = OSThread::GetCurrentThreadId();
|
|
while (i >= 0) {
|
|
if (readers_ids_.At(i) == id) {
|
|
readers_ids_.RemoveAt(i);
|
|
break;
|
|
}
|
|
i--;
|
|
}
|
|
ASSERT(i >= 0);
|
|
}
|
|
#endif
|
|
if (--state_ == 0) {
|
|
ml.NotifyAll();
|
|
}
|
|
}
|
|
|
|
void SafepointRwLock::EnterWrite() {
|
|
// No need to safepoint if the current thread is not attached.
|
|
auto thread = Thread::Current();
|
|
const bool can_block_without_safepoint = thread == nullptr;
|
|
|
|
if (!TryEnterWrite(can_block_without_safepoint)) {
|
|
// Important: must never hold monitor_ when blocking for safepoint.
|
|
TransitionVMToBlocked transition(thread);
|
|
const bool ok = TryEnterWrite(/*can_block=*/true);
|
|
RELEASE_ASSERT(ok);
|
|
}
|
|
}
|
|
|
|
bool SafepointRwLock::TryEnterWrite(bool can_block) {
|
|
MonitorLocker ml(&monitor_);
|
|
if (IsCurrentThreadWriter()) {
|
|
state_--;
|
|
return true;
|
|
}
|
|
if (can_block) {
|
|
while (state_ != 0) {
|
|
ml.Wait();
|
|
}
|
|
}
|
|
if (state_ == 0) {
|
|
writer_id_ = OSThread::GetCurrentThreadId();
|
|
state_ = -1;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void SafepointRwLock::LeaveWrite() {
|
|
MonitorLocker ml(&monitor_);
|
|
ASSERT(state_ < 0);
|
|
if (++state_ < 0) {
|
|
return;
|
|
}
|
|
writer_id_ = OSThread::kInvalidThreadId;
|
|
ml.NotifyAll();
|
|
}
|
|
|
|
} // namespace dart
|