author | egahlin |
Fri, 17 May 2019 16:02:27 +0200 | |
branch | JEP-349-branch |
changeset 57360 | 5d043a159d5c |
parent 54645 | 05aaccf7d558 |
child 58159 | 892527a70da9 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
50429
diff
changeset
|
2 |
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 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 |
* |
|
23 |
*/ |
|
24 |
||
25 |
#include "precompiled.hpp" |
|
26 |
#include "jfr/recorder/service/jfrPostBox.hpp" |
|
27 |
#include "jfr/utilities/jfrTryLock.hpp" |
|
28 |
#include "runtime/atomic.hpp" |
|
50429
83aec1d357d4
8204301: Make OrderAccess functions available to hpp rather than inline.hpp files
coleenp
parents:
50113
diff
changeset
|
29 |
#include "runtime/orderAccess.hpp" |
50113 | 30 |
#include "runtime/thread.inline.hpp" |
31 |
||
32 |
#define MSG_IS_SYNCHRONOUS ( (MSGBIT(MSG_ROTATE)) | \ |
|
33 |
(MSGBIT(MSG_STOP)) | \ |
|
34 |
(MSGBIT(MSG_START)) | \ |
|
35 |
(MSGBIT(MSG_CLONE_IN_MEMORY)) | \ |
|
57360 | 36 |
(MSGBIT(MSG_VM_ERROR)) | \ |
37 |
(MSGBIT(MSG_FLUSHPOINT)) | \ |
|
38 |
(MSGBIT(MSG_FLUSHPOINT_METADATA)) \ |
|
50113 | 39 |
) |
40 |
||
41 |
static JfrPostBox* _instance = NULL; |
|
42 |
||
43 |
JfrPostBox& JfrPostBox::instance() { |
|
44 |
return *_instance; |
|
45 |
} |
|
46 |
||
47 |
JfrPostBox* JfrPostBox::create() { |
|
48 |
assert(_instance == NULL, "invariant"); |
|
49 |
_instance = new JfrPostBox(); |
|
50 |
return _instance; |
|
51 |
} |
|
52 |
||
53 |
void JfrPostBox::destroy() { |
|
54 |
assert(_instance != NULL, "invariant"); |
|
55 |
delete _instance; |
|
56 |
_instance = NULL; |
|
57 |
} |
|
58 |
||
59 |
JfrPostBox::JfrPostBox() : |
|
60 |
_msg_read_serial(0), |
|
61 |
_msg_handled_serial(0), |
|
62 |
_messages(0), |
|
63 |
_has_waiters(false) {} |
|
64 |
||
65 |
static bool is_thread_lock_aversive() { |
|
66 |
Thread* const thread = Thread::current(); |
|
67 |
return (thread->is_Java_thread() && ((JavaThread*)thread)->thread_state() != _thread_in_vm) || thread->is_VM_thread(); |
|
68 |
} |
|
69 |
||
70 |
static bool is_synchronous(int messages) { |
|
71 |
return ((messages & MSG_IS_SYNCHRONOUS) != 0); |
|
72 |
} |
|
73 |
||
74 |
void JfrPostBox::post(JFR_Msg msg) { |
|
75 |
const int the_message = MSGBIT(msg); |
|
76 |
if (is_thread_lock_aversive()) { |
|
77 |
deposit(the_message); |
|
78 |
return; |
|
79 |
} |
|
80 |
if (!is_synchronous(the_message)) { |
|
81 |
asynchronous_post(the_message); |
|
82 |
return; |
|
83 |
} |
|
84 |
synchronous_post(the_message); |
|
85 |
} |
|
86 |
||
87 |
void JfrPostBox::deposit(int new_messages) { |
|
88 |
while (true) { |
|
89 |
const int current_msgs = OrderAccess::load_acquire(&_messages); |
|
90 |
// OR the new message |
|
91 |
const int exchange_value = current_msgs | new_messages; |
|
92 |
const int result = Atomic::cmpxchg(exchange_value, &_messages, current_msgs); |
|
93 |
if (result == current_msgs) { |
|
94 |
return; |
|
95 |
} |
|
96 |
/* Some other thread just set exactly what this thread wanted */ |
|
97 |
if ((result & new_messages) == new_messages) { |
|
98 |
return; |
|
99 |
} |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
void JfrPostBox::asynchronous_post(int msg) { |
|
104 |
assert(!is_synchronous(msg), "invariant"); |
|
105 |
deposit(msg); |
|
106 |
JfrMonitorTryLock try_msg_lock(JfrMsg_lock); |
|
107 |
if (try_msg_lock.acquired()) { |
|
108 |
JfrMsg_lock->notify_all(); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
void JfrPostBox::synchronous_post(int msg) { |
|
113 |
assert(is_synchronous(msg), "invariant"); |
|
114 |
assert(!JfrMsg_lock->owned_by_self(), "should not hold JfrMsg_lock here!"); |
|
54645
05aaccf7d558
8222988: Use MonitorLocker rather than MutexLocker when wait/notify used
coleenp
parents:
54623
diff
changeset
|
115 |
MonitorLocker msg_lock(JfrMsg_lock); |
50113 | 116 |
deposit(msg); |
117 |
// serial_id is used to check when what we send in has been processed. |
|
118 |
// _msg_read_serial is read under JfrMsg_lock protection. |
|
119 |
const uintptr_t serial_id = OrderAccess::load_acquire(&_msg_read_serial) + 1; |
|
54645
05aaccf7d558
8222988: Use MonitorLocker rather than MutexLocker when wait/notify used
coleenp
parents:
54623
diff
changeset
|
120 |
msg_lock.notify_all(); |
50113 | 121 |
while (!is_message_processed(serial_id)) { |
54645
05aaccf7d558
8222988: Use MonitorLocker rather than MutexLocker when wait/notify used
coleenp
parents:
54623
diff
changeset
|
122 |
msg_lock.wait(); |
50113 | 123 |
} |
124 |
} |
|
125 |
||
126 |
/* |
|
127 |
* Check if a synchronous message has been processed. |
|
128 |
* We avoid racing on _msg_handled_serial by ensuring |
|
129 |
* that we are holding the JfrMsg_lock when checking |
|
130 |
* completion status. |
|
131 |
*/ |
|
132 |
bool JfrPostBox::is_message_processed(uintptr_t serial_id) const { |
|
133 |
assert(JfrMsg_lock->owned_by_self(), "_msg_handled_serial must be read under JfrMsg_lock protection"); |
|
134 |
return serial_id <= OrderAccess::load_acquire(&_msg_handled_serial); |
|
135 |
} |
|
136 |
||
137 |
bool JfrPostBox::is_empty() const { |
|
138 |
assert(JfrMsg_lock->owned_by_self(), "not holding JfrMsg_lock!"); |
|
139 |
return OrderAccess::load_acquire(&_messages) == 0; |
|
140 |
} |
|
141 |
||
142 |
int JfrPostBox::collect() { |
|
143 |
// get pending and reset to 0 |
|
144 |
const int messages = Atomic::xchg(0, &_messages); |
|
145 |
if (check_waiters(messages)) { |
|
146 |
_has_waiters = true; |
|
147 |
assert(JfrMsg_lock->owned_by_self(), "incrementing _msg_read_serial is protected by JfrMsg_lock"); |
|
148 |
// Update made visible on release of JfrMsg_lock via fence instruction in Monitor::IUnlock. |
|
149 |
++_msg_read_serial; |
|
150 |
} |
|
151 |
return messages; |
|
152 |
} |
|
153 |
||
154 |
bool JfrPostBox::check_waiters(int messages) const { |
|
155 |
assert(JfrMsg_lock->owned_by_self(), "not holding JfrMsg_lock!"); |
|
156 |
assert(!_has_waiters, "invariant"); |
|
157 |
return is_synchronous(messages); |
|
158 |
} |
|
159 |
||
160 |
void JfrPostBox::notify_waiters() { |
|
161 |
if (!_has_waiters) { |
|
162 |
return; |
|
163 |
} |
|
164 |
_has_waiters = false; |
|
165 |
assert(JfrMsg_lock->owned_by_self(), "incrementing _msg_handled_serial is protected by JfrMsg_lock."); |
|
166 |
// Update made visible on release of JfrMsg_lock via fence instruction in Monitor::IUnlock. |
|
167 |
++_msg_handled_serial; |
|
168 |
JfrMsg_lock->notify(); |
|
169 |
} |
|
170 |
||
171 |
// safeguard to ensure no threads are left waiting |
|
172 |
void JfrPostBox::notify_collection_stop() { |
|
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
50429
diff
changeset
|
173 |
MutexLocker msg_lock(JfrMsg_lock); |
50113 | 174 |
JfrMsg_lock->notify_all(); |
175 |
} |