KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > imr > ResourceLock


1 package org.jacorb.imr;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24
25 /**
26  * This class provides shared or exclusive access to a ressource.
27  * It preferes the exclusive access, i.e. if threads are waiting for
28  * exclusive access, shared locks can't be gained.
29  *
30  * @author Nicolas Noffke
31  *
32  * $Id: ResourceLock.java,v 1.3 2004/05/06 12:39:59 nicolas Exp $
33  *
34  */

35
36 public class ResourceLock
37     implements java.io.Serializable JavaDoc
38 {
39     private int shared;
40     private int exclusive;
41     private boolean exclusives_waiting = false;
42
43     /**
44      * The constructor.
45      */

46
47     public ResourceLock()
48     {
49         shared = 0;
50         exclusive = 0;
51     }
52
53     /**
54      * This method tries to aquire a shared lock. It blocks
55      * until the exclusive lock is released.
56      */

57
58     public synchronized void gainSharedLock()
59     {
60         while(exclusive > 0 && exclusives_waiting)
61         {
62             try
63             {
64                 wait();
65             }
66             catch (java.lang.Exception JavaDoc _e)
67             {
68             }
69         }
70         shared++;
71     }
72
73     /**
74      * Release the shared lock. Unblocks threads waiting for
75      * access.
76      */

77
78     public synchronized void releaseSharedLock()
79     {
80         if (--shared == 0)
81             notifyAll();
82     }
83
84     /**
85      * This method tries to aquire an exclusive lock. It blocks until
86      * all shared locks have been released.
87      */

88
89     public synchronized void gainExclusiveLock()
90     {
91         while(shared > 0 || exclusive > 0)
92         {
93             try
94             {
95                 exclusives_waiting = true;
96                 wait();
97             }
98             catch (java.lang.Exception JavaDoc _e)
99             {
100             }
101         }
102         exclusive++;
103         exclusives_waiting = false;
104     }
105
106     /**
107      * Releases the exclusive lock. Unblocks all threads waiting
108      * for access.
109      */

110
111     public synchronized void releaseExclusiveLock()
112     {
113         if (--exclusive == 0)
114             notifyAll();
115     }
116
117 } // ResourceLock
118

119
120
121
Popular Tags