Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fe6a64b1e6f372040263f8d76e867d96ecb6fea3 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*******************************************************************************
 * Copyright (c) 2012, 2016 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.atomic.AtomicInteger;
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> {
	static final class LockHolder {
		private final AtomicInteger useCount = new AtomicInteger(0);
		private final ReentrantLock lock = new ReentrantLock();

		int incrementUseCount() {
			return useCount.incrementAndGet();
		}

		int decremementUseCount() {
			return useCount.decrementAndGet();
		}

		boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
			return !lock.isHeldByCurrentThread() && lock.tryLock(time, unit);
		}

		void unlock() {
			lock.unlock();
		}
	}

	private final Map<T, LockHolder> locks = new HashMap<>();

	public boolean tryLock(T t, long time, TimeUnit unit) throws InterruptedException {
		final boolean previousInterruption = Thread.interrupted();
		try {
			LockHolder lock;
			synchronized (locks) {
				lock = locks.get(t);
				if (lock == null) {
					lock = new LockHolder();
					locks.put(t, lock);
				}
				lock.incrementUseCount();
			}
			// all interested threads have the lock object and the use count is the number of such threads
			boolean acquired = false;
			try {
				acquired = lock.tryLock(time, unit);
				return acquired;
			} finally {
				if (!acquired) {
					synchronized (locks) {
						// If, after failing to acquire the lock, no other thread is using the lock, discard it.
						if (lock.decremementUseCount() == 0) {
							locks.remove(t);
						}
					}
				}
			}
		} finally {
			if (previousInterruption) {
				Thread.currentThread().interrupt();
			}
		}
	}

	public void unlock(T t) {
		synchronized (locks) {
			LockHolder lock = locks.get(t);
			if (lock == null)
				throw new IllegalStateException("No lock found."); //$NON-NLS-1$
			lock.unlock();
			// If, after unlocking, no other thread is using the lock, discard it.
			if (lock.decremementUseCount() == 0) {
				locks.remove(t);
			}
		}
	}
}

Back to the top