1 /*
2 * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
31 #include "oops/markOop.hpp"
32 #include "oops/oop.inline.hpp"
33 #include "runtime/atomic.inline.hpp"
34 #include "runtime/biasedLocking.hpp"
35 #include "runtime/handles.inline.hpp"
36 #include "runtime/interfaceSupport.hpp"
37 #include "runtime/mutexLocker.hpp"
38 #include "runtime/objectMonitor.hpp"
39 #include "runtime/objectMonitor.inline.hpp"
40 #include "runtime/osThread.hpp"
41 #include "runtime/stubRoutines.hpp"
42 #include "runtime/synchronizer.hpp"
43 #include "runtime/thread.inline.hpp"
44 #include "runtime/vframe.hpp"
45 #include "trace/traceMacros.hpp"
46 #include "trace/tracing.hpp"
47 #include "utilities/dtrace.hpp"
48 #include "utilities/events.hpp"
49 #include "utilities/preserveException.hpp"
50
51 #if defined(__GNUC__) && !defined(PPC64)
52 // Need to inhibit inlining for older versions of GCC to avoid build-time failures
53 #define NOINLINE __attribute__((noinline))
54 #else
55 #define NOINLINE
56 #endif
57
58 // The "core" versions of monitor enter and exit reside in this file.
59 // The interpreter and compilers contain specialized transliterated
60 // variants of the enter-exit fast-path operations. See i486.ad fast_lock(),
61 // for instance. If you make changes here, make sure to modify the
62 // interpreter, and both C1 and C2 fast-path inline locking code emission.
63 //
64 // -----------------------------------------------------------------------------
65
66 #ifdef DTRACE_ENABLED
67
68 // Only bother with this argument setup if dtrace is available
69 // TODO-FIXME: probes should not fire when caller is _blocked. assert() accordingly.
70
71 #define DTRACE_MONITOR_PROBE_COMMON(obj, thread) \
72 char* bytes = NULL; \
73 int len = 0; \
74 jlong jtid = SharedRuntime::get_java_tid(thread); \
75 Symbol* klassname = ((oop)(obj))->klass()->name(); \
76 if (klassname != NULL) { \
77 bytes = (char*)klassname->bytes(); \
1021 tty->flush();
1022 }
1023 }
1024 }
1025
1026 void ObjectSynchronizer::verifyInUse(Thread *Self) {
1027 ObjectMonitor* mid;
1028 int in_use_tally = 0;
1029 for (mid = Self->omInUseList; mid != NULL; mid = mid->FreeNext) {
1030 in_use_tally++;
1031 }
1032 assert(in_use_tally == Self->omInUseCount, "in-use count off");
1033
1034 int free_tally = 0;
1035 for (mid = Self->omFreeList; mid != NULL; mid = mid->FreeNext) {
1036 free_tally++;
1037 }
1038 assert(free_tally == Self->omFreeCount, "free count off");
1039 }
1040
1041 ObjectMonitor * NOINLINE ObjectSynchronizer::omAlloc(Thread * Self) {
1042 // A large MAXPRIVATE value reduces both list lock contention
1043 // and list coherency traffic, but also tends to increase the
1044 // number of objectMonitors in circulation as well as the STW
1045 // scavenge costs. As usual, we lean toward time in space-time
1046 // tradeoffs.
1047 const int MAXPRIVATE = 1024;
1048 for (;;) {
1049 ObjectMonitor * m;
1050
1051 // 1: try to allocate from the thread's local omFreeList.
1052 // Threads will attempt to allocate first from their local list, then
1053 // from the global list, and only after those attempts fail will the thread
1054 // attempt to instantiate new monitors. Thread-local free lists take
1055 // heat off the gListLock and improve allocation latency, as well as reducing
1056 // coherency traffic on the shared global list.
1057 m = Self->omFreeList;
1058 if (m != NULL) {
1059 Self->omFreeList = m->FreeNext;
1060 Self->omFreeCount--;
1061 // CONSIDER: set m->FreeNext = BAD -- diagnostic hygiene
1296 gOmInUseCount += inUseTally;
1297 }
1298
1299 Thread::muxRelease(&gListLock);
1300 TEVENT(omFlush);
1301 }
1302
1303 // Fast path code shared by multiple functions
1304 ObjectMonitor* ObjectSynchronizer::inflate_helper(oop obj) {
1305 markOop mark = obj->mark();
1306 if (mark->has_monitor()) {
1307 assert(ObjectSynchronizer::verify_objmon_isinpool(mark->monitor()), "monitor is invalid");
1308 assert(mark->monitor()->header()->is_neutral(), "monitor must record a good object header");
1309 return mark->monitor();
1310 }
1311 return ObjectSynchronizer::inflate(Thread::current(),
1312 obj,
1313 inflate_cause_vm_internal);
1314 }
1315
1316 ObjectMonitor * NOINLINE ObjectSynchronizer::inflate(Thread * Self,
1317 oop object,
1318 const InflateCause cause) {
1319
1320 // Inflate mutates the heap ...
1321 // Relaxing assertion for bug 6320749.
1322 assert(Universe::verify_in_progress() ||
1323 !SafepointSynchronize::is_at_safepoint(), "invariant");
1324
1325 EventJavaMonitorInflate event;
1326
1327 for (;;) {
1328 const markOop mark = object->mark();
1329 assert(!mark->has_bias_pattern(), "invariant");
1330
1331 // The mark can be in one of the following states:
1332 // * Inflated - just return
1333 // * Stack-locked - coerce it to inflated
1334 // * INFLATING - busy wait for conversion to complete
1335 // * Neutral - aggressively inflate the object.
1336 // * BIASED - Illegal. We should never see this
|
1 /*
2 * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
31 #include "oops/markOop.hpp"
32 #include "oops/oop.inline.hpp"
33 #include "runtime/atomic.inline.hpp"
34 #include "runtime/biasedLocking.hpp"
35 #include "runtime/handles.inline.hpp"
36 #include "runtime/interfaceSupport.hpp"
37 #include "runtime/mutexLocker.hpp"
38 #include "runtime/objectMonitor.hpp"
39 #include "runtime/objectMonitor.inline.hpp"
40 #include "runtime/osThread.hpp"
41 #include "runtime/stubRoutines.hpp"
42 #include "runtime/synchronizer.hpp"
43 #include "runtime/thread.inline.hpp"
44 #include "runtime/vframe.hpp"
45 #include "trace/traceMacros.hpp"
46 #include "trace/tracing.hpp"
47 #include "utilities/dtrace.hpp"
48 #include "utilities/events.hpp"
49 #include "utilities/preserveException.hpp"
50
51 // The "core" versions of monitor enter and exit reside in this file.
52 // The interpreter and compilers contain specialized transliterated
53 // variants of the enter-exit fast-path operations. See i486.ad fast_lock(),
54 // for instance. If you make changes here, make sure to modify the
55 // interpreter, and both C1 and C2 fast-path inline locking code emission.
56 //
57 // -----------------------------------------------------------------------------
58
59 #ifdef DTRACE_ENABLED
60
61 // Only bother with this argument setup if dtrace is available
62 // TODO-FIXME: probes should not fire when caller is _blocked. assert() accordingly.
63
64 #define DTRACE_MONITOR_PROBE_COMMON(obj, thread) \
65 char* bytes = NULL; \
66 int len = 0; \
67 jlong jtid = SharedRuntime::get_java_tid(thread); \
68 Symbol* klassname = ((oop)(obj))->klass()->name(); \
69 if (klassname != NULL) { \
70 bytes = (char*)klassname->bytes(); \
1014 tty->flush();
1015 }
1016 }
1017 }
1018
1019 void ObjectSynchronizer::verifyInUse(Thread *Self) {
1020 ObjectMonitor* mid;
1021 int in_use_tally = 0;
1022 for (mid = Self->omInUseList; mid != NULL; mid = mid->FreeNext) {
1023 in_use_tally++;
1024 }
1025 assert(in_use_tally == Self->omInUseCount, "in-use count off");
1026
1027 int free_tally = 0;
1028 for (mid = Self->omFreeList; mid != NULL; mid = mid->FreeNext) {
1029 free_tally++;
1030 }
1031 assert(free_tally == Self->omFreeCount, "free count off");
1032 }
1033
1034 ObjectMonitor* ObjectSynchronizer::omAlloc(Thread * Self) {
1035 // A large MAXPRIVATE value reduces both list lock contention
1036 // and list coherency traffic, but also tends to increase the
1037 // number of objectMonitors in circulation as well as the STW
1038 // scavenge costs. As usual, we lean toward time in space-time
1039 // tradeoffs.
1040 const int MAXPRIVATE = 1024;
1041 for (;;) {
1042 ObjectMonitor * m;
1043
1044 // 1: try to allocate from the thread's local omFreeList.
1045 // Threads will attempt to allocate first from their local list, then
1046 // from the global list, and only after those attempts fail will the thread
1047 // attempt to instantiate new monitors. Thread-local free lists take
1048 // heat off the gListLock and improve allocation latency, as well as reducing
1049 // coherency traffic on the shared global list.
1050 m = Self->omFreeList;
1051 if (m != NULL) {
1052 Self->omFreeList = m->FreeNext;
1053 Self->omFreeCount--;
1054 // CONSIDER: set m->FreeNext = BAD -- diagnostic hygiene
1289 gOmInUseCount += inUseTally;
1290 }
1291
1292 Thread::muxRelease(&gListLock);
1293 TEVENT(omFlush);
1294 }
1295
1296 // Fast path code shared by multiple functions
1297 ObjectMonitor* ObjectSynchronizer::inflate_helper(oop obj) {
1298 markOop mark = obj->mark();
1299 if (mark->has_monitor()) {
1300 assert(ObjectSynchronizer::verify_objmon_isinpool(mark->monitor()), "monitor is invalid");
1301 assert(mark->monitor()->header()->is_neutral(), "monitor must record a good object header");
1302 return mark->monitor();
1303 }
1304 return ObjectSynchronizer::inflate(Thread::current(),
1305 obj,
1306 inflate_cause_vm_internal);
1307 }
1308
1309 ObjectMonitor* ObjectSynchronizer::inflate(Thread * Self,
1310 oop object,
1311 const InflateCause cause) {
1312
1313 // Inflate mutates the heap ...
1314 // Relaxing assertion for bug 6320749.
1315 assert(Universe::verify_in_progress() ||
1316 !SafepointSynchronize::is_at_safepoint(), "invariant");
1317
1318 EventJavaMonitorInflate event;
1319
1320 for (;;) {
1321 const markOop mark = object->mark();
1322 assert(!mark->has_bias_pattern(), "invariant");
1323
1324 // The mark can be in one of the following states:
1325 // * Inflated - just return
1326 // * Stack-locked - coerce it to inflated
1327 // * INFLATING - busy wait for conversion to complete
1328 // * Neutral - aggressively inflate the object.
1329 // * BIASED - Illegal. We should never see this
|