예제 #1
0
 private void add(PooledFatPipe child) {
   long start = System.currentTimeMillis();
   while (true) {
     int fails = 0;
     for (int i = 0; i < inUse.length(); i++) {
       if (!inUse.compareAndSet(i, 0, 2)) {
         if (inUse.get(i) != 2) fails++;
       }
     }
     if (fails == 0) break;
     if (start + 100 < System.currentTimeMillis()) {
       logger.warn("unable to add cleanly " + toString());
       break;
     }
   }
   final AtomicIntegerArray updated = new AtomicIntegerArray(inUse.length() + 1);
   final List<Integer> needsReset = new ArrayList<>();
   for (int i = 0; i < inUse.length(); i++) {
     int value = inUse.get(i);
     if (value == 1) {
       updated.set(i, value);
       needsReset.add(i);
     }
   }
   final AtomicIntegerArray oldUse = inUse;
   inUse = updated;
   signals = new ChildSignalImpl[signals.length + poolSize];
   for (int i = 0; i < signals.length; i++) {
     signals[i] = new ChildSignalImpl(inUse, 0);
   }
   children = Arrays.copyOf(children, children.length + 1);
   children[children.length - 1] = child;
   scheduleReset(needsReset, oldUse, updated);
 }
예제 #2
0
 @Override
 public boolean setIfEmpty(T data, long time, TimeUnit units) {
   long end = System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(time, units);
   while (!setIfEmpty(data)) {
     if (end < System.currentTimeMillis()) return false;
   }
   return true;
 }
예제 #3
0
 @Override
 public synchronized Receptor<T> takeReceptor() {
   if (!freeReceptors.isEmpty()) {
     ReceptorImpl impl = (ReceptorImpl) freeReceptors.remove(0);
     impl.skips = 0;
     return impl;
   }
   int length = inputs.length() + 1;
   final AtomicReferenceArray<T> inputs = new AtomicReferenceArray<>(length);
   this.inputs = inputs;
   final List<ReceptorImpl> newReuseReceptors = new ArrayList<>(length);
   for (int i = 0; i < length; i++) {
     newReuseReceptors.add(new ReceptorImpl(i));
   }
   executor.execute(
       () -> {
         long lastTime = System.currentTimeMillis();
         try {
           boolean empty = false;
           while (!empty && started) {
             empty = true;
             for (int i = 0; i < outputs.length(); i++) {
               T result = outputs.get(i);
               if (result == null) continue;
               Thread.sleep(1);
               empty = false;
               if (lastTime + 30_000 < System.currentTimeMillis()) {
                 lastTime = System.currentTimeMillis();
                 logger.error(
                     "unable to set receptors for " + producer + " i=" + i + " result=" + result);
                 if (pool != null) logger.warn("pool=" + pool.producer);
               }
               break;
             }
           }
         } catch (InterruptedException e) {
           logger.warn("takeReceptor", e);
         } finally {
           reuseReceptors = newReuseReceptors;
           outputs = inputs;
         }
       });
   return new ReceptorImpl(length - 1);
 }
예제 #4
0
 Consumer<S> check(long maxTime, PooledFatPipe parent) {
   if (parent != null && parent.group == null) return null;
   AtomicLongArray outUse = this.outUse;
   Consumer<S>[] consumers = this.consumers;
   long now = System.nanoTime();
   if (consumers.length > outUse.length()) {
     logger.warn("skipping check " + outUse.length() + "/" + consumers.length);
     return null;
   }
   try {
     for (int i = 0; i < consumers.length; i++) {
       long time = outUse.get(i);
       if (time == 0) continue;
       if (time == -1) continue;
       if (now < time + maxTime * 1_000_000) continue;
       if (parent != null) parent.group.list();
       return consumers[i];
     }
   } catch (Exception e) {
     logger.error("check", e);
   }
   return null;
 }
예제 #5
0
  public void start() {
    inputs = new AtomicReferenceArray<>(0);
    outputs = inputs;
    freeReceptors = new CopyOnWriteArrayList<>();
    sequence = 0;
    finalSequence = 0;
    finalProduct = null;
    reuseReceptors = new ArrayList<>(0);
    executor = Executors.newSingleThreadExecutor();

    started = true;
    if (poolSize > 0) {
      group = new ThreadGroup("FatPipe");
      group.setMaxPriority(Thread.MAX_PRIORITY);
    }
    signals = new SignalImpl[ARBITARY_THREADS + poolSize];
    threads = new Thread[ARBITARY_THREADS + poolSize];
    startTime = System.currentTimeMillis();
    sleptTime = 0;
    toSleepTime = 0;

    for (int i = 0; i < poolSize; i++) {
      Thread t = new Thread(group, this, name + "-" + i);
      t.setDaemon(true);
      threads[i] = t;
      t.start();
      if (sleep == 0) t.setPriority(Thread.MAX_PRIORITY);
      else if (sleep < 0) t.setPriority(Thread.NORM_PRIORITY);
      else t.setPriority(Thread.MIN_PRIORITY);
    }
    for (int i = 0; i < signals.length; i++) {
      signals[i] = new SignalImpl(lock);
    }
    if (pool != null) pool.add(this);
    lock.release();
  }
예제 #6
-1
  /**
   * @param product
   * @param sequence
   * @return false if there is an error
   */
  boolean consume(S product, long sequence, long sleep) {
    if (product == null) return true;

    // make copies
    Consumer<S>[] consumers = this.consumers;
    AtomicLongArray outUse = this.outUse;
    long[] consumerSeqs = this.consumerSeqs;

    if (outUse.length() != consumers.length) return false;
    for (int j = 0; j < consumers.length; j++) {
      if (!consumers[j].isConsuming()) continue;
      long time = System.nanoTime();
      if (!outUse.compareAndSet(j, 0, time)) continue;
      try {
        if (sequence <= consumerSeqs[j]) {
          outUse.lazySet(j, 0);
          if (outUse != this.outUse) resetConsumer(consumers[j]);
          break;
        }
        consumerSeqs[j] = sequence;
        consumers[j].consume(product, time);
        if (sleep > 0) Thread.sleep(sleep);
        outUse.lazySet(j, 0);
        if (outUse != this.outUse) {
          resetConsumer(consumers[j]);
          break;
        }
      } catch (Exception e) {
        if (listener == null) logger.error("consume", e);
        else listener.exceptionThrown(e);
      }
    }
    finishConsuming(product, sequence);
    return true;
  }