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 dstm2.exceptions.PanicException;
36 import java.util.concurrent.atomic.AtomicReference;
37 import java.util.concurrent.atomic.AtomicInteger;
38 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
42 * Keeps a transaction's status and contention manager.
45 public class Transaction implements NewInterface {
48 * Possible transaction status
50 //public enum Status {ABORTED, ACTIVE, COMMITTED};
53 public enum Status {ABORTED, ACTIVE, COMMITTED};
56 * Predefined committed transaction
58 public static Transaction COMMITTED = new Transaction(Status.COMMITTED);
60 * Predefined orted transaction
62 public static Transaction ABORTED = new Transaction(Status.ABORTED);
65 * Is transaction waiting for another?
67 public boolean waiting = false;
70 * Number of times this transaction tried
72 public int attempts = 0;
75 * Number of unique memory references so far.
77 public int memRefs = 0;
80 * Time in nanos when transaction started
82 public long startTime = 0;
84 * Time in nanos when transaction committed or aborted
86 public long stopTime = 0;
88 // generate unique ids
89 private static AtomicInteger unique = new AtomicInteger(100);
91 /** Updater for status */
92 /*private static final
93 AtomicReferenceFieldUpdater<Transaction, Status>
94 statusUpdater = AtomicReferenceFieldUpdater.newUpdater
95 (Transaction.class, Status.class, "status");*/
97 protected static final
98 AtomicReferenceFieldUpdater<Transaction, Status>
99 statusUpdater = AtomicReferenceFieldUpdater.newUpdater
100 (Transaction.class, Status.class, "status");
103 private volatile Status status;
107 private ContentionManager manager;
110 * Creates a new, active transaction.
112 public Transaction() {
113 this.status = Status.ACTIVE;
114 this.id = this.startTime = System.nanoTime();
115 this.manager = Thread.getContentionManager();
119 * Creates a new transaction with given status.
120 * @param myStatus active, committed, or aborted
122 private Transaction(Transaction.Status myStatus) {
123 this.status = myStatus;
128 * Access the transaction's current status.
129 * @return current transaction status
131 public Status getStatus() {
136 * Tests whether transaction is active.
137 * @return whether transaction is active
139 public boolean isActive() {
140 return this.getStatus() == Status.ACTIVE;
144 * Tests whether transaction is aborted.
145 * @return whether transaction is aborted
147 public boolean isAborted() {
148 return this.getStatus() == Status.ABORTED;
152 * Tests whether transaction is committed.
153 * @return whether transaction is committed
155 public boolean isCommitted() {
156 return (this.getStatus() == Status.COMMITTED);
160 * Tests whether transaction is committed or active.
161 * @return whether transaction is committed or active
163 public boolean validate() {
164 Status status = this.getStatus();
167 throw new PanicException("committed transaction still running");
173 throw new PanicException("unexpected transaction state: " + status);
178 * Tries to commit transaction
179 * @return whether transaction was committed
181 public boolean commit() {
183 while (this.getStatus() == Status.ACTIVE) {
184 if (statusUpdater.compareAndSet(this,
197 * Tries to abort transaction
198 * @return whether transaction was aborted (not necessarily by this call)
200 public boolean abort() {
202 while (this.getStatus() == Status.ACTIVE) {
203 if (statusUpdater.compareAndSet(this, Status.ACTIVE, Status.ABORTED)) {
207 return this.getStatus() == Status.ABORTED;
214 * Returns a string representation of this transaction
215 * @return the string representcodes[ation
217 public String toString() {
218 switch (this.status) {
220 return "Transaction" + this.startTime + "[committed]";
222 return "Transaction" + this.startTime + "[aborted]";
224 return "Transaction" + this.startTime + "[active]";
226 return "Transaction" + this.startTime + "[???]";
231 * Block caller while transaction is active.
233 public synchronized void waitWhileActive() {
234 while (this.getStatus() == Status.ACTIVE) {
237 } catch (InterruptedException ex) {}
241 * Block caller while transaction is active.
243 public synchronized void waitWhileActiveNotWaiting() {
244 while (getStatus() == Status.ACTIVE && !waiting) {
247 } catch (InterruptedException ex) {}
252 * Wake up any transactions waiting for this one to finish.
254 public synchronized void wakeUp() {
259 * This transaction's contention manager
260 * @return the manager
262 public ContentionManager getContentionManager() {