Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 360fa13067a6eda258cd80ae69800102c1205144 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*******************************************************************************
 * Copyright (c) 2012, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.internal.container;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/*
 * Implementation note: This class does not pool ReentrantLocks for the objects
 * that are being locked.  This means that if the same object is locked and unlocked
 * over and over then new ReentrantLocks are created each time.  This set should be
 * used with care.  If the same object is going to be locked/unlocked over and over then
 * consider using a different locking strategy.
 * 
 * Previous implementations of this class attempted to use a WeakHashMap to cache
 * the locks, but this proved to be a flawed approach because of the unpredictable
 * timing of garbage collection, particularly with autoboxed types (e.g. bundle
 * long ids).
 */
public class LockSet<T> {
	private final Map<T, ReentrantLock> locks = new HashMap<T, ReentrantLock>();
	private final Object monitor = new Object();

	public boolean tryLock(T t, long time, TimeUnit unit) throws InterruptedException {
		ReentrantLock lock;
		synchronized (monitor) {
			lock = locks.get(t);
			if (lock == null) {
				lock = new ReentrantLock();
				locks.put(t, lock);
			}
		}
		boolean previousInterruption = Thread.interrupted();
		try {
			boolean obtained = !lock.isHeldByCurrentThread() && lock.tryLock(time, unit);
			if (obtained) {
				synchronized (monitor) {
					// must check that another thread did not remove the lock
					// when unlocking while we were waiting to obtain the lock
					if (!locks.containsKey(t)) {
						locks.put(t, lock);
					}
				}
			}
			return obtained;
		} finally {
			if (previousInterruption) {
				Thread.currentThread().interrupt();
			}
		}
	}

	public void unlock(T t) {
		synchronized (monitor) {
			ReentrantLock lock = locks.get(t);
			if (lock == null)
				throw new IllegalStateException("No lock found."); //$NON-NLS-1$
			if (lock.getHoldCount() == 1) {
				// We are about to remove the last hold;
				// Clear out the lock from the map;
				// This forces a new lock to get created if the same object is locked again;
				// Must remove before unlocking to avoid removing a lock that may be waiting to
				// be obtained by another thread in tryLock
				locks.remove(t);
			}
			lock.unlock();
		}
	}

}

Back to the top