001: /*
002: * $RCSfile: MRSWLock.java,v $
003: *
004: * Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
006: *
007: * This code is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License version 2 only, as
009: * published by the Free Software Foundation. Sun designates this
010: * particular file as subject to the "Classpath" exception as provided
011: * by Sun in the LICENSE file that accompanied this code.
012: *
013: * This code is distributed in the hope that it will be useful, but WITHOUT
014: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: * version 2 for more details (a copy is included in the LICENSE file that
017: * accompanied this code).
018: *
019: * You should have received a copy of the GNU General Public License version
020: * 2 along with this work; if not, write to the Free Software Foundation,
021: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
024: * CA 95054 USA or visit www.sun.com if you need additional information or
025: * have any questions.
026: *
027: * $Revision: 1.6 $
028: * $Date: 2008/02/28 20:17:26 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: /**
035: * Use this lock to allow multiple reads/single write synchronization.
036: * To prevent deadlock a read/writeLock call must match with a read/writeUnlock call.
037: * Write request has precedence over read request.
038: */
039:
040: class MRSWLock {
041:
042: static boolean debug = false;
043:
044: private int readCount;
045: private boolean write;
046: private int writeRequested;
047: private int lockRequested;
048:
049: MRSWLock() {
050: readCount = 0;
051: write = false;
052: writeRequested = 0;
053: lockRequested = 0;
054: }
055:
056: synchronized final void readLock() {
057: lockRequested++;
058: while ((write == true) || (writeRequested > 0)) {
059: try {
060: wait();
061: } catch (InterruptedException e) {
062: }
063: }
064: lockRequested--;
065: readCount++;
066: }
067:
068: synchronized final void readUnlock() {
069: if (readCount > 0)
070: readCount--;
071: else if (debug)
072: System.err
073: .println("ReadWriteLock.java : Problem! readCount is >= 0.");
074:
075: if (lockRequested > 0)
076: notifyAll();
077: }
078:
079: synchronized final void writeLock() {
080: lockRequested++;
081: writeRequested++;
082: while ((readCount > 0) || (write == true)) {
083: try {
084: wait();
085: } catch (InterruptedException e) {
086: }
087: }
088: write = true;
089: lockRequested--;
090: writeRequested--;
091: }
092:
093: synchronized final void writeUnlock() {
094: write = false;
095:
096: if (lockRequested > 0)
097: notifyAll();
098: }
099:
100: }
|