KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > concurrency > lib > TimeStamp


1 /**
2  * Copyright (C) 2003 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.perseus.concurrency.lib;
19
20 import java.util.Map JavaDoc;
21 import java.util.HashMap JavaDoc;
22
23 /**
24  *
25  * @author S.Chassande-Barrioz
26  */

27 public class TimeStamp extends Semaphore {
28
29     /**
30      * Time stamp of the object to which this object corresponds.
31      */

32     private long timeStamp;
33
34     /**
35      * Time stamp of the object to which this object corresponds, for each
36      * context. This map associates to each context the timestamp value (in the
37      * 63 higher order bits), and a flag indicating if the object has been
38      * accessed in write mode by the context (in the lower order bit).
39      */

40     private Map JavaDoc ctxtTimeStamp;
41     
42     public Object JavaDoc oid;
43
44     private int reservations = 0;
45
46     public TimeStamp(Object JavaDoc oid) {
47         ctxtTimeStamp = new HashMap JavaDoc();
48         this.oid = oid;
49     }
50
51     public void readIntention(Object JavaDoc ctxt) {
52         if (ctxtTimeStamp.get(ctxt) == null) {
53             ctxtTimeStamp.put(ctxt, new Long JavaDoc(timeStamp << 1));
54         }
55         reservations--;
56     }
57
58     public void writeIntention(Object JavaDoc ctxt) {
59         Long JavaDoc lts = (Long JavaDoc) ctxtTimeStamp.get(ctxt);
60         if (lts == null) {
61             lts = new Long JavaDoc(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 JavaDoc((ts + 1) << 1 | 1));
69         }
70         reservations--;
71     }
72
73     public boolean validate(Object JavaDoc ctxt) {
74         Long JavaDoc l = (Long JavaDoc) 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 JavaDoc ctxt) {
87         Long JavaDoc l = (Long JavaDoc) ctxtTimeStamp.get(ctxt);
88         if (l != null) {
89             timeStamp = l.longValue() >>> 1;
90         }
91     }
92
93     /**
94      * Removes the given context from the 'ctxtTimeStamp' map.
95      * @param ctxt a context
96      * @return true if the 'ctxtTimeStamp' map is empty, after the context has
97      * been removed from the map. In such a case, this object can be removed
98      * from the 'timeStamps' map of the enclosing class.
99      */

100     public boolean close(Object JavaDoc 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 JavaDoc ctx) {
110         Long JavaDoc l = (Long JavaDoc) 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