|
1 | 1 | /*
|
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
39 | 39 | #include "prims/jvmtiDeferredUpdates.hpp"
|
40 | 40 | #include "prims/jvmtiExport.hpp"
|
41 | 41 | #include "runtime/atomic.hpp"
|
| 42 | +#include "runtime/globals.hpp" |
42 | 43 | #include "runtime/handles.inline.hpp"
|
43 | 44 | #include "runtime/interfaceSupport.inline.hpp"
|
44 | 45 | #include "runtime/javaThread.inline.hpp"
|
|
53 | 54 | #include "runtime/sharedRuntime.hpp"
|
54 | 55 | #include "services/threadService.hpp"
|
55 | 56 | #include "utilities/dtrace.hpp"
|
| 57 | +#include "utilities/globalDefinitions.hpp" |
56 | 58 | #include "utilities/macros.hpp"
|
57 | 59 | #include "utilities/preserveException.hpp"
|
58 | 60 | #if INCLUDE_JFR
|
@@ -312,7 +314,70 @@ void ObjectMonitor::ClearSuccOnSuspend::operator()(JavaThread* current) {
|
312 | 314 | // -----------------------------------------------------------------------------
|
313 | 315 | // Enter support
|
314 | 316 |
|
| 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 | + |
315 | 379 | bool ObjectMonitor::enter(JavaThread* current) {
|
| 380 | + assert(current == JavaThread::current(), "must be"); |
316 | 381 | // The following code is ordered to check the most common cases first
|
317 | 382 | // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
|
318 | 383 |
|
|
0 commit comments