Skip to content

Commit 4eac266

Browse files
xmas92pull[bot]
authored andcommitted
8324881: ObjectSynchronizer::inflate(Thread* current...) is invoked for non-current thread
Reviewed-by: rrich, dholmes, coleenp, dcubed
1 parent cea6a51 commit 4eac266

File tree

6 files changed

+352
-58
lines changed

6 files changed

+352
-58
lines changed

src/hotspot/share/runtime/deoptimization.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1646,13 +1646,13 @@ bool Deoptimization::relock_objects(JavaThread* thread, GrowableArray<MonitorInf
16461646
// We have lost information about the correct state of the lock stack.
16471647
// Inflate the locks instead. Enter then inflate to avoid races with
16481648
// deflation.
1649-
ObjectSynchronizer::enter(obj, nullptr, deoptee_thread);
1649+
ObjectSynchronizer::enter_for(obj, nullptr, deoptee_thread);
16501650
assert(mon_info->owner()->is_locked(), "object must be locked now");
1651-
ObjectMonitor* mon = ObjectSynchronizer::inflate(deoptee_thread, obj(), ObjectSynchronizer::inflate_cause_vm_internal);
1651+
ObjectMonitor* mon = ObjectSynchronizer::inflate_for(deoptee_thread, obj(), ObjectSynchronizer::inflate_cause_vm_internal);
16521652
assert(mon->owner() == deoptee_thread, "must be");
16531653
} else {
16541654
BasicLock* lock = mon_info->lock();
1655-
ObjectSynchronizer::enter(obj, lock, deoptee_thread);
1655+
ObjectSynchronizer::enter_for(obj, lock, deoptee_thread);
16561656
assert(mon_info->owner()->is_locked(), "object must be locked now");
16571657
}
16581658
}

src/hotspot/share/runtime/objectMonitor.cpp

+66-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -39,6 +39,7 @@
3939
#include "prims/jvmtiDeferredUpdates.hpp"
4040
#include "prims/jvmtiExport.hpp"
4141
#include "runtime/atomic.hpp"
42+
#include "runtime/globals.hpp"
4243
#include "runtime/handles.inline.hpp"
4344
#include "runtime/interfaceSupport.inline.hpp"
4445
#include "runtime/javaThread.inline.hpp"
@@ -53,6 +54,7 @@
5354
#include "runtime/sharedRuntime.hpp"
5455
#include "services/threadService.hpp"
5556
#include "utilities/dtrace.hpp"
57+
#include "utilities/globalDefinitions.hpp"
5658
#include "utilities/macros.hpp"
5759
#include "utilities/preserveException.hpp"
5860
#if INCLUDE_JFR
@@ -312,7 +314,70 @@ void ObjectMonitor::ClearSuccOnSuspend::operator()(JavaThread* current) {
312314
// -----------------------------------------------------------------------------
313315
// Enter support
314316

317+
bool ObjectMonitor::enter_for(JavaThread* locking_thread) {
318+
// Used by ObjectSynchronizer::enter_for to enter for another thread.
319+
// The monitor is private to or already owned by locking_thread which must be suspended.
320+
// So this code may only contend with deflation.
321+
assert(locking_thread == Thread::current() || locking_thread->is_obj_deopt_suspend(), "must be");
322+
323+
// Block out deflation as soon as possible.
324+
add_to_contentions(1);
325+
326+
bool success = false;
327+
if (!is_being_async_deflated()) {
328+
void* prev_owner = try_set_owner_from(nullptr, locking_thread);
329+
330+
if (prev_owner == nullptr) {
331+
assert(_recursions == 0, "invariant");
332+
success = true;
333+
} else if (prev_owner == locking_thread) {
334+
_recursions++;
335+
success = true;
336+
} else if (prev_owner == DEFLATER_MARKER) {
337+
// Racing with deflation.
338+
prev_owner = try_set_owner_from(DEFLATER_MARKER, locking_thread);
339+
if (prev_owner == DEFLATER_MARKER) {
340+
// Cancelled deflation. Increment contentions as part of the deflation protocol.
341+
add_to_contentions(1);
342+
success = true;
343+
} else if (prev_owner == nullptr) {
344+
// At this point we cannot race with deflation as we have both incremented
345+
// contentions, seen contention > 0 and seen a DEFLATER_MARKER.
346+
// success will only be false if this races with something other than
347+
// deflation.
348+
prev_owner = try_set_owner_from(nullptr, locking_thread);
349+
success = prev_owner == nullptr;
350+
}
351+
} else if (LockingMode == LM_LEGACY && locking_thread->is_lock_owned((address)prev_owner)) {
352+
assert(_recursions == 0, "must be");
353+
_recursions = 1;
354+
set_owner_from_BasicLock(prev_owner, locking_thread);
355+
success = true;
356+
}
357+
assert(success, "Failed to enter_for: locking_thread=" INTPTR_FORMAT
358+
", this=" INTPTR_FORMAT "{owner=" INTPTR_FORMAT "}, observed owner: " INTPTR_FORMAT,
359+
p2i(locking_thread), p2i(this), p2i(owner_raw()), p2i(prev_owner));
360+
} else {
361+
// Async deflation is in progress and our contentions increment
362+
// above lost the race to async deflation. Undo the work and
363+
// force the caller to retry.
364+
const oop l_object = object();
365+
if (l_object != nullptr) {
366+
// Attempt to restore the header/dmw to the object's header so that
367+
// we only retry once if the deflater thread happens to be slow.
368+
install_displaced_markword_in_object(l_object);
369+
}
370+
}
371+
372+
add_to_contentions(-1);
373+
374+
assert(!success || owner_raw() == locking_thread, "must be");
375+
376+
return success;
377+
}
378+
315379
bool ObjectMonitor::enter(JavaThread* current) {
380+
assert(current == JavaThread::current(), "must be");
316381
// The following code is ordered to check the most common cases first
317382
// and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
318383

src/hotspot/share/runtime/objectMonitor.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -329,6 +329,7 @@ class ObjectMonitor : public CHeapObj<mtObjectMonitor> {
329329
void operator()(JavaThread* current);
330330
};
331331
public:
332+
bool enter_for(JavaThread* locking_thread);
332333
bool enter(JavaThread* current);
333334
void exit(JavaThread* current, bool not_suspended = true);
334335
void wait(jlong millis, bool interruptible, TRAPS);

0 commit comments

Comments
 (0)