| 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 | ValueNode() { |
|---|
| 36 | old = null; |
|---|
| 37 | value = null; |
|---|
| 38 | writer = FortressTaskRunner.getTransaction(); |
|---|
| 39 | readers = new ReadSet(); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | ValueNode(FValue val) { |
|---|
| 43 | old = null; |
|---|
| 44 | value = val; |
|---|
| 45 | writer = FortressTaskRunner.getTransaction(); |
|---|
| 46 | readers = new ReadSet(); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | ValueNode(FValue val, Transaction w, ValueNode o) { |
|---|
| 50 | old = o; |
|---|
| 51 | value = val; |
|---|
| 52 | writer = w; |
|---|
| 53 | if (o != null) |
|---|
| 54 | readers = new ReadSet(o.readers); |
|---|
| 55 | else readers = new ReadSet(); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | ValueNode(FValue val, ReadSet r) { |
|---|
| 59 | old = null; |
|---|
| 60 | value = val; |
|---|
| 61 | readers = new ReadSet(r); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | public String toString() { |
|---|
| 65 | return "ValueNode[" + value + ":" + writer + "]" ; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | public FValue getValue() { return value;} |
|---|
| 69 | public Transaction getWriter() { return writer;} |
|---|
| 70 | public ReadSet getReaders() { return readers;} |
|---|
| 71 | public ValueNode getOld() { return old; } |
|---|
| 72 | |
|---|
| 73 | public void addReader() { |
|---|
| 74 | Transaction me = FortressTaskRunner.getTransaction(); |
|---|
| 75 | if (!readers.add(me)) { |
|---|
| 76 | me.abort(); |
|---|
| 77 | throw new AbortedException(me, "ReadSet Sealed : " + readers ); |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | public void AbortAllReaders() { |
|---|
| 82 | readers.seal(); |
|---|
| 83 | for (Transaction r : readers) { |
|---|
| 84 | r.abort(); |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | public void AbortWriter() { |
|---|
| 89 | if (writer != null) { |
|---|
| 90 | writer.abort(); |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | public void AbortAllReadersAndWriters() { |
|---|
| 95 | AbortAllReaders(); |
|---|
| 96 | AbortWriter(); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | public void resolveReadConflicts() { |
|---|
| 100 | Transaction me = FortressTaskRunner.getTransaction(); |
|---|
| 101 | List<Transaction> conflicts = new ArrayList<Transaction>(); |
|---|
| 102 | for (Transaction reader : readers) |
|---|
| 103 | if (reader.isActive() && !reader.isAncestorOf(me)) { |
|---|
| 104 | conflicts.add(reader); |
|---|
| 105 | } |
|---|
| 106 | if (!conflicts.isEmpty()) |
|---|
| 107 | me.getContentionManager().resolveConflict(me, conflicts); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | public void resolveWriteConflict() { |
|---|
| 111 | Transaction me = FortressTaskRunner.getTransaction(); |
|---|
| 112 | if (writer != null && writer.isActive() && !writer.isAncestorOf(me)) { |
|---|
| 113 | me.getContentionManager().resolveConflict(me, writer); |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | public void resolveReadWriteConflicts() { |
|---|
| 118 | resolveReadConflicts(); |
|---|
| 119 | resolveWriteConflict(); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|