예제 #1
0
  /** closeFile Close the current file, but keep its path so we can easily open it again. */
  public void closeFile() throws NexusException {
    try {
      if (m_nfFile != null) {
        m_nfFile.close();
        m_nfFile.finalize();
      }
      m_nfFile = null;

    } catch (Throwable t) {
      if (g_mutex.isLocked() && g_curFile.equals(m_sFilePath)) {
        if (g_mutex.getHoldCount() - 1 == 0) {
          g_curFile = "";
        }
        g_mutex.unlock();
      }
      if (t instanceof NexusException) {
        throw (NexusException) t;
      } else {
        t.printStackTrace();
        return;
      }
    }
    if (g_mutex.isLocked() && g_curFile.equals(m_sFilePath)) {
      if (g_mutex.getHoldCount() - 1 == 0) {
        g_curFile = "";
      }

      g_mutex.unlock();
    }
  }
예제 #2
0
  public void leave() {
    ReentrantLock var1 = this.lock;

    try {
      if (var1.getHoldCount() == 1) {
        this.signalNextWaiter();
      }
    } finally {
      var1.unlock();
    }
  }
예제 #3
0
 public void writeUnLock(final Object key) {
   synchronized (locks) {
     final ReentrantLock lock = (ReentrantLock) locks.get(key);
     if (lock == null) {
       throw new IllegalMonitorStateException("Cannot unlock prior to locking");
     }
     if (lock.getHoldCount() == 0) {
       throw new IllegalMonitorStateException("Cannot unlock prior to locking");
     }
     lock.unlock();
   }
 }
예제 #4
0
 private void waitOnBufferFull() {
   if ((m_bufferCount > m_maxBufferSize) && (m_mutatorLock.getHoldCount() == 1)) {
     // try
     // {
     // System.out.println("++++++Thread Interrupt+++++++++");
     // m_writeThread.interrupt();
     // m_lockCondition.await();
     submitJob();
     // }
     // catch (InterruptedException ignored) {}
   }
 }
예제 #5
0
 public int getHoldCount() {
   return lock.getHoldCount();
 }
  public Future<AsyncQueueReadUnit> read(
      SelectionKey key,
      ByteBuffer buffer,
      AsyncReadCallbackHandler callbackHandler,
      AsyncReadCondition condition,
      AsyncQueueDataProcessor readPostProcessor)
      throws IOException {

    if (key == null) {
      throw new IOException(
          "SelectionKey is null! " + "Probably key was cancelled or connection was closed?");
    }

    FutureImpl<AsyncQueueReadUnit> future = new FutureImpl<AsyncQueueReadUnit>();

    SelectableChannel channel = (SelectableChannel) key.channel();
    AsyncQueueEntry channelEntry = readQueue.obtainAsyncQueueEntry(channel);

    // Update statistics
    channelEntry.totalElementsCount.incrementAndGet();

    AsyncQueueReadUnit record = new AsyncQueueReadUnit();

    final Queue<AsyncQueueReadUnit> queue = channelEntry.queue;
    final AtomicReference<AsyncQueueReadUnit> currentElement = channelEntry.currentElement;
    ReentrantLock lock = channelEntry.queuedActionLock;

    final int holdState = lock.getHoldCount();
    // If AsyncQueue is empty - try to read ByteBuffer here
    try {
      OperationResult dstResult = channelEntry.tmpResult;
      boolean isDirectReadCompleted = false;

      if (currentElement.get() == null
          && // Weak comparison for null
          lock.tryLock()) {
        // Strong comparison for null, because we're in locked region
        if (currentElement.compareAndSet(null, record)) {

          // Do direct reading
          do {
            dstResult = doRead((ReadableByteChannel) channel, buffer, readPostProcessor, dstResult);
            channelEntry.processedDataSize.addAndGet(dstResult.bytesProcessed);
            // If some data was read - we need to check "condition"
            // Check is performed for each message separately, not like for TCP
            if (dstResult.address != null
                && (!buffer.hasRemaining()
                    || (condition != null
                        && condition.checkAsyncReadCompleted(key, dstResult.address, buffer)))) {
              isDirectReadCompleted = true;
              break;
            }
          } while (dstResult.address != null);
        } else {
          lock.unlock();
        }
      }

      if (!isDirectReadCompleted && buffer.hasRemaining()) {
        // Update statistics
        channelEntry.queuedElementsCount.incrementAndGet();

        record.set(buffer, callbackHandler, condition, readPostProcessor, future);

        boolean isRegisterForReading = false;

        // add new element to the queue, if it's not current
        if (currentElement.get() != record) {
          queue.offer(record); // add to queue
          if (!lock.isLocked()) {
            isRegisterForReading = true;
          }
        } else { // if element was read direct (not fully read)
          isRegisterForReading = true;
          lock.unlock();
        }

        if (isRegisterForReading) {
          registerForReading(key);
        }
      } else { // If there are no bytes available for reading
        boolean isReregister = false;

        // Update statistics
        channelEntry.processedElementsCount.incrementAndGet();

        // If buffer was read directly - set next queue element as current
        if (lock.isHeldByCurrentThread()) {
          AsyncQueueReadUnit nextRecord = queue.poll();
          if (nextRecord != null) { // if there is something in queue
            currentElement.set(nextRecord);
            lock.unlock();
            isReregister = true;
          } else { // if nothing in queue
            currentElement.set(null);
            lock.unlock(); // unlock
            if (queue.peek() != null) { // check one more time
              isReregister = true;
            }
          }
        }

        // Notify callback handler
        record.set(buffer, callbackHandler, condition, readPostProcessor, future);

        future.setResult(record);

        if (callbackHandler != null) {
          callbackHandler.onReadCompleted(key, dstResult.address, record);
        }

        if (isReregister) {
          registerForReading(key);
        }
      }
    } catch (Exception e) {
      if (record.callbackHandler != null) {
        record.callbackHandler.onException(e, key, buffer, queue);
      }

      onClose(channel);

      if (e instanceof IOException) {
        throw (IOException) e;
      }
      throw new IOException(e.getMessage());
    } finally {
      if (lock.isHeldByCurrentThread() && holdState < lock.getHoldCount()) {
        lock.unlock();
      }
    }

    return future;
  }
 final boolean isStateLocked() {
   return m_stateLock.getHoldCount() > 0;
 }