Ejemplo n.º 1
0
  /**
   * Tries to acquire write lock for a given transaction. If <CODE>this.writeCount</CODE> is greater
   * than the currents tx's write count or the read count is greater than the currents tx's read
   * count the transaction has to wait and the {@link RagManager#checkWaitOn} method is invoked for
   * deadlock detection.
   *
   * <p>If the lock can be acquires the lock count is updated on <CODE>this</CODE> and the
   * transaction lock element (tle).
   *
   * @throws DeadlockDetectedException if a deadlock is detected
   */
  synchronized void acquireWriteLock(Object tx) throws DeadlockDetectedException {
    TxLockElement tle = getOrCreateLockElement(tx);

    try {
      tle.movedOn = false;

      boolean shouldAddWait = true;
      Thread currentThread = currentThread();

      while (totalWriteCount > tle.writeCount || totalReadCount > tle.readCount) {
        ragManager.checkWaitOn(this, tx);

        if (shouldAddWait) {
          waitingThreadList.addFirst(new WaitElement(tle, WRITE, currentThread));
        }

        try {
          wait();
          shouldAddWait = false;
        } catch (InterruptedException e) {
          interrupted();

          shouldAddWait = true;
        }
        ragManager.stopWaitOn(this, tx);
      }

      registerWriteLockAcquired(tx, tle);
    } finally {
      // if deadlocked, remove marking so lock is removed when empty
      tle.movedOn = true;
      marked--;
    }
  }
Ejemplo n.º 2
0
  /**
   * Tries to acquire write lock for a given transaction. If <CODE>this.writeCount</CODE> is greater
   * than the currents tx's write count or the read count is greater than the currents tx's read
   * count the transaction has to wait and the {@link RagManager#checkWaitOn} method is invoked for
   * deadlock detection.
   *
   * <p>If the lock can be acquires the lock count is updated on <CODE>this</CODE> and the
   * transaction lock element (tle).
   *
   * @throws DeadlockDetectedException if a deadlock is detected
   */
  synchronized void acquireWriteLock(Object tx) throws DeadlockDetectedException {
    TxLockElement tle = getOrCreateLockElement(tx);

    try {
      tle.movedOn = false;
      while (totalWriteCount > tle.writeCount || totalReadCount > tle.readCount) {
        deadlockGuardedWait(tx, tle, WRITE);
      }

      registerWriteLockAcquired(tx, tle);
    } finally {
      // if deadlocked, remove marking so lock is removed when empty
      tle.movedOn = true;
      marked--;
    }
  }
Ejemplo n.º 3
0
  synchronized boolean tryAcquireWriteLock(Object tx) {
    TxLockElement tle = getOrCreateLockElement(tx);

    try {
      tle.movedOn = false;
      if (totalWriteCount > tle.writeCount || totalReadCount > tle.readCount) {
        return false;
      }

      registerWriteLockAcquired(tx, tle);
      return true;
    } finally {
      // if deadlocked, remove marking so lock is removed when empty
      tle.movedOn = true;
      marked--;
    }
  }