KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.services.locks.ActiveLock
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.C_LockFactory;
26
27 import org.apache.derby.iapi.error.StandardException;
28 import org.apache.derby.iapi.services.context.ContextService;
29 import org.apache.derby.iapi.services.context.ContextManager;
30 import org.apache.derby.iapi.services.sanity.SanityManager;
31
32
33 /**
34     A Lock represents a granted or waiting lock request.
35
36     <BR>
37     MT - Mutable - Immutable identity : Thread Aware
38 */

39
40 public final class ActiveLock extends Lock {
41
42     /**
43         Set to true if the object waiting on this thread should wake up,
44         MT - mutable - java synchronized(this)
45     */

46     byte wakeUpNow;
47
48     /**
49         Set to true if the Lock potentially could be granted.
50
51         MT - mutable - single thread required
52     */

53     boolean potentiallyGranted;
54
55     /**
56         If true then this lock can be granted even if
57         it is not the first lock request on the wait queue.
58         This can occur if the compatability space already holds
59         a lock on the object.
60     */

61     protected boolean canSkip;
62
63     /**
64         Initialize the lock, should be seen as part of the constructor. A future
65         version of this class may become mutable - mutable identity.
66
67         MT - single thread required
68     */

69     protected ActiveLock(Object JavaDoc space, Lockable ref, Object JavaDoc qualifier) {
70         super(space, ref, qualifier);
71     }
72
73     /**
74         Set the potentially granted flag, returns true if the
75         flag changed its state.
76
77         MT - single thread required
78     */

79     protected boolean setPotentiallyGranted() {
80         if (!potentiallyGranted) {
81             potentiallyGranted = true;
82             return true;
83         }
84         return false;
85     }
86
87     /**
88         Clear the potentially granted flag.
89
90         MT - single thread required
91     */

92     protected void clearPotentiallyGranted() {
93         potentiallyGranted = false;
94     }
95
96     /**
97         Wait for a lock to be granted, returns when the lock is granted.
98         <P>
99         The sleep wakeup scheme depends on the two booleans wakeUpNow & potentiallyGranted.
100           
101         MT - Single thread required - and assumed to be the thread requesting the lock.
102
103         @return true if the wait ended early (ie. someone else woke us up).
104
105         @exception StandardException timeout, deadlock or thread interrupted
106     */

107     protected synchronized byte waitForGrant(int timeout)
108         throws StandardException
109     {
110
111         if (wakeUpNow == Constants.WAITING_LOCK_IN_WAIT) {
112
113             try {
114
115
116                 if (timeout == C_LockFactory.WAIT_FOREVER) {
117                     wait();
118                 }
119                 else if (timeout > 0) {
120                     wait(timeout);
121                 }
122
123             } catch (InterruptedException JavaDoc ie) {
124                 throw StandardException.interrupt(ie);
125             }
126         }
127
128         byte why = wakeUpNow;
129         wakeUpNow = Constants.WAITING_LOCK_IN_WAIT;
130         return why;
131     }
132
133     /**
134         Wake up anyone sleeping on this lock.
135
136         MT - Thread Safe
137     */

138     protected synchronized void wakeUp(byte why) {
139         // If we were picked as a deadlock victim then don't
140
// override the wakeup reason with another one.
141
if (wakeUpNow != Constants.WAITING_LOCK_DEADLOCK)
142             wakeUpNow = why;
143         notify();
144     }
145 }
146
147
Popular Tags