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 for (int i = 0; i < MemoryConflictProvoker.INNER_ITERATIONS; i++) { 73 synchronized(monitor) { 74 MemoryConflictProvoker.field--; 75 } 76 } 77 } 78 79 @Override 80 public void forceAbort() { 81 try { 82 Thread t = new Thread(conflictingThread); 83 t.start(); 84 try { 85 barrier.await(); 86 } catch (InterruptedException | BrokenBarrierException e) { 87 throw new RuntimeException(e); 88 } 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 }