76205241ef03a449ef6d655a3fc396e5e3a9d1e8
[IRC.git] / Robust / Transactions / dstm2 / src / dstm2 / Transaction.java
1 /*
2  * Transaction.java
3  *
4  * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa
5  * Clara, California 95054, U.S.A.  All rights reserved.  
6  * 
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
13  * in other countries.
14  * 
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
21  * countries.  
22  * 
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.
31  */
32
33 package dstm2;
34
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;
41
42 /**
43  * Transaction.java
44  * Keeps a transaction's status and contention manager.
45  */
46
47 public class Transaction implements TransactionStatu{
48   
49   private TransactionStatu othersystem; 
50   private boolean flag = true;
51   /**
52    * Possible transaction status
53    **/
54   public enum Status {ABORTED, ACTIVE, COMMITTED};
55   
56   /**
57    * Predefined committed transaction
58    */
59   public static Transaction COMMITTED = new Transaction(Status.COMMITTED);
60   /**
61    * Predefined orted transaction
62    */
63   public static Transaction ABORTED   = new Transaction(Status.ABORTED);
64   
65   /**
66    * Is transaction waiting for another?
67    */
68   public boolean waiting = false;
69   
70   /**
71    * Number of times this transaction tried
72    */
73   public int attempts = 0;
74   
75   /**
76    * Number of unique memory references so far.
77    */
78   public int memRefs = 0;
79   
80   /**
81    * Time in nanos when transaction started
82    */
83   public long startTime = 0;
84   /**
85    * Time in nanos when transaction committed or aborted
86    */
87   public long stopTime = 0;
88   
89   // generate unique ids
90   private static AtomicInteger unique = new AtomicInteger(100);
91   
92   /** Updater for status */
93   private static final
94       AtomicReferenceFieldUpdater<Transaction, Status>
95       statusUpdater = AtomicReferenceFieldUpdater.newUpdater
96       (Transaction.class, Status.class, "status");
97   
98   private volatile Status status;
99   
100   private long id;
101   
102   private ContentionManager manager;
103   
104   /**
105    * Creates a new, active transaction.
106    */
107   public Transaction() {
108     this.status = Status.ACTIVE;
109     this.id = this.startTime = System.nanoTime();
110     this.manager = Thread.getContentionManager();
111   }
112   
113   /**
114    * Creates a new transaction with given status.
115    * @param myStatus active, committed, or aborted
116    */
117   private Transaction(Transaction.Status myStatus) {
118     this.status = myStatus;
119     this.startTime = 0;
120   }
121   
122   /**
123    * Access the transaction's current status.
124    * @return current transaction status
125    */
126   public Status getStatus() {
127     return status;
128   }
129   
130   /**
131    * Tests whether transaction is active.
132    * @return whether transaction is active
133    */
134   public boolean isActive() {
135     return this.getStatus() == Status.ACTIVE;
136   }
137   
138   /**
139    * Tests whether transaction is aborted.
140    * @return whether transaction is aborted
141    */
142   public boolean isAborted() {
143   
144     return this.getStatus() == Status.ABORTED;
145   }
146   
147   /**
148    * Tests whether transaction is committed.
149    * @return whether transaction is committed
150    */
151   public boolean isCommitted() {
152     return (this.getStatus() == Status.COMMITTED);
153   }
154   
155   /**
156    * Tests whether transaction is committed or active.
157    * @return whether transaction is committed or active
158    */
159   public boolean validate() {
160     Status status = this.getStatus();
161     switch (status) {
162       case COMMITTED:
163         throw new PanicException("committed transaction still running");
164       case ACTIVE:
165         return true;
166       case ABORTED:
167         return false;
168       default:
169         throw new PanicException("unexpected transaction state: " + status);
170     }
171   }
172   
173   /**
174    * Tries to commit transaction
175    * @return whether transaction was committed
176    */
177   public boolean commit() {
178     try {
179       while (this.getStatus() == Status.ACTIVE) {
180         if (statusUpdater.compareAndSet(this,
181             Status.ACTIVE,
182             Status.COMMITTED)) {
183           return true;
184         }
185       }
186       return false;
187     } finally {
188       wakeUp();
189     }
190   }
191   
192   /**
193    * Tries to abort transaction
194    * @return whether transaction was aborted (not necessarily by this call)
195    */
196   public boolean abort() {
197     try {
198       boolean flag = false;  
199       while (this.getStatus() == Status.ACTIVE) {
200         if (statusUpdater.compareAndSet(this, Status.ACTIVE, Status.ABORTED)) {
201             flag = true;
202      //       System.out.println(Thread.currentThread() +" tamam " + this.getStatus());
203         }
204       }
205       if (flag)
206         if (getOtherSystem() != null && !(getOtherSystem().isAborted())){
207        /*     synchronized(benchmark.lock){
208                     System.out.println(Thread.currentThread() +" beeeeee gade sag raftim 1");
209              } */
210              getOtherSystem().abortThisSystem();
211          /*    synchronized(benchmark.lock){
212                     System.out.println(Thread.currentThread() +" beeeeeeeee gade sag raftim 2");
213              } */
214         }
215       
216       
217       
218       return this.getStatus() == Status.ABORTED;
219     } finally {
220         
221       wakeUp();
222     }
223   }
224   
225   /**
226    * Returns a string representation of this transaction
227    * @return the string representcodes[ation
228    */
229   public String toString() {
230     switch (this.status) {
231       case COMMITTED:
232         return "Transaction" + this.startTime + "[committed]";
233       case ABORTED:
234         return "Transaction" + this.startTime + "[aborted]";
235       case ACTIVE:
236         return "Transaction" + this.startTime + "[active]";
237       default:
238         return "Transaction" + this.startTime + "[???]";
239     }
240   }
241   
242   /**
243    * Block caller while transaction is active.
244    */
245   public synchronized void waitWhileActive() {
246     while (this.getStatus() == Status.ACTIVE) {
247       try {
248         wait();
249       } catch (InterruptedException ex) {}
250     }
251   }
252   /**
253    * Block caller while transaction is active.
254    */
255   public synchronized void waitWhileActiveNotWaiting() {
256     while (getStatus() == Status.ACTIVE && !waiting) {
257       try {
258         wait();
259       } catch (InterruptedException ex) {}
260     }
261   }
262   
263   /**
264    * Wake up any transactions waiting for this one to finish.
265    */
266   public synchronized void wakeUp() {
267     notifyAll();
268   }
269   
270   /**
271    * This transaction's contention manager
272    * @return the manager
273    */
274    public ContentionManager getContentionManager() {
275     return manager;
276    }
277
278    
279    
280     public TransactionStatu getOtherSystem() {
281         return othersystem;
282     }
283
284     public void setOtherSystem(TransactionStatu othersystem) {
285         this.othersystem = othersystem;
286     }
287
288     public void abortThisSystem() {
289         abort();
290     }
291 }