4 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa
5 * Clara, California 95054, U.S.A. All rights reserved.
7 * Sun Microsystems, Inc. has intellectual property rights relating to
8 * technology embodied in the product that is described in this
9 * document. In particular, and without limitation, these
10 * intellectual property rights may include one or more of the
11 * U.S. patents listed at http://www.sun.com/patents and one or more
12 * additional patents or pending patent applications in the U.S. and
15 * U.S. Government Rights - Commercial software.
16 * Government users are subject to the Sun Microsystems, Inc. standard
17 * license agreement and applicable provisions of the FAR and its
18 * supplements. Use is subject to license terms. Sun, Sun
19 * Microsystems, the Sun logo and Java are trademarks or registered
20 * trademarks of Sun Microsystems, Inc. in the U.S. and other
23 * This product is covered and controlled by U.S. Export Control laws
24 * and may be subject to the export or import laws in other countries.
25 * Nuclear, missile, chemical biological weapons or nuclear maritime
26 * end uses or end users, whether direct or indirect, are strictly
27 * prohibited. Export or reexport to countries subject to
28 * U.S. embargo or to entities identified on U.S. export exclusion
29 * lists, including, but not limited to, the denied persons and
30 * specially designated nationals lists is strictly prohibited.
35 import TransactionalIO.exceptions.PanicException;
36 import TransactionalIO.benchmarks.benchmark;
37 import TransactionalIO.interfaces.TransactionStatu;
38 import java.util.concurrent.atomic.AtomicReference;
39 import java.util.concurrent.atomic.AtomicInteger;
40 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
44 * Keeps a transaction's status and contention manager.
47 public class Transaction implements TransactionStatu{
49 private TransactionStatu othersystem;
50 private boolean flag = true;
52 * Possible transaction status
54 public enum Status {ABORTED, ACTIVE, COMMITTED};
57 * Predefined committed transaction
59 public static Transaction COMMITTED = new Transaction(Status.COMMITTED);
61 * Predefined orted transaction
63 public static Transaction ABORTED = new Transaction(Status.ABORTED);
66 * Is transaction waiting for another?
68 public boolean waiting = false;
71 * Number of times this transaction tried
73 public int attempts = 0;
76 * Number of unique memory references so far.
78 public int memRefs = 0;
81 * Time in nanos when transaction started
83 public long startTime = 0;
85 * Time in nanos when transaction committed or aborted
87 public long stopTime = 0;
89 // generate unique ids
90 private static AtomicInteger unique = new AtomicInteger(100);
92 /** Updater for status */
94 AtomicReferenceFieldUpdater<Transaction, Status>
95 statusUpdater = AtomicReferenceFieldUpdater.newUpdater
96 (Transaction.class, Status.class, "status");
98 private volatile Status status;
102 private ContentionManager manager;
105 * Creates a new, active transaction.
107 public Transaction() {
108 this.status = Status.ACTIVE;
109 this.id = this.startTime = System.nanoTime();
110 this.manager = Thread.getContentionManager();
114 * Creates a new transaction with given status.
115 * @param myStatus active, committed, or aborted
117 private Transaction(Transaction.Status myStatus) {
118 this.status = myStatus;
123 * Access the transaction's current status.
124 * @return current transaction status
126 public Status getStatus() {
131 * Tests whether transaction is active.
132 * @return whether transaction is active
134 public boolean isActive() {
135 return this.getStatus() == Status.ACTIVE;
139 * Tests whether transaction is aborted.
140 * @return whether transaction is aborted
142 public boolean isAborted() {
144 return this.getStatus() == Status.ABORTED;
148 * Tests whether transaction is committed.
149 * @return whether transaction is committed
151 public boolean isCommitted() {
152 return (this.getStatus() == Status.COMMITTED);
156 * Tests whether transaction is committed or active.
157 * @return whether transaction is committed or active
159 public boolean validate() {
160 Status status = this.getStatus();
163 throw new PanicException("committed transaction still running");
169 throw new PanicException("unexpected transaction state: " + status);
174 * Tries to commit transaction
175 * @return whether transaction was committed
177 public boolean commit() {
179 while (this.getStatus() == Status.ACTIVE) {
180 if (statusUpdater.compareAndSet(this,
193 * Tries to abort transaction
194 * @return whether transaction was aborted (not necessarily by this call)
196 public boolean abort() {
198 boolean flag = false;
199 while (this.getStatus() == Status.ACTIVE) {
200 if (statusUpdater.compareAndSet(this, Status.ACTIVE, Status.ABORTED)) {
202 // System.out.println(Thread.currentThread() +" tamam " + this.getStatus());
206 if (getOtherSystem() != null && !(getOtherSystem().isAborted())){
207 /* synchronized(benchmark.lock){
208 System.out.println(Thread.currentThread() +" beeeeee gade sag raftim 1");
210 getOtherSystem().abortThisSystem();
211 /* synchronized(benchmark.lock){
212 System.out.println(Thread.currentThread() +" beeeeeeeee gade sag raftim 2");
218 return this.getStatus() == Status.ABORTED;
226 * Returns a string representation of this transaction
227 * @return the string representcodes[ation
229 public String toString() {
230 switch (this.status) {
232 return "Transaction" + this.startTime + "[committed]";
234 return "Transaction" + this.startTime + "[aborted]";
236 return "Transaction" + this.startTime + "[active]";
238 return "Transaction" + this.startTime + "[???]";
243 * Block caller while transaction is active.
245 public synchronized void waitWhileActive() {
246 while (this.getStatus() == Status.ACTIVE) {
249 } catch (InterruptedException ex) {}
253 * Block caller while transaction is active.
255 public synchronized void waitWhileActiveNotWaiting() {
256 while (getStatus() == Status.ACTIVE && !waiting) {
259 } catch (InterruptedException ex) {}
264 * Wake up any transactions waiting for this one to finish.
266 public synchronized void wakeUp() {
271 * This transaction's contention manager
272 * @return the manager
274 public ContentionManager getContentionManager() {
280 public TransactionStatu getOtherSystem() {
284 public void setOtherSystem(TransactionStatu othersystem) {
285 this.othersystem = othersystem;
288 public void abortThisSystem() {