001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: LatchedLockManager.java,v 1.11.2.5 2008/01/07 15:14:17 cwl Exp $
007: */
008:
009: package com.sleepycat.je.txn;
010:
011: import java.util.Set;
012:
013: import com.sleepycat.je.DatabaseException;
014: import com.sleepycat.je.DeadlockException;
015: import com.sleepycat.je.LockStats;
016: import com.sleepycat.je.dbi.DatabaseImpl;
017: import com.sleepycat.je.dbi.EnvironmentImpl;
018: import com.sleepycat.je.dbi.MemoryBudget;
019: import com.sleepycat.je.latch.Latch;
020:
021: /**
022: * LatchedLockManager uses latches to implement its critical sections.
023: */
024: public class LatchedLockManager extends LockManager {
025:
026: public LatchedLockManager(EnvironmentImpl envImpl)
027: throws DatabaseException {
028: super (envImpl);
029: }
030:
031: /**
032: * @see LockManager#lookupLock
033: */
034: protected Lock lookupLock(Long nodeId) throws DatabaseException {
035:
036: int lockTableIndex = getLockTableIndex(nodeId);
037: Latch latch = lockTableLatches[lockTableIndex];
038: latch.acquire();
039: try {
040: return lookupLockInternal(nodeId, lockTableIndex);
041: } finally {
042: latch.release();
043: }
044: }
045:
046: /**
047: * @see LockManager#attemptLock
048: */
049: protected LockAttemptResult attemptLock(Long nodeId, Locker locker,
050: LockType type, boolean nonBlockingRequest)
051: throws DatabaseException {
052:
053: int lockTableIndex = getLockTableIndex(nodeId);
054: Latch latch = lockTableLatches[lockTableIndex];
055: latch.acquire();
056: try {
057: return attemptLockInternal(nodeId, locker, type,
058: nonBlockingRequest, lockTableIndex);
059: } finally {
060: latch.release();
061: }
062: }
063:
064: /**
065: * @see LockManager#makeTimeoutMsg
066: */
067: protected DeadlockException makeTimeoutMsg(String lockOrTxn,
068: Locker locker, long nodeId, LockType type,
069: LockGrantType grantType, Lock useLock, long timeout,
070: long start, long now, DatabaseImpl database)
071: throws DatabaseException {
072:
073: int lockTableIndex = getLockTableIndex(nodeId);
074: Latch latch = lockTableLatches[lockTableIndex];
075: latch.acquire();
076: try {
077: return makeTimeoutMsgInternal(lockOrTxn, locker, nodeId,
078: type, grantType, useLock, timeout, start, now,
079: database);
080: } finally {
081: latch.release();
082: }
083: }
084:
085: /**
086: * @see LockManager#releaseAndNotifyTargets
087: */
088: protected Set releaseAndFindNotifyTargets(long nodeId, Locker locker)
089: throws DatabaseException {
090:
091: long nid = nodeId;
092: int lockTableIndex = getLockTableIndex(nid);
093: Latch latch = lockTableLatches[lockTableIndex];
094: latch.acquire();
095: try {
096: return releaseAndFindNotifyTargetsInternal(nodeId, locker,
097: lockTableIndex);
098: } finally {
099: latch.release();
100: }
101: }
102:
103: /**
104: * @see LockManager#transfer
105: */
106: void transfer(long nodeId, Locker owningLocker, Locker destLocker,
107: boolean demoteToRead) throws DatabaseException {
108:
109: int lockTableIndex = getLockTableIndex(nodeId);
110: Latch latch = lockTableLatches[lockTableIndex];
111: latch.acquire();
112: try {
113: transferInternal(nodeId, owningLocker, destLocker,
114: demoteToRead, lockTableIndex);
115: } finally {
116: latch.release();
117: }
118: }
119:
120: /**
121: * @see LockManager#transferMultiple
122: */
123: void transferMultiple(long nodeId, Locker owningLocker,
124: Locker[] destLockers) throws DatabaseException {
125:
126: int lockTableIndex = getLockTableIndex(nodeId);
127: Latch latch = lockTableLatches[lockTableIndex];
128: latch.acquire();
129: try {
130: transferMultipleInternal(nodeId, owningLocker, destLockers,
131: lockTableIndex);
132: } finally {
133: latch.release();
134: }
135: }
136:
137: /**
138: * @see LockManager#demote
139: */
140: void demote(long nodeId, Locker locker) throws DatabaseException {
141:
142: int lockTableIndex = getLockTableIndex(nodeId);
143: Latch latch = lockTableLatches[lockTableIndex];
144: latch.acquire();
145: try {
146: demoteInternal(nodeId, locker, lockTableIndex);
147: } finally {
148: latch.release();
149: }
150: }
151:
152: /**
153: * @see LockManager#isLocked
154: */
155: boolean isLocked(Long nodeId) throws DatabaseException {
156:
157: int lockTableIndex = getLockTableIndex(nodeId);
158: Latch latch = lockTableLatches[lockTableIndex];
159: latch.acquire();
160: try {
161: return isLockedInternal(nodeId, lockTableIndex);
162: } finally {
163: latch.release();
164: }
165: }
166:
167: /**
168: * @see LockManager#isOwner
169: */
170: boolean isOwner(Long nodeId, Locker locker, LockType type)
171: throws DatabaseException {
172:
173: int lockTableIndex = getLockTableIndex(nodeId);
174: Latch latch = lockTableLatches[lockTableIndex];
175: latch.acquire();
176: try {
177: return isOwnerInternal(nodeId, locker, type, lockTableIndex);
178: } finally {
179: latch.release();
180: }
181: }
182:
183: /**
184: * @see LockManager#isWaiter
185: */
186: boolean isWaiter(Long nodeId, Locker locker)
187: throws DatabaseException {
188:
189: int lockTableIndex = getLockTableIndex(nodeId);
190: Latch latch = lockTableLatches[lockTableIndex];
191: latch.acquire();
192: try {
193: return isWaiterInternal(nodeId, locker, lockTableIndex);
194: } finally {
195: latch.release();
196: }
197: }
198:
199: /**
200: * @see LockManager#nWaiters
201: */
202: int nWaiters(Long nodeId) throws DatabaseException {
203:
204: int lockTableIndex = getLockTableIndex(nodeId);
205: Latch latch = lockTableLatches[lockTableIndex];
206: latch.acquire();
207: try {
208: return nWaitersInternal(nodeId, lockTableIndex);
209: } finally {
210: latch.release();
211: }
212: }
213:
214: /**
215: * @see LockManager#nOwners
216: */
217: int nOwners(Long nodeId) throws DatabaseException {
218:
219: int lockTableIndex = getLockTableIndex(nodeId);
220: Latch latch = lockTableLatches[lockTableIndex];
221: latch.acquire();
222: try {
223: return nOwnersInternal(nodeId, lockTableIndex);
224: } finally {
225: latch.release();
226: }
227: }
228:
229: /**
230: * @see LockManager#getWriterOwnerLocker
231: */
232: Locker getWriteOwnerLocker(Long nodeId) throws DatabaseException {
233:
234: int lockTableIndex = getLockTableIndex(nodeId);
235: Latch latch = lockTableLatches[lockTableIndex];
236: latch.acquire();
237: try {
238: return getWriteOwnerLockerInternal(nodeId, lockTableIndex);
239: } finally {
240: latch.release();
241: }
242: }
243:
244: /**
245: * @see LockManager#validateOwnership
246: */
247: protected boolean validateOwnership(Long nodeId, Locker locker,
248: LockType type, boolean flushFromWaiters, MemoryBudget mb)
249: throws DatabaseException {
250:
251: int lockTableIndex = getLockTableIndex(nodeId);
252: Latch latch = lockTableLatches[lockTableIndex];
253: latch.acquire();
254: try {
255: return validateOwnershipInternal(nodeId, locker, type,
256: flushFromWaiters, mb, lockTableIndex);
257: } finally {
258: latch.release();
259: }
260: }
261:
262: /**
263: * @see LockManager#dumpLockTable
264: */
265: protected void dumpLockTable(LockStats stats)
266: throws DatabaseException {
267:
268: for (int i = 0; i < nLockTables; i++) {
269: lockTableLatches[i].acquire();
270: try {
271: dumpLockTableInternal(stats, i);
272: } finally {
273: lockTableLatches[i].release();
274: }
275: }
276: }
277: }
|