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 } | 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 } |