1 28 29 package org.apache.commons.transaction.locking; 30 31 import org.apache.commons.transaction.util.LoggerFacade; 32 33 71 public class ReadWriteUpgradeLock extends GenericLock { 72 73 public static final int NO_LOCK = 0; 74 75 public static final int READ_LOCK = 1; 76 77 public static final int UPGRADE_LOCK = 2; 78 79 public static final int WRITE_LOCK = 3; 80 81 89 public ReadWriteUpgradeLock(Object resourceId, LoggerFacade logger) { 90 super(resourceId, WRITE_LOCK, logger); 91 } 92 93 107 public boolean acquireRead(Object ownerId, long timeoutMSecs) throws InterruptedException { 108 return acquire(ownerId, READ_LOCK, true, true, timeoutMSecs); 109 } 110 111 124 public boolean acquireUpgrade(Object ownerId, long timeoutMSecs) throws InterruptedException { 125 return acquire(ownerId, UPGRADE_LOCK, true, true, timeoutMSecs); 126 } 127 128 142 public boolean acquireWrite(Object ownerId, long timeoutMSecs) throws InterruptedException { 143 boolean preferred = getLockLevel(ownerId) == UPGRADE_LOCK; 145 return acquire(ownerId, WRITE_LOCK, true, COMPATIBILITY_REENTRANT, preferred, timeoutMSecs); 146 } 147 148 151 public synchronized boolean acquire(Object ownerId, int targetLockLevel, boolean wait, 152 int compatibility, boolean preferred, long timeoutMSecs) throws InterruptedException { 153 if (targetLockLevel == WRITE_LOCK && getLockLevel(ownerId) == UPGRADE_LOCK) { 154 preferred = true; 155 } 156 return super.acquire(ownerId, targetLockLevel, wait, compatibility, preferred, timeoutMSecs); 157 } 158 159 } | Popular Tags |