Skip to content

Commit e663206

Browse files
committed
8157726: VarHandles/Unsafe should support sub-word atomic ops
Reviewed-by: psandoz, vlivanov, lagergren
1 parent de17002 commit e663206

25 files changed

+10332
-3210
lines changed

jdk/make/gensrc/GensrcVarHandles.gmk

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,18 @@ define GenerateVarHandle
3838

3939
$1_FILENAME := $(VARHANDLES_GENSRC_DIR)/VarHandle$$($1_Type)s.java
4040

41-
ifneq ($$(findstring $$($1_Type), Object Int Long), )
41+
ifneq ($$(findstring $$($1_Type), Object Boolean Byte Short Char Int Long), )
4242
$1_ARGS += -KCAS
4343
endif
4444

45-
ifneq ($$(findstring $$($1_Type), Int Long), )
45+
ifneq ($$(findstring $$($1_Type), Byte Short Char Int Long), )
4646
$1_ARGS += -KAtomicAdd
4747
endif
4848

49+
ifneq ($$(findstring $$($1_Type), Byte Short Char), )
50+
$1_ARGS += -KShorterThanInt
51+
endif
52+
4953
$$($1_FILENAME): $(VARHANDLES_SRC_DIR)/X-VarHandle.java.template $(BUILD_TOOLS_JDK)
5054
ifeq ($$($1_Type), Object)
5155
$$(eval $1_type := $$($1_Type))

jdk/src/java.base/share/classes/java/lang/invoke/MethodHandles.java

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,16 +1208,15 @@ public MethodHandle findSetter(Class<?> refc, String name, Class<?> type) throws
12081208
* <ul>
12091209
* <li>if the field is declared {@code final}, then the write, atomic
12101210
* update, and numeric atomic update access modes are unsupported.
1211-
* <li>if the field type is anything other than {@code int},
1211+
* <li>if the field type is anything other than {@code boolean},
1212+
* {@code byte}, {@code short}, {@code char}, {@code int},
12121213
* {@code long} or a reference type, then atomic update access modes
12131214
* are unsupported. (Future major platform releases of the JDK may
12141215
* support additional types for certain currently unsupported access
12151216
* modes.)
1216-
* <li>if the field type is anything other than {@code int} or
1217-
* {@code long}, then numeric atomic update access modes are
1218-
* unsupported. (Future major platform releases of the JDK may
1219-
* support additional numeric types for certain currently
1220-
* unsupported access modes.)
1217+
* <li>if the field type is anything other than {@code byte},
1218+
* {@code short}, {@code char}, {@code int} or {@code long}, then
1219+
* numeric atomic update access modes are unsupported.
12211220
* </ul>
12221221
* <p>
12231222
* If the field is declared {@code volatile} then the returned VarHandle
@@ -1306,16 +1305,15 @@ public MethodHandle findStaticSetter(Class<?> refc, String name, Class<?> type)
13061305
* <ul>
13071306
* <li>if the field is declared {@code final}, then the write, atomic
13081307
* update, and numeric atomic update access modes are unsupported.
1309-
* <li>if the field type is anything other than {@code int},
1308+
* <li>if the field type is anything other than {@code boolean},
1309+
* {@code byte}, {@code short}, {@code char}, {@code int},
13101310
* {@code long} or a reference type, then atomic update access modes
13111311
* are unsupported. (Future major platform releases of the JDK may
13121312
* support additional types for certain currently unsupported access
13131313
* modes.)
1314-
* <li>if the field type is anything other than {@code int} or
1315-
* {@code long}, then numeric atomic update access modes are
1316-
* unsupported. (Future major platform releases of the JDK may
1317-
* support additional numeric types for certain currently
1318-
* unsupported access modes.)
1314+
* <li>if the field type is anything other than {@code byte},
1315+
* {@code short}, {@code char}, {@code int} or {@code long}, then
1316+
* numeric atomic update access modes are unsupported.
13191317
* </ul>
13201318
* <p>
13211319
* If the field is declared {@code volatile} then the returned VarHandle
@@ -1590,16 +1588,15 @@ public MethodHandle unreflectSetter(Field f) throws IllegalAccessException {
15901588
* <ul>
15911589
* <li>if the field is declared {@code final}, then the write, atomic
15921590
* update, and numeric atomic update access modes are unsupported.
1593-
* <li>if the field type is anything other than {@code int},
1591+
* <li>if the field type is anything other than {@code boolean},
1592+
* {@code byte}, {@code short}, {@code char}, {@code int},
15941593
* {@code long} or a reference type, then atomic update access modes
15951594
* are unsupported. (Future major platform releases of the JDK may
15961595
* support additional types for certain currently unsupported access
15971596
* modes.)
1598-
* <li>if the field type is anything other than {@code int} or
1599-
* {@code long}, then numeric atomic update access modes are
1600-
* unsupported. (Future major platform releases of the JDK may
1601-
* support additional numeric types for certain currently
1602-
* unsupported access modes.)
1597+
* <li>if the field type is anything other than {@code byte},
1598+
* {@code short}, {@code char}, {@code int} or {@code long}, then
1599+
* numeric atomic update access modes are unsupported.
16031600
* </ul>
16041601
* <p>
16051602
* If the field is declared {@code volatile} then the returned VarHandle
@@ -2297,16 +2294,15 @@ MethodHandle arrayElementSetter(Class<?> arrayClass) throws IllegalArgumentExcep
22972294
* Certain access modes of the returned VarHandle are unsupported under
22982295
* the following conditions:
22992296
* <ul>
2300-
* <li>if the component type is anything other than {@code int},
2297+
* <li>if the field type is anything other than {@code boolean},
2298+
* {@code byte}, {@code short}, {@code char}, {@code int},
23012299
* {@code long} or a reference type, then atomic update access modes
23022300
* are unsupported. (Future major platform releases of the JDK may
23032301
* support additional types for certain currently unsupported access
23042302
* modes.)
2305-
* <li>if the component type is anything other than {@code int} or
2306-
* {@code long}, then numeric atomic update access modes are
2307-
* unsupported. (Future major platform releases of the JDK may
2308-
* support additional numeric types for certain currently
2309-
* unsupported access modes.)
2303+
* <li>if the component type is anything other than {@code byte},
2304+
* {@code short}, {@code char}, {@code int} or {@code long}, then
2305+
* numeric atomic update access modes are unsupported.
23102306
* </ul>
23112307
* @param arrayClass the class of an array, of type {@code T[]}
23122308
* @return a VarHandle giving access to elements of an array

jdk/src/java.base/share/classes/java/lang/invoke/X-VarHandle.java.template

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ final class VarHandle$Type$s {
205205

206206
@ForceInline
207207
static $type$ addAndGet(FieldInstanceReadWrite handle, Object holder, $type$ value) {
208-
return UNSAFE.getAndAdd$Type$(Objects.requireNonNull(handle.receiverType.cast(holder)),
208+
return {#if[ShorterThanInt]?($type$)}(UNSAFE.getAndAdd$Type$(Objects.requireNonNull(handle.receiverType.cast(holder)),
209209
handle.fieldOffset,
210-
value) + value;
210+
value) + value);
211211
}
212212
#end[AtomicAdd]
213213

@@ -386,9 +386,9 @@ final class VarHandle$Type$s {
386386

387387
@ForceInline
388388
static $type$ addAndGet(FieldStaticReadWrite handle, $type$ value) {
389-
return UNSAFE.getAndAdd$Type$(handle.base,
389+
return {#if[ShorterThanInt]?($type$)}(UNSAFE.getAndAdd$Type$(handle.base,
390390
handle.fieldOffset,
391-
value) + value;
391+
value) + value);
392392
}
393393
#end[AtomicAdd]
394394

@@ -646,9 +646,9 @@ final class VarHandle$Type$s {
646646
#else[Object]
647647
$type$[] array = ($type$[]) oarray;
648648
#end[Object]
649-
return UNSAFE.getAndAdd$Type$(array,
649+
return {#if[ShorterThanInt]?($type$)}(UNSAFE.getAndAdd$Type$(array,
650650
(((long) Preconditions.checkIndex(index, array.length, AIOOBE_SUPPLIER)) << handle.ashift) + handle.abase,
651-
value) + value;
651+
value) + value);
652652
}
653653
#end[AtomicAdd]
654654

0 commit comments

Comments
 (0)