-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathX-ScopedMemoryAccess.java.template
496 lines (442 loc) · 20.3 KB
/
X-ScopedMemoryAccess.java.template
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.misc;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.foreign.MemorySegment;
import java.lang.ref.Reference;
import java.io.FileDescriptor;
import java.util.function.Supplier;
import jdk.internal.access.JavaNioAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.foreign.AbstractMemorySegmentImpl;
import jdk.internal.foreign.MemorySessionImpl;
import jdk.internal.util.ArraysSupport;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.vector.VectorSupport;
/**
* This class defines low-level methods to access on-heap and off-heap memory. The methods in this class
* can be thought of as thin wrappers around methods provided in the {@link Unsafe} class. All the methods in this
* class accept one or more {@link MemorySessionImpl} parameter, which is used to validate as to whether access to memory
* can be performed in a safe fashion - more specifically, to ensure that the memory being accessed has not
* already been released (which would result in a hard VM crash).
* <p>
* Accessing and releasing memory from a single thread is not problematic - after all, a given thread cannot,
* at the same time, access a memory region <em>and</em> free it. But ensuring correctness of memory access
* when multiple threads are involved is much trickier, as there can be cases where a thread is accessing
* a memory region while another thread is releasing it.
* <p>
* This class provides tools to manage races when multiple threads are accessing and/or releasing the same memory
* session concurrently. More specifically, when a thread wants to release a memory session, it should call the
* {@link ScopedMemoryAccess#closeScope(MemorySessionImpl)} method. This method initiates thread-local handshakes with all the other VM threads,
* which are then stopped one by one. If any thread is found accessing a resource associated to the very memory session
* being closed, the handshake fails, and the session will not be closed.
* <p>
* This synchronization strategy relies on the idea that accessing memory is atomic with respect to checking the
* validity of the session associated with that memory region - that is, a thread that wants to perform memory access will be
* suspended either <em>before</em> a liveness check or <em>after</em> the memory access. To ensure this atomicity,
* all methods in this class are marked with the special {@link Scoped} annotation, which is recognized by the VM,
* and used during the thread-local handshake to detect (and stop) threads performing potentially problematic memory access
* operations. Additionally, to make sure that the session object(s) of the memory being accessed is always
* reachable during an access operation, all the methods in this class add reachability fences around the underlying
* unsafe access.
* <p>
* This form of synchronization allows APIs to use plain memory access without any other form of synchronization
* which might be deemed to expensive; in other words, this approach prioritizes the performance of memory access over
* that of releasing a shared memory resource.
*/
public class ScopedMemoryAccess {
private static final Unsafe UNSAFE = Unsafe.getUnsafe();
private static native void registerNatives();
static {
registerNatives();
}
public void closeScope(MemorySessionImpl session, ScopedAccessError error) {
closeScope0(session, error);
}
native void closeScope0(MemorySessionImpl session, ScopedAccessError error);
private ScopedMemoryAccess() {}
private static final ScopedMemoryAccess theScopedMemoryAccess = new ScopedMemoryAccess();
public static ScopedMemoryAccess getScopedMemoryAccess() {
return theScopedMemoryAccess;
}
public static final class ScopedAccessError extends Error {
@SuppressWarnings("serial")
private final Supplier<RuntimeException> runtimeExceptionSupplier;
public ScopedAccessError(Supplier<RuntimeException> runtimeExceptionSupplier) {
super("Invalid memory access", null, false, false);
this.runtimeExceptionSupplier = runtimeExceptionSupplier;
}
static final long serialVersionUID = 1L;
public final RuntimeException newRuntimeException() {
return runtimeExceptionSupplier.get();
}
}
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
@interface Scoped { }
// bulk ops
@ForceInline
public void copyMemory(MemorySessionImpl srcSession, MemorySessionImpl dstSession,
Object srcBase, long srcOffset,
Object destBase, long destOffset,
long bytes) {
try {
copyMemoryInternal(srcSession, dstSession, srcBase, srcOffset, destBase, destOffset, bytes);
} catch (ScopedAccessError ex) {
throw ex.newRuntimeException();
}
}
@ForceInline @Scoped
private void copyMemoryInternal(MemorySessionImpl srcSession, MemorySessionImpl dstSession,
Object srcBase, long srcOffset,
Object destBase, long destOffset,
long bytes) {
try {
if (srcSession != null) {
srcSession.checkValidStateRaw();
}
if (dstSession != null) {
dstSession.checkValidStateRaw();
}
UNSAFE.copyMemory(srcBase, srcOffset, destBase, destOffset, bytes);
} finally {
Reference.reachabilityFence(srcSession);
Reference.reachabilityFence(dstSession);
}
}
@ForceInline
public void copySwapMemory(MemorySessionImpl srcSession, MemorySessionImpl dstSession,
Object srcBase, long srcOffset,
Object destBase, long destOffset,
long bytes, long elemSize) {
try {
copySwapMemoryInternal(srcSession, dstSession, srcBase, srcOffset, destBase, destOffset, bytes, elemSize);
} catch (ScopedAccessError ex) {
throw ex.newRuntimeException();
}
}
@ForceInline @Scoped
private void copySwapMemoryInternal(MemorySessionImpl srcSession, MemorySessionImpl dstSession,
Object srcBase, long srcOffset,
Object destBase, long destOffset,
long bytes, long elemSize) {
try {
if (srcSession != null) {
srcSession.checkValidStateRaw();
}
if (dstSession != null) {
dstSession.checkValidStateRaw();
}
UNSAFE.copySwapMemory(srcBase, srcOffset, destBase, destOffset, bytes, elemSize);
} finally {
Reference.reachabilityFence(srcSession);
Reference.reachabilityFence(dstSession);
}
}
@ForceInline
public void setMemory(MemorySessionImpl session, Object o, long offset, long bytes, byte value) {
try {
setMemoryInternal(session, o, offset, bytes, value);
} catch (ScopedAccessError ex) {
throw ex.newRuntimeException();
}
}
@ForceInline @Scoped
private void setMemoryInternal(MemorySessionImpl session, Object o, long offset, long bytes, byte value) {
try {
if (session != null) {
session.checkValidStateRaw();
}
UNSAFE.setMemory(o, offset, bytes, value);
} finally {
Reference.reachabilityFence(session);
}
}
@ForceInline
public int vectorizedMismatch(MemorySessionImpl aSession, MemorySessionImpl bSession,
Object a, long aOffset,
Object b, long bOffset,
int length,
int log2ArrayIndexScale) {
try {
return vectorizedMismatchInternal(aSession, bSession, a, aOffset, b, bOffset, length, log2ArrayIndexScale);
} catch (ScopedAccessError ex) {
throw ex.newRuntimeException();
}
}
@ForceInline @Scoped
private int vectorizedMismatchInternal(MemorySessionImpl aSession, MemorySessionImpl bSession,
Object a, long aOffset,
Object b, long bOffset,
int length,
int log2ArrayIndexScale) {
try {
if (aSession != null) {
aSession.checkValidStateRaw();
}
if (bSession != null) {
bSession.checkValidStateRaw();
}
return ArraysSupport.vectorizedMismatch(a, aOffset, b, bOffset, length, log2ArrayIndexScale);
} finally {
Reference.reachabilityFence(aSession);
Reference.reachabilityFence(bSession);
}
}
@ForceInline
public boolean isLoaded(MemorySessionImpl session, long address, boolean isSync, long size) {
try {
return isLoadedInternal(session, address, isSync, size);
} catch (ScopedAccessError ex) {
throw ex.newRuntimeException();
}
}
@ForceInline @Scoped
public boolean isLoadedInternal(MemorySessionImpl session, long address, boolean isSync, long size) {
try {
if (session != null) {
session.checkValidStateRaw();
}
return SharedSecrets.getJavaNioAccess().isLoaded(address, isSync, size);
} finally {
Reference.reachabilityFence(session);
}
}
@ForceInline
public void load(MemorySessionImpl session, long address, boolean isSync, long size) {
try {
loadInternal(session, address, isSync, size);
} catch (ScopedAccessError ex) {
throw ex.newRuntimeException();
}
}
@ForceInline @Scoped
public void loadInternal(MemorySessionImpl session, long address, boolean isSync, long size) {
try {
if (session != null) {
session.checkValidStateRaw();
}
SharedSecrets.getJavaNioAccess().load(address, isSync, size);
} finally {
Reference.reachabilityFence(session);
}
}
@ForceInline
public void unload(MemorySessionImpl session, long address, boolean isSync, long size) {
try {
unloadInternal(session, address, isSync, size);
} catch (ScopedAccessError ex) {
throw ex.newRuntimeException();
}
}
@ForceInline @Scoped
public void unloadInternal(MemorySessionImpl session, long address, boolean isSync, long size) {
try {
if (session != null) {
session.checkValidStateRaw();
}
SharedSecrets.getJavaNioAccess().unload(address, isSync, size);
} finally {
Reference.reachabilityFence(session);
}
}
@ForceInline
public void force(MemorySessionImpl session, FileDescriptor fd, long address, boolean isSync, long index, long length) {
try {
forceInternal(session, fd, address, isSync, index, length);
} catch (ScopedAccessError ex) {
throw ex.newRuntimeException();
}
}
@ForceInline @Scoped
public void forceInternal(MemorySessionImpl session, FileDescriptor fd, long address, boolean isSync, long index, long length) {
try {
if (session != null) {
session.checkValidStateRaw();
}
SharedSecrets.getJavaNioAccess().force(fd, address, isSync, index, length);
} finally {
Reference.reachabilityFence(session);
}
}
// MemorySegment vector access ops
@ForceInline
public static
<V extends VectorSupport.Vector<E>, E, S extends VectorSupport.VectorSpecies<E>>
V loadFromMemorySegment(Class<? extends V> vmClass, Class<E> e, int length,
AbstractMemorySegmentImpl msp, long offset,
S s,
VectorSupport.LoadOperation<AbstractMemorySegmentImpl, V, S> defaultImpl) {
try {
return loadFromMemorySegmentScopedInternal(
msp.sessionImpl(),
vmClass, e, length,
msp, offset,
s,
defaultImpl);
} catch (ScopedAccessError ex) {
throw ex.newRuntimeException();
}
}
@Scoped
@ForceInline
private static
<V extends VectorSupport.Vector<E>, E, S extends VectorSupport.VectorSpecies<E>>
V loadFromMemorySegmentScopedInternal(MemorySessionImpl session,
Class<? extends V> vmClass, Class<E> e, int length,
AbstractMemorySegmentImpl msp, long offset,
S s,
VectorSupport.LoadOperation<AbstractMemorySegmentImpl, V, S> defaultImpl) {
try {
session.checkValidStateRaw();
return VectorSupport.load(vmClass, e, length,
msp.unsafeGetBase(), msp.unsafeGetOffset() + offset, true,
msp, offset, s,
defaultImpl);
} finally {
Reference.reachabilityFence(session);
}
}
@ForceInline
public static
<V extends VectorSupport.Vector<E>, E, S extends VectorSupport.VectorSpecies<E>,
M extends VectorSupport.VectorMask<E>>
V loadFromMemorySegmentMasked(Class<? extends V> vmClass, Class<M> maskClass, Class<E> e,
int length, AbstractMemorySegmentImpl msp, long offset, M m, S s, int offsetInRange,
VectorSupport.LoadVectorMaskedOperation<AbstractMemorySegmentImpl, V, S, M> defaultImpl) {
try {
return loadFromMemorySegmentMaskedScopedInternal(
msp.sessionImpl(),
vmClass, maskClass, e, length,
msp, offset, m,
s, offsetInRange,
defaultImpl);
} catch (ScopedAccessError ex) {
throw ex.newRuntimeException();
}
}
@Scoped
@ForceInline
private static
<V extends VectorSupport.Vector<E>, E, S extends VectorSupport.VectorSpecies<E>,
M extends VectorSupport.VectorMask<E>>
V loadFromMemorySegmentMaskedScopedInternal(MemorySessionImpl session, Class<? extends V> vmClass,
Class<M> maskClass, Class<E> e, int length,
AbstractMemorySegmentImpl msp, long offset, M m,
S s, int offsetInRange,
VectorSupport.LoadVectorMaskedOperation<AbstractMemorySegmentImpl, V, S, M> defaultImpl) {
try {
session.checkValidStateRaw();
return VectorSupport.loadMasked(vmClass, maskClass, e, length,
msp.unsafeGetBase(), msp.unsafeGetOffset() + offset, true, m, offsetInRange,
msp, offset, s,
defaultImpl);
} finally {
Reference.reachabilityFence(session);
}
}
@ForceInline
public static
<V extends VectorSupport.Vector<E>, E>
void storeIntoMemorySegment(Class<? extends V> vmClass, Class<E> e, int length,
V v,
AbstractMemorySegmentImpl msp, long offset,
VectorSupport.StoreVectorOperation<AbstractMemorySegmentImpl, V> defaultImpl) {
try {
storeIntoMemorySegmentScopedInternal(
msp.sessionImpl(),
vmClass, e, length,
v,
msp, offset,
defaultImpl);
} catch (ScopedAccessError ex) {
throw ex.newRuntimeException();
}
}
@Scoped
@ForceInline
private static
<V extends VectorSupport.Vector<E>, E>
void storeIntoMemorySegmentScopedInternal(MemorySessionImpl session,
Class<? extends V> vmClass, Class<E> e, int length,
V v,
AbstractMemorySegmentImpl msp, long offset,
VectorSupport.StoreVectorOperation<AbstractMemorySegmentImpl, V> defaultImpl) {
try {
session.checkValidStateRaw();
VectorSupport.store(vmClass, e, length,
msp.unsafeGetBase(), msp.unsafeGetOffset() + offset, true,
v,
msp, offset,
defaultImpl);
} finally {
Reference.reachabilityFence(session);
}
}
@ForceInline
public static
<V extends VectorSupport.Vector<E>, E, M extends VectorSupport.VectorMask<E>>
void storeIntoMemorySegmentMasked(Class<? extends V> vmClass, Class<M> maskClass, Class<E> e,
int length, V v, M m,
AbstractMemorySegmentImpl msp, long offset,
VectorSupport.StoreVectorMaskedOperation<AbstractMemorySegmentImpl, V, M> defaultImpl) {
try {
storeIntoMemorySegmentMaskedScopedInternal(
msp.sessionImpl(),
vmClass, maskClass, e, length,
v, m,
msp, offset,
defaultImpl);
} catch (ScopedAccessError ex) {
throw ex.newRuntimeException();
}
}
@Scoped
@ForceInline
private static
<V extends VectorSupport.Vector<E>, E, M extends VectorSupport.VectorMask<E>>
void storeIntoMemorySegmentMaskedScopedInternal(MemorySessionImpl session,
Class<? extends V> vmClass, Class<M> maskClass,
Class<E> e, int length, V v, M m,
AbstractMemorySegmentImpl msp, long offset,
VectorSupport.StoreVectorMaskedOperation<AbstractMemorySegmentImpl, V, M> defaultImpl) {
try {
session.checkValidStateRaw();
VectorSupport.storeMasked(vmClass, maskClass, e, length,
msp.unsafeGetBase(), msp.unsafeGetOffset() + offset, true,
v, m,
msp, offset,
defaultImpl);
} finally {
Reference.reachabilityFence(session);
}
}
// typed-ops here
// Note: all the accessor methods defined below take advantage of argument type profiling
// (see src/hotspot/share/oops/methodData.cpp) which greatly enhances performance when the same accessor
// method is used repeatedly with different 'base' objects.