1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 import java.lang.invoke.VarHandle;
18 
19 /**
20  * Checker test on the 1.8 unsafe operations. Note, this is by no means an
21  * exhaustive unit test for these CAS (compare-and-swap) and fence operations.
22  * Instead, this test ensures the methods are recognized as intrinsic and behave
23  * as expected.
24  */
25 public class TestVarHandles {
26 
27   //
28   // Fences (native).
29   //
30 
31   /// CHECK-START: void TestVarHandles.fullFence() builder (after)
32   /// CHECK-NOT: InvokeStaticOrDirect
33   //
34   /// CHECK-START: void TestVarHandles.fullFence() builder (after)
35   /// CHECK-DAG: MemoryBarrier kind:AnyAny
fullFence()36   private static void fullFence() {
37       VarHandle.fullFence();
38   }
39 
40   /// CHECK-START: void TestVarHandles.acquireFence() builder (after)
41   /// CHECK-NOT: InvokeStaticOrDirect
42   //
43   /// CHECK-START: void TestVarHandles.acquireFence() builder (after)
44   /// CHECK-DAG: MemoryBarrier kind:LoadAny
acquireFence()45   private static void acquireFence() {
46       VarHandle.acquireFence();
47   }
48 
49   /// CHECK-START: void TestVarHandles.releaseFence() builder (after)
50   /// CHECK-NOT: InvokeStaticOrDirect
51   //
52   /// CHECK-START: void TestVarHandles.releaseFence() builder (after)
53   /// CHECK-DAG: MemoryBarrier kind:AnyStore
releaseFence()54   private static void releaseFence() {
55       VarHandle.releaseFence();
56   }
57 
58   /// CHECK-START: void TestVarHandles.loadLoadFence() builder (after)
59   /// CHECK-NOT: InvokeStaticOrDirect
60   //
61   /// CHECK-START: void TestVarHandles.loadLoadFence() builder (after)
62   /// CHECK-DAG: MemoryBarrier kind:LoadAny
loadLoadFence()63   private static void loadLoadFence() {
64       VarHandle.loadLoadFence();
65   }
66 
67   /// CHECK-START: void TestVarHandles.storeStoreFence() builder (after)
68   /// CHECK-NOT: InvokeStaticOrDirect
69   //
70   /// CHECK-START: void TestVarHandles.storeStoreFence() builder (after)
71   /// CHECK-DAG: MemoryBarrier kind:StoreStore
storeStoreFence()72   private static void storeStoreFence() {
73       VarHandle.storeStoreFence();
74   }
75 
76   //
77   // Driver.
78   //
79 
main()80   public static void main() {
81     acquireFence();
82     releaseFence();
83     loadLoadFence();
84     storeStoreFence();
85     fullFence();
86     System.out.println("TestVarHandles passed");
87   }
88 }
89