1 18 package org.objectweb.perseus.concurrency.lib; 19 20 import java.util.Map ; 21 import java.util.HashMap ; 22 23 27 public class TimeStamp extends Semaphore { 28 29 32 private long timeStamp; 33 34 40 private Map ctxtTimeStamp; 41 42 public Object oid; 43 44 private int reservations = 0; 45 46 public TimeStamp(Object oid) { 47 ctxtTimeStamp = new HashMap (); 48 this.oid = oid; 49 } 50 51 public void readIntention(Object ctxt) { 52 if (ctxtTimeStamp.get(ctxt) == null) { 53 ctxtTimeStamp.put(ctxt, new Long (timeStamp << 1)); 54 } 55 reservations--; 56 } 57 58 public void writeIntention(Object ctxt) { 59 Long lts = (Long ) ctxtTimeStamp.get(ctxt); 60 if (lts == null) { 61 lts = new Long (timeStamp << 1); 62 ctxtTimeStamp.put(ctxt, lts); 63 } 64 long l = lts.longValue(); 65 boolean dirty = (l & 1) != 0; 66 if (!dirty) { 67 long ts = l >>> 1; 68 ctxtTimeStamp.put(ctxt, new Long ((ts + 1) << 1 | 1)); 69 } 70 reservations--; 71 } 72 73 public boolean validate(Object ctxt) { 74 Long l = (Long ) ctxtTimeStamp.get(ctxt); 75 if (l != null) { 76 long v = l.longValue(); 77 boolean dirty = (v & 1) != 0; 78 long ts = (dirty ? (v >>> 1) - 1 : v >>> 1); 79 if (ts != timeStamp) { 80 return false; 81 } 82 } 83 return true; 84 } 85 86 public void finalize(Object ctxt) { 87 Long l = (Long ) ctxtTimeStamp.get(ctxt); 88 if (l != null) { 89 timeStamp = l.longValue() >>> 1; 90 } 91 } 92 93 100 public boolean close(Object ctxt) { 101 ctxtTimeStamp.remove(ctxt); 102 return reservations == 0 && ctxtTimeStamp.isEmpty(); 103 } 104 105 public void reserve() { 106 reservations++; 107 } 108 109 public boolean isDirty(Object ctx) { 110 Long l = (Long ) ctxtTimeStamp.get(ctx); 111 if (l != null) { 112 long v = l.longValue(); 113 return (v & 1) != 0; 114 } else { 115 return false; 116 } 117 } 118 } 119 | Popular Tags |