public E take() throws InterruptedException {
    Node<E> x;
    final AtomicInteger count = this.count;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lockInterruptibly();
    try {
      try {
        while (count.get() == 0) notEmpty.await();
      } catch (InterruptedException ie) {
        notEmpty.signal(); // propagate to a non-interrupted thread
        throw ie;
      }

      x = extract();
      if (count.getAndDecrement() > 1) notEmpty.signal();
    } finally {
      takeLock.unlock();
    }

    E result = x.item;

    // temporary clearence
    x.item = null;
    x.next = null;

    return result;
  }
 public static void escritorSeVa(Escritor escritor) {
   lock.lock();
   System.out.println("El Escritor:" + escritor.nombre + " Termino De Escribir.");
   escritorEscribiendo = false;
   if (!cola.isEmpty()) {
     if (cola.get(0).lector) {
       lectoresLeyendo = cola.get(0).cantidad;
       System.out.println("");
       System.out.println("Se setea lectoresLeyendo = " + lectoresLeyendo);
       System.out.println("");
       for (int i = 0; i < cola.get(0).cantidad; i++) {
         lectores.signal();
       }
       cola.remove(0);
     } else {
       cola.get(0).cantidad -= 1;
       escritorEscribiendo = true;
       if (cola.get(0).cantidad == 0) {
         cola.remove(0);
       }
       escritores.signal();
     }
   }
   lock.unlock();
 }
Exemplo n.º 3
0
 /*
  * (non-Javadoc)
  *
  * @see java.io.Reader#close()
  */
 public void close() {
   lock.lock();
   notFull.signal();
   notEmpty.signal();
   size = 0;
   putIndex = 0;
   getIndex = 0;
   lock.unlock();
 }
  public Events<Event> get(Position start, int batchSize, long timeout, TimeUnit unit)
      throws InterruptedException, CanalStoreException {
    long nanos = unit.toNanos(timeout);
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
      for (; ; ) {
        if (checkUnGetSlotAt((LogPosition) start, batchSize)) {
          return doGet(start, batchSize);
        }

        if (nanos <= 0) {
          // 如果时间到了,有多少取多少
          return doGet(start, batchSize);
        }

        try {
          nanos = notEmpty.awaitNanos(nanos);
        } catch (InterruptedException ie) {
          notEmpty.signal(); // propagate to non-interrupted thread
          throw ie;
        }
      }
    } finally {
      lock.unlock();
    }
  }
Exemplo n.º 5
0
  public void shutdown() throws InterruptedException {
    if (!isShutdown.getAndSet(true)) {
      // Shut down any pending fetchers
      LOG.info(
          "Shutting down pending fetchers on source"
              + srcNameTrimmed
              + ": "
              + runningFetchers.size());
      lock.lock();
      try {
        wakeLoop.signal(); // signal the fetch-scheduler
        for (Fetcher fetcher : runningFetchers) {
          fetcher.shutdown(); // This could be parallelized.
        }
      } finally {
        lock.unlock();
      }

      if (this.schedulerExecutor != null && !this.schedulerExecutor.isShutdown()) {
        this.schedulerExecutor.shutdownNow();
      }
      if (this.fetcherExecutor != null && !this.fetcherExecutor.isShutdown()) {
        this.fetcherExecutor.shutdownNow(); // Interrupts all running fetchers.
      }
    }
    // All threads are shutdown.  It is safe to shutdown SSL factory
    if (httpConnectionParams.isSSLShuffleEnabled()) {
      HttpConnection.cleanupSSLFactory();
    }
  }
 public RunnableScheduledFuture<?> take() throws InterruptedException {
   final ReentrantLock lock = this.lock;
   lock.lockInterruptibly();
   try {
     for (; ; ) {
       RunnableScheduledFuture<?> first = queue[0];
       if (first == null) available.await();
       else {
         long delay = first.getDelay(NANOSECONDS);
         if (delay <= 0) return finishPoll(first);
         else if (leader != null) available.await();
         else {
           Thread thisThread = Thread.currentThread();
           leader = thisThread;
           try {
             available.awaitNanos(delay);
           } finally {
             if (leader == thisThread) leader = null;
           }
         }
       }
     }
   } finally {
     if (leader == null && queue[0] != null) available.signal();
     lock.unlock();
   }
 }
Exemplo n.º 7
0
  /**
   * Called when the agent <i>agName</i> has finished its reasoning cycle. <i>breakpoint</i> is true
   * in case the agent selected one plan with "breakpoint" annotation.
   */
  public void receiveFinishedCycle(String agName, boolean breakpoint, int cycle) {
    if (nbAgs < 0 || cycle != this.cycleNumber || finished.size() + 1 > nbAgs) {
      updateNumberOfAgents();
    }
    if (cycle == this.cycleNumber && runningCycle) { // the agent finished the current cycle
      lock.lock();
      try {
        if (logger.isLoggable(Level.FINE)) {
          logger.fine(
              "Agent "
                  + agName
                  + " has finished cycle "
                  + cycle
                  + ", # of finished agents is "
                  + (finished.size() + 1)
                  + "/"
                  + nbAgs);
          if (breakpoint) logger.fine("Agent " + agName + " reached a breakpoint");
        }

        finished.add(agName);
        if (testEndCycle(finished)) {
          agFinishedCond.signal();
        }
      } finally {
        lock.unlock();
      }
    }
  }
Exemplo n.º 8
0
  /*
   * (non-Javadoc)
   *
   * @see java.io.Reader#read(char[], int, int)
   */
  @Override
  public int read(char[] cbuf, int off, int len) throws IOException {
    lock.lock(); // lock this object
    int i = 0;

    try {
      // while no data to read, place thread in waiting state
      while (size == 0) {
        if (blocking) notEmpty.await(); // await until a buffer element is
        // filled
        else {
          lock.unlock();
          return -1;
        }
      } // end while
    } // end try
    // if waiting thread interrupted, print stack trace
    catch (Exception exception) {
      exception.printStackTrace();
    } // end catch

    for (; i < len && i < size; i++) {
      cbuf[off + i] = (char) getByte();
    }
    notFull.signal();
    lock.unlock(); // unlock this object

    return i;
  }
Exemplo n.º 9
0
  /**
   * Close this Waiter so that no more errors are processed. This is a preventative method to ensure
   * that a second error thread does not get stuck in the error method after the await has returned.
   * This has not happened but in practise but if two errors occur on the Connection at the same
   * time then it is conceivably possible for the second to get stuck if the first one is processed
   * by a waiter.
   *
   * <p>Once closed any attempt to wait will throw an exception. Any notification of an exception
   * will be ignored.
   */
  public void close() {
    _lock.lock();
    try {
      // if we have already closed then our job is done.
      if (_closed) {
        return;
      }

      // Close Waiter so no more exceptions are processed
      _closed = true;

      // Wake up any await() threads

      // If we are waiting then use the error() to wake them up.
      if (_waiting.get()) {
        error(throwClosedException());
      }
      // If they are not waiting then there is nothing to do.

      // Wake up any error handling threads

      if (!_errorAck) {
        _errorAck = true;
        _errorConditionAck.signal();

        _error = null;
      }
    } finally {
      _lock.unlock();
    }
  }
Exemplo n.º 10
0
  /**
   * Get bytes from the buffer and return these in an array. Blocks if the buffer is empty and no
   * values have yet been added to the array.
   *
   * @param buf array to return bytes
   * @param off the off
   * @param len the len
   * @return the number of bytes returned in the array
   */
  public int get(byte[] buf, int off, int len) {
    lock.lock(); // lock this object
    int i = 0;

    try {
      // while no data to read, place thread in waiting state
      while (size == 0) {
        // System.out.println("read wait");
        notEmpty.await(); // await until a buffer element is
        // filled
      } // end while
    } // end try
    // if waiting thread interrupted, print stack trace
    catch (Exception exception) {
      exception.printStackTrace();
    } // end catch

    for (; i < len && i < size; i++) {
      buf[off + i] = getByte();
    }
    notFull.signal();
    lock.unlock(); // unlock this object

    return i;
  }
  public boolean put(List<Event> data, long timeout, TimeUnit unit)
      throws InterruptedException, CanalStoreException {
    if (data == null || data.isEmpty()) {
      return true;
    }

    long nanos = unit.toNanos(timeout);
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
      for (; ; ) {
        if (checkFreeSlotAt(putSequence.get() + data.size())) {
          doPut(data);
          return true;
        }
        if (nanos <= 0) {
          return false;
        }

        try {
          nanos = notFull.awaitNanos(nanos);
        } catch (InterruptedException ie) {
          notFull.signal(); // propagate to non-interrupted thread
          throw ie;
        }
      }
    } finally {
      lock.unlock();
    }
  }
Exemplo n.º 12
0
 /**
  * Retrieves and removes the head of this queue, waiting if necessary until an element with an
  * expired delay is available on this queue.
  *
  * @return the head of this queue
  * @throws InterruptedException {@inheritDoc}
  */
 public E take() throws InterruptedException {
   final ReentrantLock lock = this.lock;
   lock.lockInterruptibly();
   try {
     for (; ; ) {
       E first = q.peek();
       if (first == null) available.await();
       else {
         long delay = first.getDelay(TimeUnit.NANOSECONDS);
         if (delay <= 0) return q.poll();
         else if (leader != null) available.await();
         else {
           Thread thisThread = Thread.currentThread();
           leader = thisThread;
           try {
             available.awaitNanos(delay);
           } finally {
             if (leader == thisThread) leader = null;
           }
         }
       }
     }
   } finally {
     if (leader == null && q.peek() != null) available.signal();
     lock.unlock();
   }
 }
Exemplo n.º 13
0
  /**
   * Get next value from the buffer. Blocks indefinitely if buffer is empty.
   *
   * @return the byte
   */
  public byte get() {
    byte result = 0; // initialize value read from buffer
    lock.lock(); // lock this object

    // wait until buffer has data, then read value
    try {
      // while no data to read, place thread in waiting state
      while (size == 0) {
        if (blocking) notEmpty.await(); // await until a buffer element is
        // filled
        else {
          lock.unlock();
          return 0;
        }
      } // end while

      result = getByte();
      notFull.signal();
    } // end try
    // if waiting thread interrupted, print stack trace
    catch (InterruptedException exception) {
      exception.printStackTrace();
      Thread.currentThread().interrupt();
    } // end catch
    finally {
      lock.unlock(); // unlock this object
    } // end finally

    return result;
  } // end method get
Exemplo n.º 14
0
  public void handlePacket(final Packet packet) {
    if (packet.getType() == PacketImpl.PACKETS_CONFIRMED) {
      if (resendCache != null) {
        final PacketsConfirmedMessage msg = (PacketsConfirmedMessage) packet;

        clearUpTo(msg.getCommandID());
      }

      if (!connection.isClient()) {
        handler.handlePacket(packet);
      }

      return;
    } else {
      if (packet.isResponse()) {
        confirm(packet);

        lock.lock();

        try {
          response = packet;
          sendCondition.signal();
        } finally {
          lock.unlock();
        }
      } else if (handler != null) {
        handler.handlePacket(packet);
      }
    }
  }
  public E poll() {
    final AtomicInteger count = this.count;
    if (count.get() == 0) return null;

    Node<E> x = null;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();

    try {
      if (count.get() > 0) {
        x = extract();
        if (count.getAndDecrement() > 1) notEmpty.signal();
      }
    } finally {
      takeLock.unlock();
    }

    if (x != null) {
      E result = x.item;

      // temporary clearence
      x.item = null;
      x.next = null;

      return result;
    }

    return null;
  }
Exemplo n.º 16
0
 public void addKnownInput(
     String hostName,
     int port,
     InputAttemptIdentifier srcAttemptIdentifier,
     int srcPhysicalIndex) {
   String identifier = InputHost.createIdentifier(hostName, port);
   InputHost host = knownSrcHosts.get(identifier);
   if (host == null) {
     host = new InputHost(hostName, port, inputContext.getApplicationId(), srcPhysicalIndex);
     assert identifier.equals(host.getIdentifier());
     InputHost old = knownSrcHosts.putIfAbsent(identifier, host);
     if (old != null) {
       host = old;
     }
   }
   if (LOG.isDebugEnabled()) {
     LOG.debug("Adding input: " + srcAttemptIdentifier + ", to host: " + host);
   }
   host.addKnownInput(srcAttemptIdentifier);
   lock.lock();
   try {
     boolean added = pendingHosts.offer(host);
     if (!added) {
       String errorMessage = "Unable to add host: " + host.getIdentifier() + " to pending queue";
       LOG.error(errorMessage);
       throw new TezUncheckedException(errorMessage);
     }
     wakeLoop.signal();
   } finally {
     lock.unlock();
   }
 }
Exemplo n.º 17
0
  // 消息处理
  public void onEventMainThread(GetLastMonitorInfoEven even) {

    lock.lock();
    carId = even.getCarId();
    condition.signal();
    lock.unlock();
  }
 public RunnableScheduledFuture<?> poll(long timeout, TimeUnit unit)
     throws InterruptedException {
   long nanos = unit.toNanos(timeout);
   final ReentrantLock lock = this.lock;
   lock.lockInterruptibly();
   try {
     for (; ; ) {
       RunnableScheduledFuture<?> first = queue[0];
       if (first == null) {
         if (nanos <= 0) return null;
         else nanos = available.awaitNanos(nanos);
       } else {
         long delay = first.getDelay(NANOSECONDS);
         if (delay <= 0) return finishPoll(first);
         if (nanos <= 0) return null;
         if (nanos < delay || leader != null) nanos = available.awaitNanos(nanos);
         else {
           Thread thisThread = Thread.currentThread();
           leader = thisThread;
           try {
             long timeLeft = available.awaitNanos(delay);
             nanos -= delay - timeLeft;
           } finally {
             if (leader == thisThread) leader = null;
           }
         }
       }
     }
   } finally {
     if (leader == null && queue[0] != null) available.signal();
     lock.unlock();
   }
 }
Exemplo n.º 19
0
  /**
   * Blocks until an object is received that is handled by process, or the specified timeout has
   * passed.
   *
   * <p>Once closed any attempt to wait will throw an exception.
   *
   * @param timeout The timeout in milliseconds.
   * @return The object that resolved the blocking.
   * @throws QpidException
   * @throws FailoverException
   */
  public Object block(long timeout) throws QpidException, FailoverException {
    if (timeout < 0) {
      throw new IllegalArgumentException("timeout must be zero or greater");
    }

    long nanoTimeout = TimeUnit.MILLISECONDS.toNanos(timeout);

    _lock.lock();

    try {
      if (_closed) {
        throw throwClosedException();
      }

      if (_error == null) {
        _waiting.set(true);

        while (!_ready && _error == null) {
          try {
            nanoTimeout = _receivedCondition.awaitNanos(nanoTimeout);

            if (nanoTimeout <= 0 && !_ready && _error == null) {
              _error = new AMQTimeoutException("Server did not respond in a timely fashion", null);
              _ready = true;
            }
          } catch (InterruptedException e) {
            _logger.error(e.getMessage(), e);
            // IGNORE    -- //fixme this isn't ideal as being interrupted isn't equivalent to
            // success
          }
        }
      }

      if (_error != null) {
        if (_error instanceof QpidException) {
          throw (QpidException) _error;
        } else if (_error instanceof FailoverException) {
          // This should ensure that FailoverException is not wrapped and can be caught.
          throw (FailoverException) _error; // needed to expose FailoverException.
        } else {
          throw new QpidException("Woken up due to " + _error.getClass(), _error);
        }
      }

    } finally {
      _waiting.set(false);

      // Release Error handling thread
      if (_error != null) {
        _errorAck = true;
        _errorConditionAck.signal();

        _error = null;
      }
      _lock.unlock();
    }

    return _doneObject;
  }
Exemplo n.º 20
0
  @Override
  public void fetchSucceeded(
      String host,
      InputAttemptIdentifier srcAttemptIdentifier,
      FetchedInput fetchedInput,
      long fetchedBytes,
      long decompressedLength,
      long copyDuration)
      throws IOException {
    InputIdentifier inputIdentifier = srcAttemptIdentifier.getInputIdentifier();

    LOG.info(
        "Completed fetch for attempt: " + srcAttemptIdentifier + " to " + fetchedInput.getType());

    // Count irrespective of whether this is a copy of an already fetched input
    lock.lock();
    try {
      lastProgressTime = System.currentTimeMillis();
    } finally {
      lock.unlock();
    }

    boolean committed = false;
    if (!completedInputSet.contains(inputIdentifier)) {
      synchronized (completedInputSet) {
        if (!completedInputSet.contains(inputIdentifier)) {
          fetchedInput.commit();
          committed = true;

          // Processing counters for completed and commit fetches only. Need
          // additional counters for excessive fetches - which primarily comes
          // in after speculation or retries.
          shuffledInputsCounter.increment(1);
          bytesShuffledCounter.increment(fetchedBytes);
          if (fetchedInput.getType() == Type.MEMORY) {
            bytesShuffledToMemCounter.increment(fetchedBytes);
          } else {
            bytesShuffledToDiskCounter.increment(fetchedBytes);
          }
          decompressedDataSizeCounter.increment(decompressedLength);

          registerCompletedInput(fetchedInput);
        }
      }
    }
    if (!committed) {
      fetchedInput.abort(); // If this fails, the fetcher may attempt another abort.
    } else {
      lock.lock();
      try {
        // Signal the wakeLoop to check for termination.
        wakeLoop.signal();
      } finally {
        lock.unlock();
      }
    }
    // TODO NEWTEZ Maybe inform fetchers, in case they have an alternate attempt of the same task in
    // their queue.
  }
Exemplo n.º 21
0
 private void enqueue(T t) {
   queue[tail] = t;
   if (++tail == getCapacity()) {
     tail = 0;
   }
   notEmpty.signal();
   count++;
 }
Exemplo n.º 22
0
 void signalSimSentApps() {
   try {
     simLock.lock();
     simSentApps.signal();
   } finally {
     simLock.unlock();
   }
 }
Exemplo n.º 23
0
 void signalSimRegistered() {
   try {
     simLock.lock();
     simRegistered.signal();
   } finally {
     simLock.unlock();
   }
 }
Exemplo n.º 24
0
 public void finish() {
   runningLock.lock();
   running = false;
   runningLock.unlock();
   waitingLock.lock();
   waitingCondition.signal();
   waitingLock.unlock();
 }
 /**
  * Enqueues the given request as priority Request, and wakes up the WorkerThread for immediate
  * processing
  *
  * @param request
  */
 private void EnqueuePriorityRequest(Request request) {
   try {
     _requestLock.lock();
     _priorityQueue.add(request);
     _requestCond.signal();
   } finally {
     _requestLock.unlock();
   }
 }
Exemplo n.º 26
0
 void setConfigDialog(JDialog configDialog) {
   lock.lock();
   try {
     mConfigDialog = configDialog;
     gotConfigDialog.signal();
   } finally {
     lock.unlock();
   }
 }
 /**
  * Signals a waiting take. Called only from put/offer (which do not otherwise ordinarily lock
  * takeLock.)
  */
 private void signalNotEmpty() {
   final ReentrantLock takeLock = this.takeLock;
   takeLock.lock();
   try {
     notEmpty.signal();
   } finally {
     takeLock.unlock();
   }
 }
Exemplo n.º 28
0
 /**
  * Extracts element at current take position, advances, and signals. Call only when holding lock.
  */
 private E extract() {
   final E[] items = this.items;
   E x = items[takeIndex];
   items[takeIndex] = null;
   takeIndex = inc(takeIndex);
   --count;
   notFull.signal();
   return x;
 }
 private void signal(Lock lock, Condition condition, Runnable runnable) {
   lock.lock();
   try {
     runnable.run();
     condition.signal();
   } finally {
     lock.unlock();
   }
 }
Exemplo n.º 30
0
 public void put(T x) {
   lock.lock();
   try {
     item = x;
     if (x != null) notEmpty.signal();
   } finally {
     lock.unlock();
   }
 }