Exemplo n.º 1
0
  public final boolean setSemaphore(final Transaction trans, final String name, final int timeout) {
    if (name == null) {
      throw new NullPointerException();
    }
    synchronized (_lock) {
      if (_semaphores == null) {
        _semaphores = new Hashtable4(10);
      }
    }

    final BooleanByRef acquired = new BooleanByRef();
    _semaphoresLock.run(
        new Closure4() {
          public Object run() {
            try {
              Transaction transaction = checkTransaction(trans);
              Object candidateTransaction = _semaphores.get(name);
              if (trans == candidateTransaction) {
                acquired.value = true;
                return null;
              }

              if (candidateTransaction == null) {
                _semaphores.put(name, transaction);
                acquired.value = true;
                return null;
              }

              long endtime = System.currentTimeMillis() + timeout;
              long waitTime = timeout;
              while (waitTime > 0) {
                _semaphoresLock.awake();
                _semaphoresLock.snooze(waitTime);

                if (classCollection() == null) {
                  acquired.value = false;
                  return null;
                }

                candidateTransaction = _semaphores.get(name);
                if (candidateTransaction == null) {
                  _semaphores.put(name, transaction);
                  acquired.value = true;
                  return null;
                }

                waitTime = endtime - System.currentTimeMillis();
              }

              acquired.value = false;
              return null;
            } finally {
              _semaphoresLock.awake();
            }
          }
        });

    return acquired.value;
  }
Exemplo n.º 2
0
  public final void releaseSemaphore(final Transaction trans, final String name) {
    synchronized (_lock) {
      if (_semaphores == null) {
        return;
      }
    }
    _semaphoresLock.run(
        new Closure4() {
          public Object run() {
            Transaction transaction = checkTransaction(trans);
            if (_semaphores != null && transaction == _semaphores.get(name)) {
              _semaphores.remove(name);
            }
            _semaphoresLock.awake();

            return null;
          }
        });
  }
Exemplo n.º 3
0
  public void releaseSemaphores(final Transaction trans) {
    if (_semaphores != null) {
      final Hashtable4 semaphores = _semaphores;
      _semaphoresLock.run(
          new Closure4() {
            public Object run() {
              semaphores.forEachKeyForIdentity(
                  new Visitor4() {
                    public void visit(Object a_object) {
                      semaphores.remove(a_object);
                    }
                  },
                  trans);

              _semaphoresLock.awake();
              return null;
            }
          });
    }
  }