Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/gc_glue_java/JNICriticalRegion.hpp
5985 views
1
/*******************************************************************************
2
* Copyright (c) 1991, 2019 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
23
24
/**
25
* @file
26
* @ingroup GC_Base
27
*/
28
29
#if !defined(JNICRITICALREGION_HPP_)
30
#define JNICRITICALREGION_HPP_
31
32
#include "modron.h"
33
#include "ModronAssertions.h"
34
35
#include "AtomicSupport.hpp"
36
#include "BaseNonVirtual.hpp"
37
#include "VMAccess.hpp"
38
39
class MM_JNICriticalRegion : public MM_BaseNonVirtual
40
{
41
/* data members */
42
private:
43
protected:
44
public:
45
46
/* member function */
47
private:
48
protected:
49
public:
50
static void reacquireAccess(J9VMThread* vmThread, UDATA accessMask);
51
static void releaseAccess(J9VMThread* vmThread, UDATA* accessMask);
52
53
/**
54
* Enter a JNI critical region (i.e. GetPrimitiveArrayCritical or GetStringCritical).
55
* Once a thread has successfully entered a critical region, it has privileges similar
56
* to holding VM access. No object can move while any thread is in a critical region.
57
*
58
* @param vmThread the J9VMThread requesting to enter a critical region
59
* @param hasVMAccess true if caller has acquired VM access, false if not
60
*/
61
static MMINLINE void
62
enterCriticalRegion(J9VMThread* vmThread, bool hasVMAccess)
63
{
64
if (J9_ARE_ANY_BITS_SET(vmThread->publicFlags, J9_PUBLIC_FLAGS_DEBUG_VM_ACCESS)) {
65
Assert_MM_true(J9_VM_FUNCTION(vmThread, currentVMThread)(vmThread->javaVM) == vmThread);
66
}
67
68
/* Handle nested case first to avoid the unnecessary atomic */
69
if (J9_ARE_ANY_BITS_SET(vmThread->publicFlags, J9_PUBLIC_FLAGS_JNI_CRITICAL_REGION)) {
70
/* Nested critical region; increment the count */
71
vmThread->jniCriticalDirectCount += 1;
72
} else {
73
UDATA const criticalFlags = J9_PUBLIC_FLAGS_JNI_CRITICAL_REGION | J9_PUBLIC_FLAGS_JNI_CRITICAL_ACCESS;
74
#if defined(J9VM_INTERP_ATOMIC_FREE_JNI)
75
UDATA const expectedFlags = J9_PUBLIC_FLAGS_VM_ACCESS;
76
#else /* J9VM_INTERP_ATOMIC_FREE_JNI */
77
UDATA const expectedFlags = hasVMAccess ? J9_PUBLIC_FLAGS_VM_ACCESS : 0;
78
#endif /* J9VM_INTERP_ATOMIC_FREE_JNI */
79
/* Expected case: swap in JNI access bits */
80
if (expectedFlags == VM_AtomicSupport::lockCompareExchange(&vmThread->publicFlags, expectedFlags, expectedFlags | criticalFlags)) {
81
/* First entry into a critical region */
82
vmThread->jniCriticalDirectCount = 1;
83
} else {
84
omrthread_t const osThread = vmThread->osThread;
85
omrthread_monitor_t const publicFlagsMutex = vmThread->publicFlagsMutex;
86
omrthread_monitor_enter_using_threadId(publicFlagsMutex, osThread);
87
if (hasVMAccess) {
88
/* Entering the first critical region with VM access; set the critical flags */
89
VM_VMAccess::setPublicFlags(vmThread, criticalFlags);
90
vmThread->jniCriticalDirectCount = 1;
91
92
/* The current thread has VM access and just acquired JNI critical access.
93
* If an exclusive request is in progress and the current thread has already
94
* been requested to halt, then adjust the JNI response count accordingly.
95
*/
96
if (J9_ARE_ANY_BITS_SET(vmThread->publicFlags, J9_PUBLIC_FLAGS_HALT_THREAD_EXCLUSIVE)) {
97
J9JavaVM* const vm = vmThread->javaVM;
98
omrthread_monitor_t const exclusiveAccessMutex = vm->exclusiveAccessMutex;
99
omrthread_monitor_enter_using_threadId(exclusiveAccessMutex, osThread);
100
vm->jniCriticalResponseCount += 1;
101
omrthread_monitor_exit_using_threadId(exclusiveAccessMutex, osThread);
102
}
103
} else {
104
/* Entering the first critical region; acquire VM access and set the critical flags */
105
if (0 == VM_AtomicSupport::lockCompareExchange(&vmThread->publicFlags, 0, criticalFlags)) {
106
/* Set the count to 1 */
107
vmThread->jniCriticalDirectCount = 1;
108
} else {
109
J9_VM_FUNCTION(vmThread, internalEnterVMFromJNI)(vmThread);
110
VM_VMAccess::setPublicFlags(vmThread, J9_PUBLIC_FLAGS_JNI_CRITICAL_REGION | J9_PUBLIC_FLAGS_JNI_CRITICAL_ACCESS);
111
vmThread->jniCriticalDirectCount = 1;
112
J9_VM_FUNCTION(vmThread, internalExitVMToJNI)(vmThread);
113
}
114
}
115
omrthread_monitor_exit_using_threadId(publicFlagsMutex, osThread);
116
}
117
}
118
}
119
120
/**
121
* Exit a JNI critical region (i.e. ReleasePrimitiveArrayCritical or ReleaseStringCritical).
122
* Once a thread has successfully exited a critical region, objects in the java
123
* heap are allowed to move again.
124
*
125
* @param vmThread the J9VMThread requesting to exit a critical region
126
* @param hasVMAccess true if caller has acquired VM access, false if not
127
*/
128
static MMINLINE void
129
exitCriticalRegion(J9VMThread* vmThread, bool hasVMAccess)
130
{
131
if (J9_ARE_ANY_BITS_SET(vmThread->publicFlags, J9_PUBLIC_FLAGS_DEBUG_VM_ACCESS)) {
132
Assert_MM_true(J9_VM_FUNCTION(vmThread, currentVMThread)(vmThread->javaVM) == vmThread);
133
}
134
135
Assert_MM_mustHaveJNICriticalRegion(vmThread);
136
if (--vmThread->jniCriticalDirectCount == 0) {
137
/* Exiting last critical region, swap out critical flags */
138
UDATA const criticalFlags = J9_PUBLIC_FLAGS_JNI_CRITICAL_REGION | J9_PUBLIC_FLAGS_JNI_CRITICAL_ACCESS;
139
#if defined(J9VM_INTERP_ATOMIC_FREE_JNI)
140
UDATA const finalFlags = J9_PUBLIC_FLAGS_VM_ACCESS;
141
#else /* J9VM_INTERP_ATOMIC_FREE_JNI */
142
UDATA const finalFlags = hasVMAccess ? J9_PUBLIC_FLAGS_VM_ACCESS : 0;
143
#endif /* J9VM_INTERP_ATOMIC_FREE_JNI */
144
UDATA const jniAccess = criticalFlags | finalFlags;
145
if (jniAccess != VM_AtomicSupport::lockCompareExchange(&vmThread->publicFlags, jniAccess, finalFlags)) {
146
/* Exiting the last critical region; clear the critical flags.
147
* Cache a copy of the flags first to determine if we must respond to an exclusive access request.
148
*/
149
omrthread_t const osThread = vmThread->osThread;
150
omrthread_monitor_t const publicFlagsMutex = vmThread->publicFlagsMutex;
151
omrthread_monitor_enter_using_threadId(publicFlagsMutex, osThread);
152
UDATA const publicFlags = VM_VMAccess::clearPublicFlagsNoMutex(vmThread, criticalFlags);
153
if (J9_ARE_ALL_BITS_SET(publicFlags, J9_PUBLIC_FLAGS_JNI_CRITICAL_ACCESS | J9_PUBLIC_FLAGS_HALT_THREAD_EXCLUSIVE)) {
154
/* If an exclusive request is pending, then respond. */
155
J9JavaVM* const vm = vmThread->javaVM;
156
omrthread_monitor_t const exclusiveAccessMutex = vm->exclusiveAccessMutex;
157
omrthread_monitor_enter_using_threadId(exclusiveAccessMutex, osThread);
158
PORT_ACCESS_FROM_JAVAVM(vm);
159
U_64 const timeNow = VM_VMAccess::updateExclusiveVMAccessStats(vmThread, vm, PORTLIB);
160
if (--vm->jniCriticalResponseCount == 0) {
161
VM_VMAccess::respondToExclusiveRequest(vmThread, vm, PORTLIB, timeNow, J9_EXCLUSIVE_SLOW_REASON_JNICRITICAL);
162
}
163
omrthread_monitor_exit_using_threadId(exclusiveAccessMutex, osThread);
164
}
165
omrthread_monitor_exit_using_threadId(publicFlagsMutex, osThread);
166
}
167
}
168
}
169
};
170
171
#endif /* JNICRITICALREGION_HPP_ */
172
173