1 8 9 package com.sleepycat.je.latch; 10 11 import java.util.ArrayList ; 12 import java.util.Collections ; 13 import java.util.List ; 14 import java.util.concurrent.locks.ReentrantReadWriteLock ; 15 16 import com.sleepycat.je.DatabaseException; 17 import com.sleepycat.je.dbi.EnvironmentImpl; 18 19 29 class Java5SharedLatchImpl 30 extends ReentrantReadWriteLock 31 implements SharedLatch { 32 33 private String name; 34 private boolean noteLatch; 35 private List readers; 36 37 41 private boolean exclusiveOnly; 42 43 Java5SharedLatchImpl() { 44 super(EnvironmentImpl.getFairLatches()); 45 assert 46 (readers = Collections.synchronizedList(new ArrayList ())) != null; 47 exclusiveOnly = false; 48 } 49 50 public void setExclusiveOnly(boolean exclusiveOnly) { 51 this.exclusiveOnly = exclusiveOnly; 52 } 53 54 58 public void setName(String name) { 59 this.name = name; 60 } 61 62 66 public boolean setNoteLatch(boolean noteLatch) { 67 this.noteLatch = noteLatch; 68 return true; 69 } 70 71 82 public void acquireExclusive() 83 throws DatabaseException { 84 85 try { 86 if (isWriteLockedByCurrentThread()) { 87 throw new LatchException(name + " already held"); 88 } 89 90 writeLock().lock(); 91 92 assert (noteLatch ? noteLatch() : true); } finally { 94 assert EnvironmentImpl.maybeForceYield(); 95 } 96 } 97 98 public boolean acquireExclusiveNoWait() 99 throws DatabaseException { 100 101 try { 102 if (isWriteLockedByCurrentThread()) { 103 throw new LatchException(name + " already held"); 104 } 105 106 boolean ret = writeLock().tryLock(); 107 108 109 assert ((noteLatch & ret) ? noteLatch() : true); 110 return ret; 111 } finally { 112 assert EnvironmentImpl.maybeForceYield(); 113 } 114 } 115 116 119 public void acquireShared() 120 throws DatabaseException { 121 122 if (exclusiveOnly) { 123 acquireExclusive(); 124 return; 125 } 126 127 try { 128 boolean assertionsEnabled = false; 129 assert assertionsEnabled = true; 130 if (assertionsEnabled) { 131 if (readers.add(Thread.currentThread())) { 132 readLock().lock(); 133 } else { 134 135 } 136 } else { 137 readLock().lock(); 138 } 139 140 assert (noteLatch ? noteLatch() : true); } finally { 142 assert EnvironmentImpl.maybeForceYield(); 143 } 144 } 145 146 public boolean isOwner() { 147 boolean assertionsEnabled = false; 148 assert assertionsEnabled = true; 149 if (assertionsEnabled && !exclusiveOnly) { 150 return readers.contains(Thread.currentThread()) || 151 isWriteLockedByCurrentThread(); 152 } else { 153 return isWriteLockedByCurrentThread(); 154 } 155 } 156 157 161 public void release() 162 throws LatchNotHeldException { 163 164 try { 165 if (isWriteLockedByCurrentThread()) { 166 writeLock().unlock(); 167 168 assert (noteLatch ? unNoteLatch() : true); 169 return; 170 } 171 172 if (exclusiveOnly) { 173 return; 174 } 175 176 boolean assertionsEnabled = false; 177 assert assertionsEnabled = true; 178 if (assertionsEnabled) { 179 if (readers.remove(Thread.currentThread())) { 180 readLock().unlock(); 181 } else { 182 throw new LatchNotHeldException(name + " not held"); 183 } 184 } else { 185 186 190 readLock().unlock(); 191 } 192 193 assert (noteLatch ? unNoteLatch() : true); 194 } catch (IllegalMonitorStateException IMSE) { 195 IMSE.printStackTrace(); 196 return; 197 } 198 } 199 200 public void releaseIfOwner() 201 throws LatchNotHeldException { 202 203 if (isWriteLockedByCurrentThread()) { 204 writeLock().unlock(); 205 assert (noteLatch ? unNoteLatch() : true); 206 return; 207 } 208 209 if (exclusiveOnly) { 210 return; 211 } 212 213 assert (getReadLockCount() > 0); 214 boolean assertionsEnabled = false; 215 assert assertionsEnabled = true; 216 if (assertionsEnabled) { 217 if (readers.contains(Thread.currentThread())) { 218 readLock().unlock(); 219 assert (noteLatch ? unNoteLatch() : true); 220 } 221 } else { 222 223 227 readLock().unlock(); 228 } 229 } 230 231 234 private boolean noteLatch() 235 throws LatchException { 236 237 return LatchSupport.latchTable.noteLatch(this); 238 } 239 240 243 private boolean unNoteLatch() { 244 245 return LatchSupport.latchTable.unNoteLatch(this, name); 246 } 247 } 248 | Popular Tags |