| 1 | package org.jtoolkit.essence.utils; |
| 2 | /* |
| 3 | Copyright 2006 Peter Lawrey |
| 4 | |
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | import org.apache.commons.logging.Log; |
| 19 | import org.apache.commons.logging.LogFactory; |
| 20 | import org.jetbrains.annotations.NotNull; |
| 21 | import org.jtoolkit.essence.ConfigProperties; |
| 22 | |
| 23 | import java.util.*; |
| 24 | import java.util.concurrent.TimeUnit; |
| 25 | import java.util.concurrent.locks.Condition; |
| 26 | import java.util.concurrent.locks.Lock; |
| 27 | import java.util.concurrent.locks.ReadWriteLock; |
| 28 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 29 | |
| 30 | /** |
| 31 | * A lock which can help you detect ReadWriteLock deadlocks and attempts to upgrade a lock. |
| 32 | * It will log which threads have locked a lock if you are waiting for one too long. |
| 33 | * It can also be used to ensure you have no locks when attempting a lengthy (blocking) operation. |
| 34 | * Use RWLock.createLock() to create a normal lock or one of these reporting locks based on a system property |
| 35 | * Add -Drwlock.debug=true to the commandline to turn on. |
| 36 | */ |
| 37 | public class RWLock implements ReadWriteLock, Named { |
| 38 | private static final Log LOG = LogFactory.getLog(RWLock.class); |
| 39 | private static final ThreadLocal<Set<RWLock>> thisThreadsLocks = new ThreadLocal<Set<RWLock>>() { |
| 40 | protected Set<RWLock> initialValue() { |
| 41 | return new LinkedHashSet<RWLock>(); |
| 42 | } |
| 43 | }; |
| 44 | private static final boolean DEBUG = Boolean.getBoolean(ConfigProperties.ESSENCE_RWLOCK_DEBUG); |
| 45 | |
| 46 | private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); |
| 47 | private final List<Thread> readThreads = Collections.synchronizedList(new ArrayList<Thread>()); |
| 48 | private final List<Thread> writeThreads = Collections.synchronizedList(new ArrayList<Thread>()); |
| 49 | private final String name; |
| 50 | |
| 51 | public RWLock(String name) { |
| 52 | this.name = name; |
| 53 | } |
| 54 | |
| 55 | @NotNull |
| 56 | public String getName() { |
| 57 | return name; |
| 58 | } |
| 59 | |
| 60 | private final Lock readLock = new ReadLock(); |
| 61 | |
| 62 | private final Lock writeLock = new WriteLock(); |
| 63 | |
| 64 | public static boolean isDebug() { |
| 65 | return DEBUG; |
| 66 | } |
| 67 | |
| 68 | public Lock readLock() { |
| 69 | return readLock; |
| 70 | } |
| 71 | |
| 72 | public Lock writeLock() { |
| 73 | return writeLock; |
| 74 | } |
| 75 | |
| 76 | public String toString() { |
| 77 | return "RWLock " + name + ",readThreads=" + readThreads + ",writeThreads=" + writeThreads; |
| 78 | } |
| 79 | |
| 80 | public static void checkUnlocked(String oper) { |
| 81 | Set<RWLock> rwLocks = thisThreadsLocks.get(); |
| 82 | if (!rwLocks.isEmpty()) |
| 83 | LOG.warn(Thread.currentThread() + ": Attempting " + oper + " while locked " + rwLocks, new Throwable("here")); |
| 84 | } |
| 85 | |
| 86 | public static ReadWriteLock createLock(String name) { |
| 87 | return DEBUG ? new RWLock(name) : new ReentrantReadWriteLock(); |
| 88 | } |
| 89 | |
| 90 | private class ReadLock implements Lock { |
| 91 | public void lock() { |
| 92 | lock.readLock().lock(); |
| 93 | addReadLock(); |
| 94 | } |
| 95 | |
| 96 | public void lockInterruptibly() throws InterruptedException { |
| 97 | lock.readLock().lockInterruptibly(); |
| 98 | addReadLock(); |
| 99 | } |
| 100 | |
| 101 | public boolean tryLock() { |
| 102 | boolean ret = lock.readLock().tryLock(); |
| 103 | if (ret) |
| 104 | addReadLock(); |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { |
| 109 | boolean ret = lock.readLock().tryLock(time, unit); |
| 110 | if (ret) |
| 111 | addReadLock(); |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | public void unlock() { |
| 116 | lock.readLock().unlock(); |
| 117 | removeReadLock(); |
| 118 | } |
| 119 | |
| 120 | public Condition newCondition() { |
| 121 | return lock.readLock().newCondition(); |
| 122 | } |
| 123 | |
| 124 | private void addReadLock() { |
| 125 | if (readThreads.add(Thread.currentThread())) |
| 126 | thisThreadsLocks.get().add(RWLock.this); |
| 127 | } |
| 128 | |
| 129 | private void removeReadLock() { |
| 130 | Thread t = Thread.currentThread(); |
| 131 | readThreads.remove(t); |
| 132 | if (!readThreads.contains(t) && !writeThreads.contains(t)) |
| 133 | thisThreadsLocks.get().remove(RWLock.this); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | private class WriteLock implements Lock { |
| 138 | public void lock() { |
| 139 | checkReadLock(); |
| 140 | lock.writeLock().lock(); |
| 141 | addWriteLock(); |
| 142 | } |
| 143 | |
| 144 | public void lockInterruptibly() throws InterruptedException { |
| 145 | checkReadLock(); |
| 146 | while (!lock.writeLock().tryLock(1000, TimeUnit.MILLISECONDS)) |
| 147 | LOG.info(name + ": Unable to get lock for " + Thread.currentThread().getName() + " locked by read" + readThreads + ", write" + writeThreads); |
| 148 | addWriteLock(); |
| 149 | } |
| 150 | |
| 151 | public boolean tryLock() { |
| 152 | checkReadLock(); |
| 153 | boolean ret = lock.writeLock().tryLock(); |
| 154 | if (ret) |
| 155 | addWriteLock(); |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { |
| 160 | checkReadLock(); |
| 161 | boolean ret = lock.writeLock().tryLock(time, unit); |
| 162 | if (ret) |
| 163 | addWriteLock(); |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | @SuppressWarnings({"ProhibitedExceptionCaught"}) |
| 168 | public void unlock() { |
| 169 | try { |
| 170 | lock.writeLock().unlock(); |
| 171 | } catch (IllegalMonitorStateException e) { |
| 172 | throw new IllegalMonitorStateException(name + ": Unable to unlock for " + Thread.currentThread() + " writeThreads" + writeThreads + ' ' + e); |
| 173 | } |
| 174 | removeWriteLock(); |
| 175 | } |
| 176 | |
| 177 | public Condition newCondition() { |
| 178 | return lock.writeLock().newCondition(); |
| 179 | } |
| 180 | |
| 181 | private void checkReadLock() { |
| 182 | Thread t = Thread.currentThread(); |
| 183 | if (readThreads.contains(t)) |
| 184 | throw new IllegalMonitorStateException(name + ": Cannot be upgraded from readLock to writeLock"); |
| 185 | } |
| 186 | |
| 187 | private void addWriteLock() { |
| 188 | if (writeThreads.add(Thread.currentThread())) |
| 189 | thisThreadsLocks.get().add(RWLock.this); |
| 190 | } |
| 191 | |
| 192 | private void removeWriteLock() { |
| 193 | Thread t = Thread.currentThread(); |
| 194 | writeThreads.remove(t); |
| 195 | if (!readThreads.contains(t) && !writeThreads.contains(t)) |
| 196 | thisThreadsLocks.get().remove(RWLock.this); |
| 197 | } |
| 198 | } |
| 199 | } |