| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | package com.sun.fortress.interpreter.env; |
|---|
| 19 | |
|---|
| 20 | import com.sun.fortress.interpreter.evaluator.values.FValue; |
|---|
| 21 | import com.sun.fortress.interpreter.evaluator.tasks.FortressTaskRunner; |
|---|
| 22 | import com.sun.fortress.exceptions.transactions.AbortedException; |
|---|
| 23 | import com.sun.fortress.interpreter.evaluator.transactions.ReadSet; |
|---|
| 24 | import com.sun.fortress.interpreter.evaluator.transactions.Transaction; |
|---|
| 25 | |
|---|
| 26 | import java.util.ArrayList; |
|---|
| 27 | import java.util.List; |
|---|
| 28 | |
|---|
| 29 | public class ValueNode { |
|---|
| 30 | ValueNode old; |
|---|
| 31 | FValue value; |
|---|
| 32 | Transaction writer; |
|---|
| 33 | ReadSet readers; |
|---|
| 34 | |
|---|
| 35 | public ValueNode(FValue v, Transaction w, ReadSet r, ValueNode o) { |
|---|
| 36 | old = o; |
|---|
| 37 | value = v; |
|---|
| 38 | writer = w; |
|---|
| 39 | readers = new ReadSet(r); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | ValueNode() { |
|---|
| 43 | old = null; |
|---|
| 44 | value = null; |
|---|
| 45 | writer = null; |
|---|
| 46 | readers = new ReadSet(); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | public static final ValueNode nullValueNode = new ValueNode(); |
|---|
| 50 | |
|---|
| 51 | public String toString() { |
|---|
| 52 | if (this == nullValueNode) |
|---|
| 53 | return "nullValueNode"; |
|---|
| 54 | else return "ValueNode[" + value + ":" + writer + "::" + readers + "]" ; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | public FValue getValue() { return value;} |
|---|
| 58 | public Transaction getWriter() { return writer;} |
|---|
| 59 | public ReadSet getReaders() { return readers;} |
|---|
| 60 | public ValueNode getOld() { return old; } |
|---|
| 61 | |
|---|
| 62 | public void addReader() { |
|---|
| 63 | Transaction me = FortressTaskRunner.getTransaction(); |
|---|
| 64 | if (!readers.add(me)) { |
|---|
| 65 | me.abort(); |
|---|
| 66 | throw new AbortedException(me, "ReadSet Sealed : " + readers ); |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | public void AbortAllReaders() { |
|---|
| 71 | if (this == nullValueNode) |
|---|
| 72 | throw new RuntimeException(Thread.currentThread().getName() + "Trying to abort all the readers of the null value node"); |
|---|
| 73 | |
|---|
| 74 | readers.seal(); |
|---|
| 75 | for (Transaction r : readers) { |
|---|
| 76 | r.abort(); |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | public void AbortWriter() { |
|---|
| 81 | if (writer != null) { |
|---|
| 82 | writer.abort(); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | public void AbortAllReadersAndWriters() { |
|---|
| 87 | AbortAllReaders(); |
|---|
| 88 | AbortWriter(); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | public void resolveReadConflicts() { |
|---|
| 92 | Transaction me = FortressTaskRunner.getTransaction(); |
|---|
| 93 | List<Transaction> conflicts = new ArrayList<Transaction>(); |
|---|
| 94 | for (Transaction reader : readers) |
|---|
| 95 | if (reader.isActive() && !reader.isAncestorOf(me)) { |
|---|
| 96 | conflicts.add(reader); |
|---|
| 97 | } |
|---|
| 98 | if (!conflicts.isEmpty()) |
|---|
| 99 | me.getContentionManager().resolveConflict(me, conflicts); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | public void resolveWriteConflict() { |
|---|
| 103 | Transaction me = FortressTaskRunner.getTransaction(); |
|---|
| 104 | if (writer != null && writer.isActive() && !writer.isAncestorOf(me)) { |
|---|
| 105 | me.getContentionManager().resolveConflict(me, writer); |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | public void resolveReadWriteConflicts() { |
|---|
| 110 | resolveReadConflicts(); |
|---|
| 111 | resolveWriteConflict(); |
|---|
| 112 | } |
|---|
| 113 | } |
|---|