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.
32 package dstm2.file.factory;
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;
42 import dstm2.factory.Releasable;
43 import dstm2.factory.Snapable;
45 import dstm2.factory.shadow.RecoverableFactory;
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;
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;
64 * Obstruction-free atomic object implementation. Visible reads.
65 * Support snapshots and early release.
68 public class Adapter {
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;
78 public Transaction getWriter() {
82 public void setWriter(Transaction writer) {
89 // lock = new ReentrantLock();
91 lockmap = new HashMap();
92 // commitedoffset.set(0);
95 /* public Adapter(Adapter adapter) {
96 version.set(adapter.version.get());
98 writer = adapter.writer;
99 lockmap = adapter.lockmap;
102 * Creates a new instance of Adapter
104 //protected Factory<T> factory;
106 protected AtomicReference<Locator> start;
108 public AtomicReference<Locator> getStart() {
112 public void setStart(AtomicReference<Locator> start) {
116 /* public void onRead(){
119 Thread.onCommitOnce( new Runnable() {
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
126 Thread.onAbortOnce( new Runnable() {
132 Transaction me = Thread.getTransaction();
133 Locator oldLocator = start.get();
134 Copyable version = oldLocator.fastPath(me);
136 ContentionManager manager = Thread.getContentionManager();
137 Locator newLocator = new Locator(me, (Copyable) new CopyableFileFactory());
138 version = (Copyable) newLocator.newVersion;
140 oldLocator.writePath(me, manager, newLocator);
141 if (!me.isActive()) {
142 throw new AbortedException();
145 if (Adapter.this.start.compareAndSet(oldLocator, newLocator)) {
148 oldLocator = Adapter.this.start.get();
150 } catch (IllegalAccessException e) {
151 throw new PanicException(e);
152 } catch (InvocationTargetException e) {
153 throw new PanicException(e);
157 public void onWrite(){
159 T version = (T) start.get().newVersion;
160 final Method method = version.getClass().getMethod(methodName);
161 return new Adapter.Getter<V>() {
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();
171 oldLocator.readPath(me, manager, newLocator);
172 if (Adapter.this.start.compareAndSet(oldLocator, newLocator)) {
173 version = (T) newLocator.newVersion;
176 oldLocator = start.get();
178 if (!me.isActive()) {
179 throw new AbortedException();
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);
191 } catch (NoSuchMethodException e) {
192 throw new PanicException(e);
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;
205 oldLocator.releasePath(me, manager, newLocator);
206 if (this.start.compareAndSet(oldLocator, newLocator)) {
209 oldLocator = this.start.get();
211 if (!me.isActive()) {
212 throw new AbortedException();
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);
230 public void validate(T snap) {
231 if (snap != snapshot()) {
232 throw new SnapshotException();
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();
247 ContentionManager manager = Thread.getContentionManager();
248 Locator newLocator = new Locator(me, (Copyable)factory.create());
250 oldLocator.writePath(me, manager, newLocator);
251 if (!me.isActive()) {
252 throw new AbortedException();
254 if (snap != newLocator.oldVersion) {
255 throw new SnapshotException();
257 if (this.start.compareAndSet(oldLocator, newLocator)) {
260 oldLocator = this.start.get();