📄 rdwrlock.java
字号:
/* * $Source: /home/data/cvsroot/src/jacomma/util/RdWrLock.java,v $ * $Revision: 1.4 $ * $Date: 2000/10/28 20:09:08 $ * * This file is part of the jacomma framework * Copyright (c) 2000 Dimitrios Vyzovitis * mailto:dviz@egnatia.ee.auth.gr * * * * * * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA */package jacomma.util;/** * A recursive (re-enterant) read-write Lock implementation with single reader * priviledge elevation and deadlock detection **/public class RdWrLock { private java.util.Map readers_; private LockCount writer_; public RdWrLock( ) { readers_ = new java.util.HashMap( ); } public synchronized void rdlock() { try { while( !_try_rdlock() ) { // Attempt to fix the 100% CPU utilization behavior incurred by LinuxThreads Thread.currentThread().yield(); wait(); } } catch ( InterruptedException exc ) { throw new CheckedException( exc ); } } /** * Acquires a write-lock. */ public synchronized void wrlock() { try { while( !_try_wrlock() ) if ( readers_.containsKey( Thread.currentThread() ) ) // priviledge elevation attempt failed throw new IllegalStateException( "Potential Deadlock: Failed to elevate priviledges of Reader thread: " + Thread.currentThread() ); else { // Attempt to fix the 100% CPU utilization behavior incurred by LinuxThreads Thread.currentThread().yield(); wait(); } } catch ( InterruptedException exc ) { throw new CheckedException( exc ); } } /** * @return true if a read-lock is successfully acquired. */ public synchronized boolean try_rdlock() { return _try_rdlock(); } /** * @return true if a write-lock is succesfully acquired */ public synchronized boolean try_wrlock() { return _try_wrlock(); } /** * Releases (decrements count) a lock ownder by the current thread * Nothing happens if the thread is not a lock owner */ public synchronized void unlock() { if ( writer_ == null ) { LockCount rd = (LockCount)readers_.get( Thread.currentThread() ); if ( rd != null && rd.down() == 0 ) { readers_.remove( rd.thread_ ); notify(); // only writers are waiting - notify one of them } } else if ( writer_.thread_ == Thread.currentThread() && writer_.down() == 0 ) { writer_ = null; notifyAll(); // potentially many readers and/or writers are waiting. } } private boolean _try_rdlock() { if ( writer_ == null ) { LockCount count = (LockCount)readers_.get( Thread.currentThread() ); if ( count == null ) { count = new LockCount( ); readers_.put( count.thread_, count ); } count.up(); return true; } else if ( writer_.thread_ == Thread.currentThread() ) { writer_.up(); return true; } else return false; } private boolean _try_wrlock() { if ( writer_ == null ) { writer_ = (LockCount)readers_.remove( Thread.currentThread() ); if ( readers_.size() > 0 ) { if ( writer_ != null ) { // put back readers_.put( writer_.thread_, writer_ ); writer_ = null; return false; } } else if ( writer_ == null ) writer_ = new LockCount( ); } else if ( writer_.thread_ != Thread.currentThread() ) { return false; } writer_.up(); return true; } private class LockCount { Thread thread_ = Thread.currentThread(); int count_ = 0;; int up() { return ++count_; } int down() { return --count_; } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -