comparison test/compiler/testlibrary/rtm/MemoryConflictProvoker.java @ 17871:665bbe93823f

8039499: Add all common classes used by tests on RTM support to testlibrary Reviewed-by: kvn, iignatyev Contributed-by: filipp.zhinkin@oracle.com
author iignatyev
date Mon, 14 Apr 2014 19:29:34 +0400
parents
children 9041e030d11f
comparison
equal deleted inserted replaced
17870:a0eb3f61d34a 17871:665bbe93823f
1 /*
2 * Copyright (c) 2014, 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 */
24
25 package rtm;
26
27 import java.util.concurrent.BrokenBarrierException;
28 import java.util.concurrent.CyclicBarrier;
29
30 /**
31 * To force transactional execution abort due to memory conflict
32 * one thread should access memory region from transactional region
33 * while another thread should modify the same memory region.
34 * Since this scenario is based on the race condition between threads
35 * you should not expect some particular amount of aborts.
36 */
37 class MemoryConflictProvoker extends AbortProvoker {
38 // Following field have to be static in order to avoid escape analysis.
39 @SuppressWarnings("UnsuedDeclaration")
40 private static int field = 0;
41 private static final int INNER_ITERATIONS = 10000;
42 private final CyclicBarrier barrier;
43 /**
44 * This thread will access and modify memory region
45 * from outside of the transaction.
46 */
47 private final Runnable conflictingThread;
48
49 public MemoryConflictProvoker() {
50 this(new Object());
51 }
52
53 public MemoryConflictProvoker(Object monitor) {
54 super(monitor);
55 barrier = new CyclicBarrier(2);
56 conflictingThread = () -> {
57 try {
58 barrier.await();
59 } catch (Exception e) {
60 throw new RuntimeException(e);
61 }
62 for (int i = 0; i < MemoryConflictProvoker.INNER_ITERATIONS; i++) {
63 MemoryConflictProvoker.field++;
64 }
65 };
66 }
67
68 /**
69 * Accesses and modifies memory region from within the transaction.
70 */
71 public void transactionalRegion() {
72 try {
73 barrier.await();
74 } catch (InterruptedException | BrokenBarrierException e) {
75 throw new RuntimeException(e);
76 }
77 for (int i = 0; i < MemoryConflictProvoker.INNER_ITERATIONS; i++) {
78 synchronized(monitor) {
79 MemoryConflictProvoker.field--;
80 }
81 }
82 }
83
84 @Override
85 public void forceAbort() {
86 try {
87 Thread t = new Thread(conflictingThread);
88 t.start();
89 transactionalRegion();
90 t.join();
91 } catch (Exception e) {
92 throw new RuntimeException(e);
93 }
94 }
95
96 @Override
97 public String getMethodWithLockName() {
98 return this.getClass().getName() + "::transactionalRegion";
99 }
100 }