KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > services > locks > Lock


1 /*
2
3    Derby - Class org.apache.derby.impl.services.locks.Lock
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.services.locks;
23
24 import org.apache.derby.iapi.services.locks.Lockable;
25 import org.apache.derby.iapi.services.locks.Latch;
26
27 import org.apache.derby.iapi.services.sanity.SanityManager;
28
29 import java.util.List JavaDoc;
30
31 /**
32     A Lock represents a granted or waiting lock request.
33
34     <BR>
35     MT - Mutable - Immutable identity : Thread Aware
36 */

37
38 public class Lock implements Latch, Control {
39
40     /**
41         Compatibility space the object is locked in.
42         MT - immutable - reference only
43     */

44     private final Object JavaDoc space;
45
46     /**
47         Object being locked.
48         MT - immutable - reference only
49     */

50     private final Lockable ref;
51     /**
52         Qualifier used in the lock request..
53         MT - immutable - reference only
54     */

55     private final Object JavaDoc qualifier;
56
57     int count;
58
59     protected Lock(Object JavaDoc space, Lockable ref, Object JavaDoc qualifier) {
60         super();
61         this.space = space;
62         this.ref = ref;
63         this.qualifier = qualifier;
64     }
65
66     /**
67         Return the object this lock is held on
68
69         MT - Thread safe
70     */

71     public final Lockable getLockable() {
72         return ref;
73     }
74
75     /**
76         Return the compatability space this lock is held in
77
78         MT - Thread safe
79     */

80     public final Object JavaDoc getCompatabilitySpace() {
81         return space;
82     }
83
84     /**
85         Return the qualifier lock was obtained with.
86
87         MT - Thread safe
88     */

89
90     public final Object JavaDoc getQualifier() {
91         return qualifier;
92     }
93
94     /**
95         Return the count of locks.
96
97         MT - Thread safe
98     */

99
100     public final int getCount() {
101         return count;
102     }
103
104     // make a copy of this lock with the count set to zero, copies are only
105
// to be used in the LockSpace code.
106
final Lock copy() {
107
108         return new Lock(space, ref, qualifier);
109     }
110
111     void grant() {
112
113         count++;
114
115         // Tell the object it has been locked by this type of qualifier.
116
ref.lockEvent(this);
117     }
118
119     int unlock(int unlockCount) {
120
121         if (unlockCount > count)
122             unlockCount = count;
123
124         count -= unlockCount;
125         if (count == 0) {
126
127             // Inform the object an unlock event occured with this qualifier
128

129             ref.unlockEvent(this);
130         }
131
132         return unlockCount;
133     }
134
135     /*
136     ** Methods of object
137     */

138
139     public final int hashCode() {
140
141         // qualifier can be null so don't use it in hashCode
142

143         return ref.hashCode() ^ space.hashCode();
144     }
145
146     public final boolean equals(Object JavaDoc other) {
147
148         if (other instanceof Lock) {
149             Lock ol = (Lock) other;
150
151             return (space.equals(ol.space)) && ref.equals(ol.ref) && (qualifier == ol.qualifier);
152         }
153
154         return false;
155     }
156
157     /*
158     ** Methods of Control
159     */

160
161     public LockControl getLockControl() {
162         return new LockControl(this, ref);
163     }
164
165     public Lock getLock(Object JavaDoc compatabilitySpace, Object JavaDoc qualifier) {
166         if (space.equals(compatabilitySpace) && (this.qualifier == qualifier))
167             return this;
168         return null;
169     }
170
171 //EXCLUDE-START-lockdiag-
172
/**
173         We can return ourselves here because our identity
174         is immutable and what we returned will not be accessed
175         as a Lock, so the count cannot be changed.
176     */

177     public Control shallowClone() {
178         return this;
179     }
180 //EXCLUDE-END-lockdiag-
181

182     public ActiveLock firstWaiter() {
183         return null;
184     }
185
186     public boolean isEmpty() {
187         return count == 0;
188     }
189
190     public boolean unlock(Latch lockInGroup, int unlockCount) {
191
192         if (unlockCount == 0)
193             unlockCount = lockInGroup.getCount();
194         
195         if (SanityManager.DEBUG) {
196             if (unlockCount > getCount())
197                 SanityManager.THROWASSERT(this + " unlockCount " + unlockCount + " is greater than lockCount " + getCount());
198             if (!equals(lockInGroup))
199                 SanityManager.THROWASSERT(this + " mismatched locks " + lockInGroup);
200
201         }
202
203         unlock(unlockCount);
204
205         return false;
206     }
207     public void addWaiters(java.util.Dictionary JavaDoc waiters) {
208     }
209     public Lock getFirstGrant() {
210         return this;
211     }
212     public List JavaDoc getGranted() {
213         return null;
214     }
215     public List JavaDoc getWaiting() {
216         return null;
217     }
218
219     public boolean isGrantable(boolean noWaitersBeforeMe, Object JavaDoc compatabilitySpace, Object JavaDoc requestQualifier)
220     {
221         boolean sameSpace = space.equals(compatabilitySpace);
222         if (sameSpace && ref.lockerAlwaysCompatible())
223             return true;
224
225         return ref.requestCompatible(requestQualifier, this.qualifier);
226     }
227 }
228
229
Popular Tags