if (mode == Lock.NO_LOCK) {
LOG.warn("acquired with no lock !");
return true;
}
if (Thread.interrupted())
{throw new LockException();}
Thread caller = Thread.currentThread();
synchronized (this) {
WaitingThread waitingOnResource;
if (caller == owner_) {
++holds_;
modeStack.push(Integer.valueOf(mode));
if (mode == Lock.WRITE_LOCK)
{writeLocks++;}
if (DEBUG) {
final Throwable t = new Throwable();
seStack.push(t.getStackTrace());
}
mode_ = mode;
return true;
} else if (owner_ == null) {
owner_ = caller;
holds_ = 1;
modeStack.push(Integer.valueOf(mode));
if (mode== Lock.WRITE_LOCK)
{writeLocks++;}
if (DEBUG) {
final Throwable t = new Throwable();
seStack.push(t.getStackTrace());
}
mode_ = mode;
return true;
} else if ((waitingOnResource =
DeadlockDetection.deadlockCheckResource(caller, owner_)) != null) {
waitingOnResource.suspendWaiting();
final SuspendedWaiter suspended = new SuspendedWaiter(owner_, mode_, holds_);
suspendedThreads.push(suspended);
owner_ = caller;
holds_ = 1;
modeStack.push(Integer.valueOf(mode));
if (mode== Lock.WRITE_LOCK)
{writeLocks++;}
mode_ = mode;
listener = waitingOnResource;
return true;
} else {
DeadlockDetection.addCollectionWaiter(caller, this);
try {
for (;;) {
wait(WAIT_CHECK_PERIOD);
if ((waitingOnResource = DeadlockDetection.deadlockCheckResource(caller, owner_)) != null) {
waitingOnResource.suspendWaiting();
final SuspendedWaiter suspended = new SuspendedWaiter(owner_, mode_, holds_);
suspendedThreads.push(suspended);
owner_ = caller;
holds_ = 1;
modeStack.push(Integer.valueOf(mode));
if (mode== Lock.WRITE_LOCK)
{writeLocks++;}
mode_ = mode;
listener = waitingOnResource;
DeadlockDetection.clearCollectionWaiter(owner_);
return true;
} else if (caller == owner_) {
++holds_;
modeStack.push(Integer.valueOf(mode));
if (mode == Lock.WRITE_LOCK)
{writeLocks++;}
if (DEBUG) {
final Throwable t = new Throwable();
seStack.push(t.getStackTrace());
}
mode_ = mode;
DeadlockDetection.clearCollectionWaiter(owner_);
return true;
} else if (owner_ == null) {
owner_ = caller;
holds_ = 1;
modeStack.push(Integer.valueOf(mode));
if (mode == Lock.WRITE_LOCK)
{writeLocks++;}
if (DEBUG) {
final Throwable t = new Throwable();
seStack.push(t.getStackTrace());
}
mode_ = mode;
DeadlockDetection.clearCollectionWaiter(owner_);
return true;
}
}
} catch (final InterruptedException ex) {
notify();
throw new LockException("Interrupted while waiting for lock");
}
}
}
}