helpful progress reporting
[IRC.git] / Robust / Transactions / src / file / factory / Adapter.java
1 /*
2  * Adapter.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 package dstm2.file.factory;
33
34 import dstm2.ContentionManager;
35 import dstm2.Transaction;
36 import dstm2.exceptions.AbortedException;
37 import dstm2.exceptions.PanicException;
38 import dstm2.exceptions.SnapshotException;
39 import dstm2.factory.Copyable;
40 import dstm2.factory.Factory;
41 import dstm2.Thread;
42 import dstm2.factory.Releasable;
43 import dstm2.factory.Snapable;
44
45 import dstm2.factory.shadow.RecoverableFactory;
46 import java.io.File;
47 import java.io.RandomAccessFile;
48 import java.lang.Class;
49 import java.lang.reflect.InvocationTargetException;
50 import java.lang.reflect.Method;
51 import java.util.HashMap;
52 import java.util.Map;
53 import java.util.concurrent.ConcurrentHashMap;
54 import java.util.concurrent.atomic.AtomicBoolean;
55 import java.util.concurrent.atomic.AtomicInteger;
56 import java.util.concurrent.atomic.AtomicLong;
57 import java.util.concurrent.atomic.AtomicReference;
58 import java.util.concurrent.locks.Lock;
59 import java.util.concurrent.locks.ReentrantLock;
60 import java.util.concurrent.locks.ReentrantReadWriteLock;
61 import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
62
63 /**
64  * Obstruction-free atomic object implementation. Visible reads.
65  * Support snapshots and early release.
66  * @author Navid Farri
67  */
68 public class Adapter {
69
70     //public HashMap<Integer,BlockLock> lockmap;  
71     public HashMap lockmap;
72     //public AtomicLong commitedoffset;
73     public AtomicLong commitedfilesize;
74     private Transaction writer;
75    // protected AtomicInteger version = new AtomicInteger(0);
76     //public ReentrantLock lock;
77
78     public Transaction getWriter() {
79         return writer;
80     }
81
82     public void setWriter(Transaction writer) {
83         this.writer = writer;
84     }
85     
86
87     public Adapter() {
88        // version.set(0);
89       //  lock = new ReentrantLock();
90         writer = null;
91         lockmap = new HashMap();
92 //        commitedoffset.set(0);
93     }
94
95    /* public Adapter(Adapter adapter) {
96         version.set(adapter.version.get());
97         lock = adapter.lock;
98         writer = adapter.writer;
99         lockmap = adapter.lockmap;
100     }*/
101     /*
102      * Creates a new instance of Adapter
103      */
104     //protected Factory<T> factory;
105   /*
106     protected AtomicReference<Locator> start;
107     
108     public AtomicReference<Locator> getStart() {
109     return start;
110     }
111     
112     public void setStart(AtomicReference<Locator> start) {
113     this.start = start;
114     }
115      */
116     /*  public void onRead(){
117     
118     try {
119     Thread.onCommitOnce( new Runnable() {
120     public void run() {
121     /// commit the changes to the actual file, meanwhile the memory blocks would be owned by this
122     /// transaction so no change could take place regarding those, this need not be done atomically
123     }
124     });
125     
126     Thread.onAbortOnce( new Runnable() {
127     public void run() {
128     //// nothing no-op
129     }
130     });
131     
132     Transaction me  = Thread.getTransaction();
133     Locator oldLocator = start.get();
134     Copyable version = oldLocator.fastPath(me);
135     
136     ContentionManager manager = Thread.getContentionManager();
137     Locator newLocator = new Locator(me, (Copyable) new CopyableFileFactory());
138     version = (Copyable) newLocator.newVersion;
139     while (true) {
140     oldLocator.writePath(me, manager, newLocator);
141     if (!me.isActive()) {
142     throw new AbortedException();
143     }
144     
145     if (Adapter.this.start.compareAndSet(oldLocator, newLocator)) {
146     return;
147     }
148     oldLocator = Adapter.this.start.get();
149     }
150     } catch (IllegalAccessException e) {
151     throw new PanicException(e);
152     } catch (InvocationTargetException e) {
153     throw new PanicException(e);
154     }
155     }
156     
157     public void onWrite(){
158     try {
159     T version = (T) start.get().newVersion;
160     final Method method = version.getClass().getMethod(methodName);
161     return new Adapter.Getter<V>() {
162     public V call() {
163     try {
164     Transaction me  = Thread.getTransaction();
165     Locator oldLocator = Adapter.this.start.get();
166     T version = (T) oldLocator.fastPath(me);
167     if (version == null) {
168     ContentionManager manager = Thread.getContentionManager();
169     Locator newLocator = new Locator();
170     while (true) {
171     oldLocator.readPath(me, manager, newLocator);
172     if (Adapter.this.start.compareAndSet(oldLocator, newLocator)) {
173     version = (T) newLocator.newVersion;
174     break;
175     }
176     oldLocator = start.get();
177     }
178     if (!me.isActive()) {
179     throw new AbortedException();
180     }
181     }
182     return (V)method.invoke(version);
183     } catch (SecurityException e) {
184     throw new PanicException(e);
185     } catch (IllegalAccessException e) {
186     throw new PanicException(e);
187     } catch (InvocationTargetException e) {
188     throw new PanicException(e);
189     }
190     }};
191     } catch (NoSuchMethodException e) {
192     throw new PanicException(e);
193     }
194     }
195     
196     public void release() {
197     Transaction me  = Thread.getTransaction();
198     Locator oldLocator = this.start.get();
199     T version = (T) oldLocator.fastPath(me);
200     if (version == null) {
201     ContentionManager manager = Thread.getContentionManager();
202     Locator newLocator = new Locator();
203     version = (T) newLocator.newVersion;
204     while (true) {
205     oldLocator.releasePath(me, manager, newLocator);
206     if (this.start.compareAndSet(oldLocator, newLocator)) {
207     break;
208     }
209     oldLocator = this.start.get();
210     }
211     if (!me.isActive()) {
212     throw new AbortedException();
213     }
214     }
215     return;
216     }
217     
218     public T snapshot() {
219     Transaction me  = Thread.getTransaction();
220     Locator oldLocator = this.start.get();
221     T version = (T) oldLocator.fastPath(me);
222     if (version == null) {
223     ContentionManager manager = Thread.getContentionManager();
224     return (T)oldLocator.snapshot(me, manager);
225     } else {
226     return version;
227     }
228     }
229     
230     public void validate(T snap) {
231     if (snap != snapshot()) {
232     throw new SnapshotException();
233     }
234     }
235     
236     public void upgrade(T snap) {
237     Transaction me  = Thread.getTransaction();
238     Locator oldLocator = this.start.get();
239     T version = (T) oldLocator.fastPath(me);
240     if (version != null) {
241     if (version != snap) {
242     throw new SnapshotException();
243     } else {
244     return;
245     }
246     }
247     ContentionManager manager = Thread.getContentionManager();
248     Locator newLocator = new Locator(me, (Copyable)factory.create());
249     while (true) {
250     oldLocator.writePath(me, manager, newLocator);
251     if (!me.isActive()) {
252     throw new AbortedException();
253     }
254     if (snap != newLocator.oldVersion) {
255     throw new SnapshotException();
256     }
257     if (this.start.compareAndSet(oldLocator, newLocator)) {
258     return;
259     }
260     oldLocator = this.start.get();
261     }
262     }*/
263 }
264