*** empty log message ***
[IRC.git] / Robust / Transactions / dstm2src / manager / KindergartenManager.java
1 /*
2  * KindergartenManager.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.manager;
34
35 import dstm2.Transaction;
36 import dstm2.ContentionManager;
37 import dstm2.util.Random;
38 import java.util.TreeSet;
39
40 /**
41  * Transactions take turns playing with blocks.
42  *
43  * @author Bill Scherer
44  */
45 public class KindergartenManager extends BaseManager {
46   static final int SLEEP_PERIOD = 1000; // was 100
47   static final int MAX_RETRIES = 100; // was 10
48   TreeSet<KindergartenManager> otherChildren;
49   Random random;
50   
51   /** Creates new <code>Kindergarten</code> manager */
52   public KindergartenManager() {
53     super();
54     otherChildren = new TreeSet<KindergartenManager>();
55     otherChildren.add(this);
56     random = new Random();
57   }
58   
59   public void resolveConflict(Transaction me, Transaction other) {
60     try {
61       KindergartenManager otherManager =
62           (KindergartenManager) other.getContentionManager();
63       // first, check sharing records.
64       if (otherChildren.contains(otherManager)) {
65         otherChildren.remove(otherManager);
66         other.abort();                      // My turn! My turn!
67         return;
68       }
69       for (int i = 0; i < MAX_RETRIES; i++) {
70         this.sleep(SLEEP_PERIOD);
71         if (!other.isActive()) {
72           return;
73         }
74       }
75       me.abort(); // give up
76       return;
77     } catch (ClassCastException e) {
78       other.abort(); // Oh, other not a Kindergartener. Kill it.
79       return;
80     }
81   }
82 }