001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: SyncedLockManager.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:
020: /**
021: * SyncedLockManager uses the synchronized keyword to implement its critical
022: * sections.
023: */
024: public class SyncedLockManager extends LockManager {
025:
026: public SyncedLockManager(EnvironmentImpl envImpl)
027: throws DatabaseException {
028:
029: super (envImpl);
030: }
031:
032: /**
033: * @see LockManager#attemptLock
034: */
035: protected LockAttemptResult attemptLock(Long nodeId, Locker locker,
036: LockType type, boolean nonBlockingRequest)
037: throws DatabaseException {
038:
039: int lockTableIndex = getLockTableIndex(nodeId);
040: synchronized (lockTableLatches[lockTableIndex]) {
041: return attemptLockInternal(nodeId, locker, type,
042: nonBlockingRequest, lockTableIndex);
043: }
044: }
045:
046: /**
047: * @see LockManager#lookupLock
048: */
049: protected Lock lookupLock(Long nodeId) throws DatabaseException {
050:
051: int lockTableIndex = getLockTableIndex(nodeId);
052: synchronized (lockTableLatches[lockTableIndex]) {
053: return lookupLockInternal(nodeId, lockTableIndex);
054: }
055: }
056:
057: /**
058: * @see LockManager#makeTimeoutMsg
059: */
060: protected DeadlockException makeTimeoutMsg(String lockOrTxn,
061: Locker locker, long nodeId, LockType type,
062: LockGrantType grantType, Lock useLock, long timeout,
063: long start, long now, DatabaseImpl database) {
064:
065: int lockTableIndex = getLockTableIndex(nodeId);
066: synchronized (lockTableLatches[lockTableIndex]) {
067: return makeTimeoutMsgInternal(lockOrTxn, locker, nodeId,
068: type, grantType, useLock, timeout, start, now,
069: database);
070: }
071: }
072:
073: /**
074: * @see LockManager#releaseAndNotifyTargets
075: */
076: protected Set releaseAndFindNotifyTargets(long nodeId, Locker locker)
077: throws DatabaseException {
078:
079: long nid = nodeId;
080: int lockTableIndex = getLockTableIndex(nid);
081: synchronized (lockTableLatches[lockTableIndex]) {
082: return releaseAndFindNotifyTargetsInternal(nodeId, locker,
083: lockTableIndex);
084: }
085: }
086:
087: /**
088: * @see LockManager#transfer
089: */
090: void transfer(long nodeId, Locker owningLocker, Locker destLocker,
091: boolean demoteToRead) throws DatabaseException {
092:
093: int lockTableIndex = getLockTableIndex(nodeId);
094: synchronized (lockTableLatches[lockTableIndex]) {
095: transferInternal(nodeId, owningLocker, destLocker,
096: demoteToRead, lockTableIndex);
097: }
098: }
099:
100: /**
101: * @see LockManager#transferMultiple
102: */
103: void transferMultiple(long nodeId, Locker owningLocker,
104: Locker[] destLockers) throws DatabaseException {
105:
106: int lockTableIndex = getLockTableIndex(nodeId);
107: synchronized (lockTableLatches[lockTableIndex]) {
108: transferMultipleInternal(nodeId, owningLocker, destLockers,
109: lockTableIndex);
110: }
111: }
112:
113: /**
114: * @see LockManager#demote
115: */
116: void demote(long nodeId, Locker locker) throws DatabaseException {
117:
118: int lockTableIndex = getLockTableIndex(nodeId);
119: synchronized (lockTableLatches[lockTableIndex]) {
120: demoteInternal(nodeId, locker, lockTableIndex);
121: }
122: }
123:
124: /**
125: * @see LockManager#isLocked
126: */
127: boolean isLocked(Long nodeId) {
128:
129: int lockTableIndex = getLockTableIndex(nodeId);
130: synchronized (lockTableLatches[lockTableIndex]) {
131: return isLockedInternal(nodeId, lockTableIndex);
132: }
133: }
134:
135: /**
136: * @see LockManager#isOwner
137: */
138: boolean isOwner(Long nodeId, Locker locker, LockType type) {
139:
140: int lockTableIndex = getLockTableIndex(nodeId);
141: synchronized (lockTableLatches[lockTableIndex]) {
142: return isOwnerInternal(nodeId, locker, type, lockTableIndex);
143: }
144: }
145:
146: /**
147: * @see LockManager#isWaiter
148: */
149: boolean isWaiter(Long nodeId, Locker locker) {
150:
151: int lockTableIndex = getLockTableIndex(nodeId);
152: synchronized (lockTableLatches[lockTableIndex]) {
153: return isWaiterInternal(nodeId, locker, lockTableIndex);
154: }
155: }
156:
157: /**
158: * @see LockManager#nWaiters
159: */
160: int nWaiters(Long nodeId) {
161:
162: int lockTableIndex = getLockTableIndex(nodeId);
163: synchronized (lockTableLatches[lockTableIndex]) {
164: return nWaitersInternal(nodeId, lockTableIndex);
165: }
166: }
167:
168: /**
169: * @see LockManager#nOwners
170: */
171: int nOwners(Long nodeId) {
172:
173: int lockTableIndex = getLockTableIndex(nodeId);
174: synchronized (lockTableLatches[lockTableIndex]) {
175: return nOwnersInternal(nodeId, lockTableIndex);
176: }
177: }
178:
179: /**
180: * @see LockManager#getWriterOwnerLocker
181: */
182: Locker getWriteOwnerLocker(Long nodeId) throws DatabaseException {
183:
184: int lockTableIndex = getLockTableIndex(nodeId);
185: synchronized (lockTableLatches[lockTableIndex]) {
186: return getWriteOwnerLockerInternal(nodeId, lockTableIndex);
187: }
188: }
189:
190: /**
191: * @see LockManager#validateOwnership
192: */
193: protected boolean validateOwnership(Long nodeId, Locker locker,
194: LockType type, boolean flushFromWaiters, MemoryBudget mb)
195: throws DatabaseException {
196:
197: int lockTableIndex = getLockTableIndex(nodeId);
198: synchronized (lockTableLatches[lockTableIndex]) {
199: return validateOwnershipInternal(nodeId, locker, type,
200: flushFromWaiters, mb, lockTableIndex);
201: }
202: }
203:
204: /**
205: * @see LockManager#dumpLockTable
206: */
207: protected void dumpLockTable(LockStats stats)
208: throws DatabaseException {
209:
210: for (int i = 0; i < nLockTables; i++) {
211: synchronized (lockTableLatches[i]) {
212: dumpLockTableInternal(stats, i);
213: }
214: }
215: }
216: }
|