KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > concurrency > pessimistic > MutexLock


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

25
26 package org.objectweb.perseus.concurrency.pessimistic;
27
28 import org.objectweb.perseus.concurrency.api.ConcurrencyException;
29 import org.objectweb.perseus.concurrency.lib.RWLockValue;
30 import org.objectweb.perseus.dependency.api.DependencyGraph;
31
32 /**
33  * A lock associated to an oid (see the "locks" map within the pessimistic
34  * concurrency manager).
35  * Provides a mutex concurrency policy.
36  * @author E. Bruneton, P. Dechamboux
37  */

38 public final class MutexLock extends Lock {
39     /**
40      * The contexts that have a lock for the oid to which this object is
41      * associated.
42      */

43     protected Object JavaDoc writer = null;
44     /**
45      * The number of threads that wait for aquiring this lock.
46      */

47     int waiter = 0;
48
49     public MutexLock() {
50     }
51
52     public MutexLock(Object JavaDoc hints, DependencyGraph dg) {
53         super(hints, dg);
54     }
55
56     /**
57      * Acquires this lock in read mode for the given context. This method
58      * blocks until the lock can be acquired in read mode by this context.
59      * @param ctxt a context.
60      */

61     public void readIntention(Object JavaDoc ctxt)
62             throws ConcurrencyException {
63         writeIntention(ctxt);
64     }
65
66     /**
67      * Acquires this lock in write mode for the given context. This method
68      * blocks until the lock can be acquired in write mode by this context.
69      * @param ctxt a context.
70      */

71     public synchronized void writeIntention(Object JavaDoc ctxt)
72             throws ConcurrencyException {
73         boolean ok;
74         do {
75             ok = ((writer == null) || writer.equals(ctxt));
76             if (!ok) {
77                 waiter ++;
78                 try {
79                     wait();
80                 } catch (InterruptedException JavaDoc e) {
81                     throw new ConcurrencyException(
82                         "Waiting of a read intention has been interupted: ", e);
83                 } finally {
84                     waiter --;
85                 }
86             }
87         } while (!ok);
88         reservations--;
89         writer = ctxt;
90     }
91
92     /**
93      * Removes the given context from the reader and writer lists of this
94      * lock.
95      * @param ctxt a context
96      * @return true if the reader and writer list are empty, after the
97      * context has been removed from these lists. In such a case, this
98      * object can be removed from the 'locks' map.
99      */

100     public synchronized boolean close(Object JavaDoc ctxt) {
101         //TODO: wake up blocking threads working in the closed context
102
if ((writer != null) && writer.equals(ctxt)) {
103             writer = null;
104         }
105         boolean res = reservations == 0 && writer == null && waiter == 0;
106         if (!res) {
107             notifyAll();
108         }
109         return res;
110     }
111
112     public synchronized byte getMax() {
113         if (writer != null) return RWLockValue.WRITE;
114         return RWLockValue.NOLOCK;
115     }
116 }
117
Popular Tags