-
Notifications
You must be signed in to change notification settings - Fork 752
/
Copy pathContinuationHelpers.hpp
325 lines (284 loc) · 11.3 KB
/
ContinuationHelpers.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*******************************************************************************
* Copyright IBM Corp. and others 2022
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#if !defined(CONTINUATIONHELPERS_HPP_)
#define CONTINUATIONHELPERS_HPP_
#include "j9.h"
#include "j9consts.h"
#include "j9vmconstantpool.h"
/* These should match the error code values in enum Pinned within class Continuation. */
#define J9VM_CONTINUATION_PINNED_REASON_NATIVE 1
#define J9VM_CONTINUATION_PINNED_REASON_MONITOR 2
#define J9VM_CONTINUATION_PINNED_REASON_CRITICAL_SECTION 3
#if JAVA_SPEC_VERSION >= 24
#define J9VM_CONTINUATION_PINNED_REASON_EXCEPTION 4
#endif /* JAVA_SPEC_VERSION >= 24 */
class VM_ContinuationHelpers {
/*
* Data members
*/
private:
protected:
public:
/*
* Function members
*/
private:
protected:
public:
static VMINLINE void
swapFieldsWithContinuation(J9VMThread *vmThread, J9VMContinuation *continuation, j9object_t continuationObject, bool swapJ9VMthreadSavedRegisters = true)
{
/* Helper macro to swap fields between the two J9Class structs. */
#define SWAP_MEMBER(fieldName, fieldType, class1, class2) \
do { \
fieldType temp = (fieldType)((class1)->fieldName); \
(class1)->fieldName = (class2)->fieldName; \
(class2)->fieldName = (fieldType)temp; \
} while (0)
SWAP_MEMBER(arg0EA, UDATA*, vmThread, continuation);
SWAP_MEMBER(bytecodes, UDATA*, vmThread, continuation);
SWAP_MEMBER(sp, UDATA*, vmThread, continuation);
SWAP_MEMBER(pc, U_8*, vmThread, continuation);
SWAP_MEMBER(literals, J9Method*, vmThread, continuation);
SWAP_MEMBER(stackOverflowMark, UDATA*, vmThread, continuation);
SWAP_MEMBER(stackOverflowMark2, UDATA*, vmThread, continuation);
SWAP_MEMBER(stackObject, J9JavaStack*, vmThread, continuation);
SWAP_MEMBER(decompilationStack, J9JITDecompilationInfo*, vmThread, continuation);
SWAP_MEMBER(j2iFrame, UDATA*, vmThread, continuation);
SWAP_MEMBER(dropFlags, UDATA, vmThread, continuation);
J9VMEntryLocalStorage *threadELS = vmThread->entryLocalStorage;
/* Swap the JIT GPR registers data referenced by ELS */
J9JITGPRSpillArea tempGPRs = continuation->jitGPRs;
J9I2JState tempI2J = continuation->i2jState;
continuation->jitGPRs = *(J9JITGPRSpillArea*)threadELS->jitGlobalStorageBase;
continuation->i2jState = threadELS->i2jState;
if (swapJ9VMthreadSavedRegisters) {
*(J9JITGPRSpillArea*)threadELS->jitGlobalStorageBase = tempGPRs;
}
threadELS->i2jState = tempI2J;
SWAP_MEMBER(oldEntryLocalStorage, J9VMEntryLocalStorage*, threadELS, continuation);
j9object_t scopedValueCache = J9VMJDKINTERNALVMCONTINUATION_SCOPEDVALUECACHE(vmThread, continuationObject);
J9VMJDKINTERNALVMCONTINUATION_SET_SCOPEDVALUECACHE(vmThread, continuationObject, vmThread->scopedValueCache);
vmThread->scopedValueCache = scopedValueCache;
#if JAVA_SPEC_VERSION >= 24
if (J9_ARE_ANY_BITS_SET(vmThread->javaVM->extendedRuntimeFlags3, J9_EXTENDED_RUNTIME3_YIELD_PINNED_CONTINUATION)) {
SWAP_MEMBER(ownedMonitorCount, UDATA, vmThread, continuation);
SWAP_MEMBER(monitorEnterRecordPool, J9Pool*, vmThread, continuation);
SWAP_MEMBER(monitorEnterRecords, J9MonitorEnterRecord*, vmThread, continuation);
SWAP_MEMBER(jniMonitorEnterRecords, J9MonitorEnterRecord*, vmThread, continuation);
}
#endif /* JAVA_SPEC_VERSION >= 24 */
}
static VMINLINE ContinuationState volatile *
getContinuationStateAddress(J9VMThread *vmThread , j9object_t object)
{
return (ContinuationState volatile *) ((uintptr_t) object + J9VMJDKINTERNALVMCONTINUATION_STATE_OFFSET(vmThread));
}
static VMINLINE bool
isStarted(ContinuationState continuationState)
{
return J9_ARE_ALL_BITS_SET(continuationState, J9_GC_CONTINUATION_STATE_STARTED);
}
static VMINLINE void
setStarted(ContinuationState volatile *continuationStatePtr)
{
*continuationStatePtr |= J9_GC_CONTINUATION_STATE_STARTED;
}
static VMINLINE bool
isLastUnmount(ContinuationState continuationState)
{
return J9_ARE_ALL_BITS_SET(continuationState, J9_GC_CONTINUATION_STATE_LAST_UNMOUNT);
}
static VMINLINE void
setLastUnmount(ContinuationState volatile *continuationStatePtr)
{
*continuationStatePtr |= J9_GC_CONTINUATION_STATE_LAST_UNMOUNT;
}
static VMINLINE bool
isFinished(ContinuationState continuationState)
{
return J9_ARE_ALL_BITS_SET(continuationState, J9_GC_CONTINUATION_STATE_FINISHED);
}
static VMINLINE void
setFinished(ContinuationState volatile *continuationStatePtr)
{
*continuationStatePtr |= J9_GC_CONTINUATION_STATE_FINISHED;
}
static VMINLINE bool
isActive(ContinuationState continuationState)
{
return isStarted(continuationState) && !isFinished(continuationState);
}
static VMINLINE uintptr_t
getConcurrentGCMask(bool isGlobalGC)
{
if (isGlobalGC) {
return J9_GC_CONTINUATION_STATE_CONCURRENT_SCAN_GLOBAL;
} else {
return J9_GC_CONTINUATION_STATE_CONCURRENT_SCAN_LOCAL;
}
}
/**
* Check if the related J9VMContinuation is concurrently scaned from the state
* 2 variants with and without bool isGlobalGC param.
* without isGlobalGC param, return true if it is either local concurrent scanning case or global concurrent scanning case.
* with isGlobalGC param, if isGlobalGC == true, only check if global concurrent scanning case.
* if isGlobalGC == false, only check if local concurrent scanning case.
*/
static VMINLINE bool
isConcurrentlyScanned(ContinuationState continuationState)
{
return J9_ARE_ANY_BITS_SET(continuationState, J9_GC_CONTINUATION_STATE_CONCURRENT_SCAN_ANY);
}
static VMINLINE bool
isConcurrentlyScanned(ContinuationState continuationState, bool isGlobalGC)
{
uintptr_t concurrentGCMask = getConcurrentGCMask(isGlobalGC);
return J9_ARE_ALL_BITS_SET(continuationState, concurrentGCMask);
}
static VMINLINE void
setConcurrentlyScanned(ContinuationState *continuationStatePtr, bool isGlobalGC)
{
*continuationStatePtr |= getConcurrentGCMask(isGlobalGC);
}
static VMINLINE void
resetConcurrentlyScanned(ContinuationState *continuationStatePtr, bool isGlobalGC)
{
*continuationStatePtr &= ~getConcurrentGCMask(isGlobalGC);
}
static VMINLINE bool
isPendingToBeMounted(ContinuationState continuationState)
{
return J9_ARE_ALL_BITS_SET(continuationState, J9_GC_CONTINUATION_STATE_PENDING_TO_BE_MOUNTED);
}
static VMINLINE void
resetPendingState(ContinuationState *continuationStatePtr)
{
*continuationStatePtr &= ~J9_GC_CONTINUATION_STATE_PENDING_TO_BE_MOUNTED;
}
/**
* Check if the related continuation is fully mounted to carrier thread
* if carrierThreadID has been set in ContinuationState, the continuation might be mounted,
* there also is pending to be mounted case, when the mounting is blocked by the concurrent continuation
* scanning or related vm access.
*
* @param[in] continuationState the related continuationObject->state
* @return true if it is mounted.
*/
static VMINLINE bool
isFullyMounted(ContinuationState continuationState)
{
bool mounted = J9_ARE_ANY_BITS_SET(continuationState, J9_GC_CONTINUATION_STATE_CARRIERID_MASK);
if (mounted && isPendingToBeMounted(continuationState)) {
mounted = false;
}
return mounted;
}
static VMINLINE J9VMThread *
getCarrierThread(ContinuationState continuationState)
{
return (J9VMThread *)(continuationState & J9_GC_CONTINUATION_STATE_CARRIERID_MASK);
}
static VMINLINE bool
isMountedWithCarrierThread(ContinuationState continuationState, J9VMThread *carrierThread)
{
return carrierThread == getCarrierThread(continuationState);
}
static VMINLINE void
settingCarrierAndPendingState(ContinuationState *continuationStatePtr, J9VMThread *carrierThread)
{
/* also set PendingToBeMounted */
*continuationStatePtr |= (uintptr_t)carrierThread | J9_GC_CONTINUATION_STATE_PENDING_TO_BE_MOUNTED;
}
static VMINLINE void
resetCarrierID(ContinuationState volatile *continuationStatePtr)
{
*continuationStatePtr &= ~J9_GC_CONTINUATION_STATE_CARRIERID_MASK;
}
/**
* Return the thread object whose state is stored in the J9VMContinuation.
*
* @param[in] vmThread the current thread
* @param[in] continuation the native continuation structure
* @param[in] continuationObject the continuation object
* @return the thread object whose state is stored in the continuation
*/
static VMINLINE j9object_t
getThreadObjectForContinuation(J9VMThread *vmThread, J9VMContinuation *continuation, j9object_t continuationObject)
{
/* threadObject points to the virtual thread. */
j9object_t threadObject = (j9object_t)J9VMJDKINTERNALVMCONTINUATION_VTHREAD(vmThread, continuationObject);
ContinuationState volatile *continuationStatePtr = getContinuationStateAddress(vmThread, continuationObject);
ContinuationState continuationState = *continuationStatePtr;
if (isFullyMounted(continuationState)) {
/* If the continuation is fully mounted, then the continuation stores the state of the carrier thread.
* Below, threadObject points to the carrier thread.
*/
threadObject = (j9object_t)J9VMJAVALANGVIRTUALTHREAD_CARRIERTHREAD(vmThread, threadObject);
}
return threadObject;
}
#if JAVA_SPEC_VERSION >= 24
/**
* Check if the threadObject mounted on a J9VMThread is a virtual thread and can be yielded.
*
* @param[in] vmThread the J9VMThread
*
* @return true if the virtual thread is yieldable, otherwise false
*/
static VMINLINE bool
isYieldableVirtualThread(J9VMThread *vmThread)
{
return (J9_ARE_ANY_BITS_SET(vmThread->javaVM->extendedRuntimeFlags3, J9_EXTENDED_RUNTIME3_YIELD_PINNED_CONTINUATION)
&& IS_JAVA_LANG_VIRTUALTHREAD(vmThread, vmThread->threadObject)
&& (0 == vmThread->continuationPinCount)
&& (0 == vmThread->callOutCount));
}
/**
* Remove a continuation from the provided list.
*
* @param[in] list the list from which the continuation should be removed
* @param[in] continuation the continuation to be removed
*
* @return true if the continuation is found and removed from the list, otherwise false
*/
static bool
removeContinuationFromList(J9VMContinuation **list, J9VMContinuation *continuation)
{
bool foundInList = false;
J9VMContinuation *current = *list;
while (NULL != current) {
if (continuation == current) {
foundInList = true;
/* Remove Continuation from the linked list. */
*list = current->nextWaitingContinuation;
current->nextWaitingContinuation = NULL;
break;
}
link = ¤t->nextWaitingContinuation;
current = *link;
}
return foundInList;
}
#endif /* JAVA_SPEC_VERSION >= 24 */
};
#endif /* CONTINUATIONHELPERS_HPP_ */