Mercurial Hosting > nabble
diff src/fschmidt/util/locks/TimedReadWriteLocker.java @ 68:00520880ad02
add fschmidt source
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 05 Oct 2025 17:24:15 -0600 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fschmidt/util/locks/TimedReadWriteLocker.java Sun Oct 05 17:24:15 2025 -0600 @@ -0,0 +1,154 @@ +/* +Copyright (c) 2008 Franklin Schmidt <fschmidt@gmail.com> + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +package fschmidt.util.locks; + +import java.util.Map; +import java.util.HashMap; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + + +public final class TimedReadWriteLocker<T> implements ReadWriteLocker<T> { + + private static class Tracker { + final ReentrantReadWriteLock lock0 = new ReentrantReadWriteLock(); + final ReadWriteLock lock = new TimedReadWriteLock(lock0); + int waiters = 0; + } + + private final Map<T,Tracker> map = new HashMap<T,Tracker>(); + + private Tracker getTracker(T obj) { + Tracker t = map.get(obj); + if( t==null ) { + t = new Tracker(); + map.put(obj,t); + } + return t; + } + + private Tracker getTrackerForWait(T obj) { + synchronized(map) { + Tracker t = getTracker(obj); + t.waiters++; + return t; + } + } + + private void afterWait(Tracker t) { + synchronized(map) { + t.waiters--; + } + } + + private void maybeRemove(T obj,Tracker t) { + if( t.waiters==0 && !t.lock0.isWriteLocked() && t.lock0.getReadLockCount()==0 ) { + map.remove(obj); + } + } + + private final Locker<T> writeLocker = new Locker<T>() { + + public void lock(T obj) { + Tracker t = getTrackerForWait(obj); + try { + t.lock.writeLock().lock(); + } finally { + afterWait(t); + } + } + + public boolean tryLock(T obj) { + synchronized(map) { + return getTracker(obj).lock.writeLock().tryLock(); + } + } + + public boolean tryLock(T obj,long time,TimeUnit unit) + throws InterruptedException + { + Tracker t = getTrackerForWait(obj); + try { + return t.lock.writeLock().tryLock(time,unit); + } finally { + afterWait(t); + } + } + + public void unlock(T obj) { + synchronized(map) { + Tracker t = map.get(obj); + t.lock.writeLock().unlock(); + maybeRemove(obj,t); + } + } + }; + + private final Locker<T> readLocker = new Locker<T>() { + + public void lock(T obj) { + Tracker t = getTrackerForWait(obj); + try { + t.lock.readLock().lock(); + } finally { + afterWait(t); + } + } + + public boolean tryLock(T obj) { + synchronized(map) { + return getTracker(obj).lock.readLock().tryLock(); + } + } + + public boolean tryLock(T obj,long time,TimeUnit unit) + throws InterruptedException + { + Tracker t = getTrackerForWait(obj); + try { + return t.lock.readLock().tryLock(time,unit); + } finally { + afterWait(t); + } + } + + public void unlock(T obj) { + synchronized(map) { + Tracker t = map.get(obj); + t.lock.readLock().unlock(); + maybeRemove(obj,t); + } + } + }; + + public Locker<T> readLocker() { + return readLocker; + } + + public Locker<T> writeLocker() { + return writeLocker; + } + +}