01: /*
02: * $RCSfile: GeometryLock.java,v $
03: *
04: * Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved.
05: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06: *
07: * This code is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License version 2 only, as
09: * published by the Free Software Foundation. Sun designates this
10: * particular file as subject to the "Classpath" exception as provided
11: * by Sun in the LICENSE file that accompanied this code.
12: *
13: * This code is distributed in the hope that it will be useful, but WITHOUT
14: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16: * version 2 for more details (a copy is included in the LICENSE file that
17: * accompanied this code).
18: *
19: * You should have received a copy of the GNU General Public License version
20: * 2 along with this work; if not, write to the Free Software Foundation,
21: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22: *
23: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24: * CA 95054 USA or visit www.sun.com if you need additional information or
25: * have any questions.
26: *
27: * $Revision: 1.5 $
28: * $Date: 2008/02/28 20:17:22 $
29: * $State: Exp $
30: */
31:
32: package javax.media.j3d;
33:
34: import java.util.Vector;
35:
36: class GeometryLock {
37:
38: // Current thread holding the lock
39: Thread threadId = null;
40:
41: // Whether the lock is currently owned
42: boolean lockOwned = false;
43:
44: // Count > 1 , if there is nested lock by the same thread
45: int count = 0;
46:
47: // Number of outstanding threads waiting for the lock
48: int waiting = 0;
49:
50: synchronized void getLock() {
51: Thread curThread = Thread.currentThread();
52: // If the thread already has the lock, incr
53: // a count and return
54: if (threadId == curThread) {
55: count++;
56: return;
57: }
58: // Otherwise, wait until the lock is released
59: while (lockOwned) {
60: try {
61: waiting++;
62: wait();
63: } catch (InterruptedException e) {
64: System.err.println(e);
65: }
66: waiting--;
67: }
68: count++;
69: // Acquire the lock
70: lockOwned = true;
71: threadId = curThread;
72: }
73:
74: synchronized void unLock() {
75: Thread curThread = Thread.currentThread();
76: if (threadId == curThread) {
77: // If the lock count > 0, then return
78: if (--count > 0) {
79: return;
80: }
81: lockOwned = false;
82: threadId = null;
83: if (waiting > 0) {
84: notify();
85: }
86: }
87:
88: }
89:
90: }
|