6 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa
7 * Clara, California 95054, U.S.A. All rights reserved.
9 * Sun Microsystems, Inc. has intellectual property rights relating to
10 * technology embodied in the product that is described in this
11 * document. In particular, and without limitation, these
12 * intellectual property rights may include one or more of the
13 * U.S. patents listed at http://www.sun.com/patents and one or more
14 * additional patents or pending patent applications in the U.S. and
17 * U.S. Government Rights - Commercial software.
18 * Government users are subject to the Sun Microsystems, Inc. standard
19 * license agreement and applicable provisions of the FAR and its
20 * supplements. Use is subject to license terms. Sun, Sun
21 * Microsystems, the Sun logo and Java are trademarks or registered
22 * trademarks of Sun Microsystems, Inc. in the U.S. and other
25 * This product is covered and controlled by U.S. Export Control laws
26 * and may be subject to the export or import laws in other countries.
27 * Nuclear, missile, chemical biological weapons or nuclear maritime
28 * end uses or end users, whether direct or indirect, are strictly
29 * prohibited. Export or reexport to countries subject to
30 * U.S. embargo or to entities identified on U.S. export exclusion
31 * lists, including, but not limited to, the denied persons and
32 * specially designated nationals lists is strictly prohibited.
37 import TransactionalIO.exceptions.AbortedException;
38 import TransactionalIO.exceptions.PanicException;
39 import dstm2.factory.ofree.ReadSet;
40 import java.lang.reflect.Array;
45 @atomic public class AtomicByteArray {
47 private final Byte[] array;
48 private final Byte[] shadow;
49 private Transaction writer;
50 private ReadSet readers;
52 private final String FORMAT = "Unexpected transaction state: %s";
54 /** Creates a new instance of AtomicArray */
55 public AtomicByteArray(Class _class, int capacity) {
56 array = (Byte[]) Array.newInstance(_class, capacity);
57 shadow = (Byte[]) Array.newInstance(_class, capacity);
58 writer = Transaction.COMMITTED;
59 readers = new ReadSet();
63 public Byte get(int i) {
64 Transaction me = Thread.getTransaction();
65 Transaction other = null;
66 ContentionManager manager = Thread.getContentionManager();
74 manager.resolveConflict(me, other);
78 public void set(int i, Byte value) {
79 Transaction me = Thread.getTransaction();
80 Transaction other = null;
81 ContentionManager manager = Thread.getContentionManager();
84 other = openWrite(me);
90 manager.resolveConflict(me, other);
94 * Tries to open object for reading. Returns reference to conflictin transaction, if one exists
96 private Transaction openRead(Transaction me) {
97 // not in a transaction
98 if (me == null) { // restore object if latest writer aborted
99 if (writer.isAborted()) {
102 writer = Transaction.COMMITTED;
106 // Am I still active?
107 if (!me.isActive()) {
108 throw new AbortedException();
110 // Have I already opened this object?
114 switch (writer.getStatus()) {
124 throw new PanicException(FORMAT, writer.getStatus());
126 writer = Transaction.COMMITTED;
132 * Tries to open object for reading. Returns reference to conflicting transaction, if one exists
134 public Transaction openWrite(Transaction me) {
135 // not in a transaction
136 if (me == null) { // restore object if latest writer aborted
137 if (writer.isAborted()) {
140 writer = Transaction.COMMITTED;
144 if (!me.validate()) {
145 throw new AbortedException();
150 for (Transaction reader : readers) {
151 if (reader.isActive() && reader != me) {
156 switch (writer.getStatus()) {
168 throw new PanicException(FORMAT, writer.getStatus());
174 private void restore() {
175 System.arraycopy(shadow, 0, array, 0, array.length);
177 private void backup() {
178 System.arraycopy(array, 0, shadow, 0, array.length);