Skip to content

Commit f3ba269

Browse files
committed
8256306: ObjectMonitor::_contentions field should not be 'jint'
Reviewed-by: dholmes, stuefe, dcubed
1 parent 52d5d1b commit f3ba269

File tree

5 files changed

+28
-27
lines changed

5 files changed

+28
-27
lines changed

src/hotspot/share/runtime/objectMonitor.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ bool ObjectMonitor::enter(JavaThread* current) {
485485
// just exited the monitor.
486486
}
487487
if (event.should_commit()) {
488-
event.set_previousOwner((uintptr_t)_previous_owner_tid);
488+
event.set_previousOwner(_previous_owner_tid);
489489
event.commit();
490490
}
491491
OM_PERFDATA_OP(ContendedLockAttempts, inc());
@@ -545,7 +545,7 @@ bool ObjectMonitor::deflate_monitor() {
545545
// Java threads. The GC already broke the association with the object.
546546
set_owner_from(NULL, DEFLATER_MARKER);
547547
assert(contentions() >= 0, "must be non-negative: contentions=%d", contentions());
548-
_contentions = -max_jint;
548+
_contentions = INT_MIN; // minimum negative int
549549
} else {
550550
// Attempt async deflation protocol.
551551

@@ -572,7 +572,7 @@ bool ObjectMonitor::deflate_monitor() {
572572

573573
// Make a zero contentions field negative to force any contending threads
574574
// to retry. This is the second part of the async deflation dance.
575-
if (Atomic::cmpxchg(&_contentions, (jint)0, -max_jint) != 0) {
575+
if (Atomic::cmpxchg(&_contentions, 0, INT_MIN) != 0) {
576576
// Contentions was no longer 0 so we lost the race since the
577577
// ObjectMonitor is now busy. Restore owner to NULL if it is
578578
// still DEFLATER_MARKER:
@@ -2243,7 +2243,7 @@ void ObjectMonitor::print_debug_style_on(outputStream* st) const {
22432243
st->print_cr(" [%d] = '\\0'", (int)sizeof(_pad_buf0) - 1);
22442244
st->print_cr(" }");
22452245
st->print_cr(" _owner = " INTPTR_FORMAT, p2i(owner_raw()));
2246-
st->print_cr(" _previous_owner_tid = " JLONG_FORMAT, _previous_owner_tid);
2246+
st->print_cr(" _previous_owner_tid = " INTPTR_FORMAT, _previous_owner_tid);
22472247
st->print_cr(" _pad_buf1 = {");
22482248
st->print_cr(" [0] = '\\0'");
22492249
st->print_cr(" ...");

src/hotspot/share/runtime/objectMonitor.hpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ class ObjectMonitor : public CHeapObj<mtInternal> {
148148
// Used by async deflation as a marker in the _owner field:
149149
#define DEFLATER_MARKER reinterpret_cast<void*>(-1)
150150
void* volatile _owner; // pointer to owning thread OR BasicLock
151-
volatile jlong _previous_owner_tid; // thread id of the previous owner of the monitor
151+
volatile uintptr_t _previous_owner_tid; // thread id of the previous owner of the monitor
152152
// Separate _owner and _next_om on different cache lines since
153153
// both can have busy multi-threaded access. _previous_owner_tid is only
154154
// changed by ObjectMonitor::exit() so it is a good choice to share the
155155
// cache line with _owner.
156156
DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(void* volatile) +
157-
sizeof(volatile jlong));
157+
sizeof(volatile uintptr_t));
158158
ObjectMonitor* _next_om; // Next ObjectMonitor* linkage
159159
volatile intx _recursions; // recursion count, 0 for first entry
160160
ObjectWaiter* volatile _EntryList; // Threads blocked on entry or reentry.
@@ -168,13 +168,13 @@ class ObjectMonitor : public CHeapObj<mtInternal> {
168168
volatile int _Spinner; // for exit->spinner handoff optimization
169169
volatile int _SpinDuration;
170170

171-
jint _contentions; // Number of active contentions in enter(). It is used by is_busy()
171+
int _contentions; // Number of active contentions in enter(). It is used by is_busy()
172172
// along with other fields to determine if an ObjectMonitor can be
173173
// deflated. It is also used by the async deflation protocol. See
174174
// ObjectMonitor::deflate_monitor().
175175
protected:
176176
ObjectWaiter* volatile _WaitSet; // LL of threads wait()ing on the monitor
177-
volatile jint _waiters; // number of waiting threads
177+
volatile int _waiters; // number of waiting threads
178178
private:
179179
volatile int _WaitSetLock; // protects Wait Queue - simple spinlock
180180

@@ -238,9 +238,10 @@ class ObjectMonitor : public CHeapObj<mtInternal> {
238238

239239
bool is_busy() const {
240240
// TODO-FIXME: assert _owner == null implies _recursions = 0
241-
intptr_t ret_code = _waiters | intptr_t(_cxq) | intptr_t(_EntryList);
242-
if (contentions() > 0) {
243-
ret_code |= contentions();
241+
intptr_t ret_code = intptr_t(_waiters) | intptr_t(_cxq) | intptr_t(_EntryList);
242+
int cnts = contentions(); // read once
243+
if (cnts > 0) {
244+
ret_code |= intptr_t(cnts);
244245
}
245246
if (!owner_is_DEFLATER_MARKER()) {
246247
ret_code |= intptr_t(owner_raw());
@@ -281,10 +282,10 @@ class ObjectMonitor : public CHeapObj<mtInternal> {
281282
// _next_om field. Returns the prior value of the _next_om field.
282283
ObjectMonitor* try_set_next_om(ObjectMonitor* old_value, ObjectMonitor* new_value);
283284

284-
jint waiters() const;
285+
int waiters() const;
285286

286-
jint contentions() const;
287-
void add_to_contentions(jint value);
287+
int contentions() const;
288+
void add_to_contentions(int value);
288289
intx recursions() const { return _recursions; }
289290

290291
// JVM/TI GetObjectMonitorUsage() needs this:

src/hotspot/share/runtime/objectMonitor.inline.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ inline void ObjectMonitor::set_header(markWord hdr) {
5252
Atomic::store(&_header, hdr);
5353
}
5454

55-
inline jint ObjectMonitor::waiters() const {
55+
inline int ObjectMonitor::waiters() const {
5656
return _waiters;
5757
}
5858

@@ -79,12 +79,12 @@ inline bool ObjectMonitor::is_being_async_deflated() {
7979
}
8080

8181
// Return number of threads contending for this monitor.
82-
inline jint ObjectMonitor::contentions() const {
82+
inline int ObjectMonitor::contentions() const {
8383
return Atomic::load(&_contentions);
8484
}
8585

8686
// Add value to the contentions field.
87-
inline void ObjectMonitor::add_to_contentions(jint value) {
87+
inline void ObjectMonitor::add_to_contentions(int value) {
8888
Atomic::add(&_contentions, value);
8989
}
9090

src/hotspot/share/runtime/vmStructs.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,8 +881,8 @@ typedef HashtableEntry<InstanceKlass*, mtClass> KlassHashtableEntry;
881881
unchecked_nonstatic_field(ObjectMonitor, _owner, sizeof(void *)) /* NOTE: no type */ \
882882
volatile_nonstatic_field(ObjectMonitor, _next_om, ObjectMonitor*) \
883883
volatile_nonstatic_field(BasicLock, _displaced_header, markWord) \
884-
nonstatic_field(ObjectMonitor, _contentions, jint) \
885-
volatile_nonstatic_field(ObjectMonitor, _waiters, jint) \
884+
nonstatic_field(ObjectMonitor, _contentions, int) \
885+
volatile_nonstatic_field(ObjectMonitor, _waiters, int) \
886886
volatile_nonstatic_field(ObjectMonitor, _recursions, intx) \
887887
nonstatic_field(BasicObjectLock, _lock, BasicLock) \
888888
nonstatic_field(BasicObjectLock, _obj, oop) \

src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/ObjectMonitor.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2021, 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
@@ -52,9 +52,9 @@ private static synchronized void initialize(TypeDataBase db) throws WrongTypeExc
5252
ownerFieldOffset = f.getOffset();
5353
f = type.getField("_next_om");
5454
nextOMFieldOffset = f.getOffset();
55-
contentionsField = type.getJIntField("_contentions");
56-
waitersField = type.getJIntField("_waiters");
57-
recursionsField = type.getCIntegerField("_recursions");
55+
contentionsField = new CIntField(type.getCIntegerField("_contentions"), 0);
56+
waitersField = new CIntField(type.getCIntegerField("_waiters"), 0);
57+
recursionsField = type.getCIntegerField("_recursions");
5858
}
5959

6060
public ObjectMonitor(Address addr) {
@@ -83,7 +83,7 @@ public boolean isEntered(sun.jvm.hotspot.runtime.Thread current) {
8383
// FIXME
8484
// void set_owner(void* owner);
8585

86-
public int waiters() { return waitersField.getValue(addr); }
86+
public int waiters() { return (int)waitersField.getValue(this); }
8787

8888
public Address nextOM() { return addr.getAddressAt(nextOMFieldOffset); }
8989
// FIXME
@@ -100,7 +100,7 @@ public OopHandle object() {
100100
}
101101

102102
public int contentions() {
103-
return contentionsField.getValue(addr);
103+
return (int)contentionsField.getValue(this);
104104
}
105105

106106
// The following four either aren't expressed as typed fields in
@@ -111,8 +111,8 @@ public int contentions() {
111111
private static long objectFieldOffset;
112112
private static long ownerFieldOffset;
113113
private static long nextOMFieldOffset;
114-
private static JIntField contentionsField;
115-
private static JIntField waitersField;
114+
private static CIntField contentionsField;
115+
private static CIntField waitersField;
116116
private static CIntegerField recursionsField;
117117
// FIXME: expose platform-dependent stuff
118118
}

0 commit comments

Comments
 (0)