1 21 22 package org.apache.derby.impl.services.locks; 23 24 import org.apache.derby.iapi.services.locks.Lockable; 25 import org.apache.derby.iapi.services.locks.Latch; 26 27 import org.apache.derby.iapi.services.sanity.SanityManager; 28 29 import java.util.List ; 30 31 37 38 public class Lock implements Latch, Control { 39 40 44 private final Object space; 45 46 50 private final Lockable ref; 51 55 private final Object qualifier; 56 57 int count; 58 59 protected Lock(Object space, Lockable ref, Object qualifier) { 60 super(); 61 this.space = space; 62 this.ref = ref; 63 this.qualifier = qualifier; 64 } 65 66 71 public final Lockable getLockable() { 72 return ref; 73 } 74 75 80 public final Object getCompatabilitySpace() { 81 return space; 82 } 83 84 89 90 public final Object getQualifier() { 91 return qualifier; 92 } 93 94 99 100 public final int getCount() { 101 return count; 102 } 103 104 final Lock copy() { 107 108 return new Lock(space, ref, qualifier); 109 } 110 111 void grant() { 112 113 count++; 114 115 ref.lockEvent(this); 117 } 118 119 int unlock(int unlockCount) { 120 121 if (unlockCount > count) 122 unlockCount = count; 123 124 count -= unlockCount; 125 if (count == 0) { 126 127 129 ref.unlockEvent(this); 130 } 131 132 return unlockCount; 133 } 134 135 138 139 public final int hashCode() { 140 141 143 return ref.hashCode() ^ space.hashCode(); 144 } 145 146 public final boolean equals(Object other) { 147 148 if (other instanceof Lock) { 149 Lock ol = (Lock) other; 150 151 return (space.equals(ol.space)) && ref.equals(ol.ref) && (qualifier == ol.qualifier); 152 } 153 154 return false; 155 } 156 157 160 161 public LockControl getLockControl() { 162 return new LockControl(this, ref); 163 } 164 165 public Lock getLock(Object compatabilitySpace, Object qualifier) { 166 if (space.equals(compatabilitySpace) && (this.qualifier == qualifier)) 167 return this; 168 return null; 169 } 170 171 177 public Control shallowClone() { 178 return this; 179 } 180 182 public ActiveLock firstWaiter() { 183 return null; 184 } 185 186 public boolean isEmpty() { 187 return count == 0; 188 } 189 190 public boolean unlock(Latch lockInGroup, int unlockCount) { 191 192 if (unlockCount == 0) 193 unlockCount = lockInGroup.getCount(); 194 195 if (SanityManager.DEBUG) { 196 if (unlockCount > getCount()) 197 SanityManager.THROWASSERT(this + " unlockCount " + unlockCount + " is greater than lockCount " + getCount()); 198 if (!equals(lockInGroup)) 199 SanityManager.THROWASSERT(this + " mismatched locks " + lockInGroup); 200 201 } 202 203 unlock(unlockCount); 204 205 return false; 206 } 207 public void addWaiters(java.util.Dictionary waiters) { 208 } 209 public Lock getFirstGrant() { 210 return this; 211 } 212 public List getGranted() { 213 return null; 214 } 215 public List getWaiting() { 216 return null; 217 } 218 219 public boolean isGrantable(boolean noWaitersBeforeMe, Object compatabilitySpace, Object requestQualifier) 220 { 221 boolean sameSpace = space.equals(compatabilitySpace); 222 if (sameSpace && ref.lockerAlwaysCompatible()) 223 return true; 224 225 return ref.requestCompatible(requestQualifier, this.qualifier); 226 } 227 } 228 229 | Popular Tags |