Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 93   Methods: 6
NCLOC: 27   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
LockMap.java 0% 8.3% 16.7% 10%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK*
 2   
 3    PLT Utilities BSD License
 4   
 5    Copyright (c) 2007-2010 JavaPLT group at Rice University
 6    All rights reserved.
 7   
 8    Developed by: Java Programming Languages Team
 9    Rice University
 10    http://www.cs.rice.edu/~javaplt/
 11   
 12    Redistribution and use in source and binary forms, with or without modification, are permitted
 13    provided that the following conditions are met:
 14   
 15    - Redistributions of source code must retain the above copyright notice, this list of conditions
 16    and the following disclaimer.
 17    - Redistributions in binary form must reproduce the above copyright notice, this list of
 18    conditions and the following disclaimer in the documentation and/or other materials provided
 19    with the distribution.
 20    - Neither the name of the JavaPLT group, Rice University, nor the names of the library's
 21    contributors may be used to endorse or promote products derived from this software without
 22    specific prior written permission.
 23   
 24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 25    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 26    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND
 27    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 28    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 29    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 30    IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 31    OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32   
 33    *END_COPYRIGHT_BLOCK*/
 34   
 35    package edu.rice.cs.plt.concurrent;
 36   
 37    import java.util.WeakHashMap;
 38    import java.util.concurrent.locks.ReentrantLock;
 39   
 40    import edu.rice.cs.plt.tuple.IdentityWrapper;
 41   
 42    /**
 43    * A map for associating arbitrary objects with locks for the lifetime of the objects. This allows, for example,
 44    * synchronization based on the arguments to a method (the alternative of directly locking on the arguments
 45    * themselves is dangerous, because the argument objects may be used for locking in a different context; two
 46    * threads locking on the same object for unrelated purposes may lead to deadlock). Each unique key
 47    * (distinguished via {@code ==}) is associated with a different ReentrantLock. Weak references are used to
 48    * allow keys to be freely garbage-collected.
 49    */
 50    public class LockMap<T> {
 51   
 52    private final WeakHashMap<IdentityWrapper<T>, ReentrantLock> _map;
 53   
 54  0 public LockMap() { _map = new WeakHashMap<IdentityWrapper<T>, ReentrantLock>(); }
 55   
 56  14 public LockMap(int initialCapacity) {
 57  14 _map = new WeakHashMap<IdentityWrapper<T>, ReentrantLock>(initialCapacity);
 58    }
 59   
 60    /** Get the lock associated with the given value. If necessary, allocate and cache a new lock. */
 61  0 public synchronized ReentrantLock get(T val) {
 62  0 IdentityWrapper<T> key = IdentityWrapper.make(val);
 63  0 if (!_map.containsKey(key)) { _map.put(key, new ReentrantLock()); }
 64  0 return _map.get(key);
 65    }
 66   
 67    /**
 68    * Acquire the lock associated with the given value, and return a Runnable for unlocking when the lock can
 69    * be released. Clients should generally follow this invocation with a {@code try/finally} block that
 70    * guarantees execution of the resulting runnable.
 71    * @return A Runnable for invoking {@code lock.unlock()}. If invoked incorrectly, may throw an
 72    * {@link IllegalMonitorStateException}.
 73    * @see ReentrantLock#lock
 74    * @see ReentrantLock#unlock
 75    */
 76  0 public Runnable lock(T val) {
 77  0 final ReentrantLock l = get(val);
 78  0 Runnable result = new Unlocker(l);
 79  0 l.lock();
 80  0 return result;
 81    }
 82   
 83    /**
 84    * Declared out of local scope to prevent accidental ties to local variables, potentially preventing
 85    * garbage collection.
 86    */
 87    private static class Unlocker implements Runnable {
 88    private final ReentrantLock _l;
 89  0 public Unlocker(ReentrantLock l) { _l = l; }
 90  0 public void run() { _l.unlock(); }
 91    }
 92   
 93    }