| 68 | 1 /* | 
|  | 2 Copyright (c) 2008  Franklin Schmidt <fschmidt@gmail.com> | 
|  | 3 | 
|  | 4 Permission is hereby granted, free of charge, to any person obtaining a copy | 
|  | 5 of this software and associated documentation files (the "Software"), to deal | 
|  | 6 in the Software without restriction, including without limitation the rights | 
|  | 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
|  | 8 copies of the Software, and to permit persons to whom the Software is | 
|  | 9 furnished to do so, subject to the following conditions: | 
|  | 10 | 
|  | 11 The above copyright notice and this permission notice shall be included in | 
|  | 12 all copies or substantial portions of the Software. | 
|  | 13 | 
|  | 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
|  | 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
|  | 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
|  | 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
|  | 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
|  | 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
|  | 20 THE SOFTWARE. | 
|  | 21 */ | 
|  | 22 | 
|  | 23 package fschmidt.util.locks; | 
|  | 24 | 
|  | 25 import java.util.Map; | 
|  | 26 import java.util.HashMap; | 
|  | 27 import java.util.concurrent.TimeUnit; | 
|  | 28 import java.util.concurrent.locks.Lock; | 
|  | 29 import java.util.concurrent.locks.ReentrantLock; | 
|  | 30 | 
|  | 31 | 
|  | 32 public final class TimedLocker<T> implements Locker<T> { | 
|  | 33 | 
|  | 34 	private static class Tracker { | 
|  | 35 		final ReentrantLock lock0 = new ReentrantLock(); | 
|  | 36 		final Lock lock = new TimedLock(lock0); | 
|  | 37 		int waiters = 0; | 
|  | 38 	} | 
|  | 39 | 
|  | 40 	private final Map<T,Tracker> map = new HashMap<T,Tracker>(); | 
|  | 41 | 
|  | 42 	private Tracker getTracker(T obj) { | 
|  | 43 		Tracker t = map.get(obj); | 
|  | 44 		if( t==null ) { | 
|  | 45 			t = new Tracker(); | 
|  | 46 			map.put(obj,t); | 
|  | 47 		} | 
|  | 48 		return t; | 
|  | 49 	} | 
|  | 50 | 
|  | 51 	private Tracker getTrackerForWait(T obj) { | 
|  | 52 		synchronized(map) { | 
|  | 53 			Tracker t = getTracker(obj); | 
|  | 54 			t.waiters++; | 
|  | 55 			return t; | 
|  | 56 		} | 
|  | 57 	} | 
|  | 58 | 
|  | 59 	private void afterWait(Tracker t) { | 
|  | 60 		synchronized(map) { | 
|  | 61 			t.waiters--; | 
|  | 62 		} | 
|  | 63 	} | 
|  | 64 | 
|  | 65 	private void maybeRemove(T obj,Tracker t) { | 
|  | 66 		if( t.waiters==0 && !t.lock0.isHeldByCurrentThread() ) { | 
|  | 67 			map.remove(obj); | 
|  | 68 		} | 
|  | 69 	} | 
|  | 70 | 
|  | 71 	public void lock(T obj) { | 
|  | 72 		Tracker t = getTrackerForWait(obj); | 
|  | 73 		try { | 
|  | 74 			t.lock.lock(); | 
|  | 75 		} finally { | 
|  | 76 			afterWait(t); | 
|  | 77 		} | 
|  | 78 	} | 
|  | 79 | 
|  | 80 	public boolean tryLock(T obj) { | 
|  | 81 		synchronized(map) { | 
|  | 82 			return getTracker(obj).lock.tryLock(); | 
|  | 83 		} | 
|  | 84 	} | 
|  | 85 | 
|  | 86 	public boolean tryLock(T obj,long time,TimeUnit unit) | 
|  | 87 		throws InterruptedException | 
|  | 88 	{ | 
|  | 89 		Tracker t = getTrackerForWait(obj); | 
|  | 90 		try { | 
|  | 91 			return t.lock.tryLock(time,unit); | 
|  | 92 		} finally { | 
|  | 93 			afterWait(t); | 
|  | 94 		} | 
|  | 95 	} | 
|  | 96 | 
|  | 97 	public void unlock(T obj) { | 
|  | 98 		synchronized(map) { | 
|  | 99 			Tracker t = map.get(obj); | 
|  | 100 			t.lock.unlock(); | 
|  | 101 			maybeRemove(obj,t); | 
|  | 102 		} | 
|  | 103 	} | 
|  | 104 | 
|  | 105 	public boolean hasLock(T obj) { | 
|  | 106 		synchronized(map) { | 
|  | 107 			Tracker t = map.get(obj); | 
|  | 108 			return t!=null && t.lock0.isHeldByCurrentThread(); | 
|  | 109 		} | 
|  | 110 	} | 
|  | 111 | 
|  | 112 } |