KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > view > spi > ViewHierarchyMutex


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor.view.spi;
21
22 /**
23  * Mutex that allows only one thread to proceed
24  * other threads must wait until the ONE finishes.
25  * <br>
26  * The thread that "holds" the mutex (has the mutex access granted)
27  * may reenter the mutex any number of times
28  * (just increasing a "depth" of the locking).
29  * <br>
30  * If the priority thread enters waiting the mutex
31  * then it will get serviced first once the current thread
32  * leaves the mutex.
33  *
34  * @author Miloslav Metelka
35  * @version 1.00
36  */

37
38 public class ViewHierarchyMutex {
39
40     private Thread JavaDoc lockThread;
41
42     private int lockDepth;
43     
44     private Thread JavaDoc waitingPriorityThread;
45     
46     public synchronized void lock() {
47         Thread JavaDoc thread = Thread.currentThread();
48         boolean priorityThread = isPriorityThread(thread);
49
50         if (thread != lockThread) { // not nested locking
51
// Will wait if either there is another thread already holding the lock
52
// or if there is a priority thread waiting but it's not this thread
53
while (lockThread != null
54                 || (waitingPriorityThread != null && waitingPriorityThread != thread)
55             ) {
56                 try {
57                     if (waitingPriorityThread == null && priorityThread) {
58                         waitingPriorityThread = thread;
59                     }
60
61                     wait();
62
63                 } catch (InterruptedException JavaDoc e) {
64                     waitingPriorityThread = null;
65                 }
66             }
67
68             lockThread = thread;
69
70             if (thread == waitingPriorityThread) {
71                 waitingPriorityThread = null; // it's now allowed to enter
72
}
73         }
74
75         lockDepth++;
76     }
77     
78     public synchronized void unlock() {
79         if (Thread.currentThread() != lockThread) {
80             throw new IllegalStateException JavaDoc("Not locker"); // NOI18N
81
}
82
83         if (--lockDepth == 0) {
84             lockThread = null;
85
86             notifyAll(); // must all to surely notify waitingPriorityThread too
87
}
88     }
89     
90     /**
91      * This method is intended to be called by the non-priority thread
92      * that acquired the mutex to check whether there
93      * is no priority thread (such as AWT event-notification thread)
94      * waiting.
95      * <br>
96      * If there is a priority thread waiting the non-priority thread
97      * should attempt to stop its work as soon as possible and unlock
98      * the hierarchy.
99      * <br>
100      * The method must *not* be called without first locking the hierarchy
101      * (it is intentionally not synchronized).
102      */

103     public boolean isPriorityThreadWaiting() {
104         return (waitingPriorityThread != null);
105     }
106     
107     protected boolean isPriorityThread(Thread JavaDoc thread) {
108         return (thread != ViewLayoutQueue.getDefaultQueue().getWorkerThread());
109     }
110
111     /**
112      * Return the thread that holds a lock on this mutex.
113      * <br>
114      * This method is intended for diagnostic purposes only to determine
115      * an intruder thread that entered the view hierarchy without obtaining
116      * the lock first.
117      *
118      * @return thread that currently holds a lock on the hierarchy or null
119      * if there is currently no thread holding a lock on the hierarchy.
120      */

121     public final synchronized Thread JavaDoc getLockThread() {
122         return lockThread;
123     }
124
125 }
126
Popular Tags