[K/N] Use already dereferenced weakReferee (KT-66371)

And prevent weak barriers from execution during STW

Merge-request: KT-MR-14797
Merged-by: Alexey Glushko <aleksei.glushko@jetbrains.com>
This commit is contained in:
Aleksei.Glushko
2024-03-13 14:43:59 +00:00
committed by Space Team
parent 284d5437e5
commit f2501ad424
2 changed files with 10 additions and 1 deletions
@@ -187,13 +187,19 @@ ALWAYS_INLINE ObjHeader* gc::barriers::weakRefReadBarrier(std::atomic<ObjHeader*
auto phase = currentPhase();
BarriersLogDebug(phase, "Weak read %p", weak);
// The weak read protector switches the thread state to native, thus making this code able to execute during STW.
// However, this is only possible with the disabled barriers.
// Be extra cautious not to access or modify heap structure here. e.g. do not allocate objects
AssertThreadState(ThreadState::kNative);
RuntimeAssert(!mm::IsThreadSuspensionRequested() || phase == BarriersPhase::kDisabled, "Unexpected barriers phase during STW: %s", toString(phase));
if (__builtin_expect(phase == BarriersPhase::kMarkClosure, false)) {
weakRefReadInMarkSlowPath(weak);
} else {
if (__builtin_expect(phase == BarriersPhase::kWeakProcessing, false)) {
// TODO reread the referee here under the barrier guard
// if `disableBarriers` would be possible outside of STW
return weakRefReadInWeakSweepSlowPath(weakReferee);
return weakRefReadInWeakSweepSlowPath(weak);
}
}
return weak;
@@ -87,6 +87,9 @@ void gc::mark::ConcurrentMark::runMainInSTW() {
gc::processWeaks<DefaultProcessWeaksTraits>(gcHandle(), mm::SpecialRefRegistry::instance());
if (!terminateInSTW) {
// Mutator threads execute weak barrier in "native" state. This hack maes them stop with the rest of the world.
std::unique_lock markTerminationGuard(markTerminationMutex_);
stopTheWorld(gcHandle(), "GC stop the world #2: prepare to sweep");
}