Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/AtomicReferenceGetAndSetTest.java @ 12742:40924dbc623b
HSAIL support for compare-and-swap and volatile load/store operations
Contributed-by: Tom Deneau <tom.deneau@amd.com>
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Sun, 10 Nov 2013 11:42:31 +0100 |
parents | |
children | de1d50c121cd |
comparison
equal
deleted
inserted
replaced
12741:a5b5e1ebab81 | 12742:40924dbc623b |
---|---|
1 /* | |
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.graal.compiler.hsail.test; | |
24 | |
25 import java.util.concurrent.atomic.*; | |
26 | |
27 import org.junit.*; | |
28 | |
29 import sun.misc.*; | |
30 | |
31 import com.oracle.graal.compiler.hsail.test.infra.*; | |
32 import com.oracle.graal.debug.*; | |
33 | |
34 /** | |
35 * Tests {@link AtomicReference#getAndSet(Object)} which indirectly tests | |
36 * {@link Unsafe#compareAndSwapObject(Object, long, Object, Object)}. The latter requires special | |
37 * handling if compressed oops are enabled. | |
38 */ | |
39 public class AtomicReferenceGetAndSetTest extends GraalKernelTester { | |
40 | |
41 static final int NUM = 20; | |
42 @Result public int[] followedCount = new int[NUM]; | |
43 public MyObj[] inArray = new MyObj[NUM]; | |
44 AtomicReference<MyObj> atomicRef = new AtomicReference<>(); | |
45 | |
46 public static class MyObj { | |
47 public int val; | |
48 public boolean[] followedBy = new boolean[NUM + 1]; | |
49 | |
50 MyObj(int n) { | |
51 val = n; | |
52 } | |
53 } | |
54 | |
55 void setupArrays() { | |
56 for (int i = 0; i < NUM; i++) { | |
57 inArray[i] = new MyObj(i + 1); | |
58 } | |
59 atomicRef.set(new MyObj(0)); // initial value | |
60 } | |
61 | |
62 private static final boolean DEBUG = false; | |
63 | |
64 @Override | |
65 public void runTest() { | |
66 setupArrays(); | |
67 | |
68 dispatchMethodKernel(NUM); | |
69 | |
70 // make a fake followedBy for the final object | |
71 MyObj finalObj = atomicRef.get(); | |
72 finalObj.followedBy[0] = true; | |
73 | |
74 // When the kernel is done, compute the number of true bits in each followedBy array; | |
75 for (int i = 0; i < NUM; i++) { | |
76 MyObj obj = inArray[i]; | |
77 int count = 0; | |
78 for (int j = 0; j < NUM + 1; j++) { | |
79 boolean b = obj.followedBy[j]; | |
80 if (b) { | |
81 count++; | |
82 if (DEBUG) { | |
83 TTY.println("obj " + obj.val + " was followed by " + j); | |
84 } | |
85 } | |
86 | |
87 } | |
88 followedCount[i] = count; | |
89 } | |
90 } | |
91 | |
92 public void run(int gid) { | |
93 MyObj newObj = inArray[gid]; | |
94 MyObj oldObj = atomicRef.getAndSet(newObj); | |
95 oldObj.followedBy[newObj.val] = true; | |
96 } | |
97 | |
98 @Test | |
99 public void test() { | |
100 testGeneratedHsail(); | |
101 } | |
102 } |