1 8 9 package com.sleepycat.je.latch; 10 11 import java.util.concurrent.locks.ReentrantLock ; 12 13 import com.sleepycat.je.DatabaseException; 14 import com.sleepycat.je.dbi.EnvironmentImpl; 15 16 26 class Java5LatchImpl implements Latch { 27 28 32 static private class JEReentrantLock extends ReentrantLock { 33 JEReentrantLock(boolean fair) { 34 super(fair); 35 } 36 37 protected Thread getOwner() { 38 return super.getOwner(); 39 } 40 } 41 42 private JEReentrantLock lock; 43 private String name; 44 private LatchStats stats = new LatchStats(); 45 46 Java5LatchImpl() { 47 lock = new JEReentrantLock(EnvironmentImpl.getFairLatches()); 48 } 49 50 54 public void setName(String name) { 55 this.name = name; 56 } 57 58 69 public void acquire() 70 throws DatabaseException { 71 72 try { 73 if (lock.isHeldByCurrentThread()) { 74 stats.nAcquiresSelfOwned++; 75 throw new LatchException(name + " already held"); 76 } 77 78 if (lock.isLocked()) { 79 stats.nAcquiresWithContention++; 80 } else { 81 stats.nAcquiresNoWaiters++; 82 } 83 84 lock.lock(); 85 86 assert noteLatch(); } finally { 88 assert EnvironmentImpl.maybeForceYield(); 89 } 90 } 91 92 101 public boolean acquireNoWait() 102 throws LatchException { 103 try { 104 if (lock.isHeldByCurrentThread()) { 105 stats.nAcquiresSelfOwned++; 106 throw new LatchException(name + " already held"); 107 } 108 109 boolean ret = lock.tryLock(); 110 if (ret) { 111 assert noteLatch(); 112 stats.nAcquireNoWaitSuccessful++; 113 } else { 114 stats.nAcquireNoWaitUnsuccessful++; 115 } 116 return ret; 117 } finally { 118 assert EnvironmentImpl.maybeForceYield(); 119 } 120 } 121 122 127 public void releaseIfOwner() { 128 doRelease(false); 129 } 130 131 137 public void release() 138 throws LatchNotHeldException { 139 140 if (doRelease(true)) { 141 throw new LatchNotHeldException(name + " not held"); 142 } 143 } 144 145 150 private boolean doRelease(boolean checkHeld) { 151 152 try { 153 if (!lock.isHeldByCurrentThread()) { 154 return true; 155 } 156 lock.unlock(); 157 stats.nReleases++; 158 assert unNoteLatch(checkHeld); } catch (IllegalMonitorStateException IMSE) { 160 return true; 161 } 162 return false; 163 } 164 165 170 public boolean isOwner() { 171 return lock.isHeldByCurrentThread(); 172 } 173 174 179 public Thread owner() { 180 return lock.getOwner(); 181 } 182 183 188 public int nWaiters() { 189 return lock.getQueueLength(); 190 } 191 192 195 public LatchStats getLatchStats() { 196 LatchStats s = null; 197 try { 198 s = (LatchStats) stats.clone(); 199 } catch (CloneNotSupportedException e) { 200 201 } 202 return s; 203 } 204 205 208 public String toString() { 209 return lock.toString(); 210 } 211 212 215 private boolean noteLatch() 216 throws LatchException { 217 218 return LatchSupport.latchTable.noteLatch(this); 219 } 220 221 224 private boolean unNoteLatch(boolean checkHeld) { 225 226 227 if (checkHeld) { 228 return LatchSupport.latchTable.unNoteLatch(this, name); 229 } else { 230 LatchSupport.latchTable.unNoteLatch(this, name); 231 return true; 232 } 233 } 234 } 235 | Popular Tags |