/** * Asynchronously runs the Proton {@link Reactor} accepting and sending messages. Any other call * to this method on this {@link AmqpsIotHubConnection} will block until the {@link Reactor} * terminates and this {@link AmqpsIotHubConnection} closes. Normally, this method should only be * called once by the {@link #open()} method until the {@link AmqpsIotHubConnection} has been * closed. * * @throws IOException if the {@link AmqpsIotHubConnectionBaseHandler} has not been initialized. * @throws InterruptedException if there is a problem acquiring the semaphore. */ private synchronized void startReactorAsync() throws IOException, InterruptedException { // Acquire permit to continue execution of this method and spawn a new thread. reactorSemaphore.acquire(); if (this.amqpsHandler != null) { this.reactor = Proton.reactor(this); new Thread( () -> { try { reactor.run(); // Closing here should be okay. The reactor will only stop running if the // connection is remotely // or locally closed. The transport will attempt to receive/send a message and // will get an exception // causing it to create a new AmqpsIoTHubConnection. close(); // Release the semaphore and make permit available allowing for the next reactor // thread to spawn. reactorSemaphore.release(); } catch (Exception e) { close(); reactorSemaphore.release(); } }) .start(); } else { throw new IOException( "The Handler has not been initialized. Ensure that the AmqpsIotHubConnection is in an OPEN state by calling open()."); } }
/** * @return if <0, then this pipe should be stopped. If ==0, it should not wait. If >0, if it * should wait */ int execute() { if (!lock.tryAcquire()) return 1; // currently being accessed Thread myThread = Thread.currentThread(); int threadIndex = -1; for (int i = 0; i < threads.length; i++) { if (threads[i] == null) { threads[i] = myThread; threadIndex = i; break; } if (myThread != threads[i]) continue; threadIndex = i; break; } Signal signal; if (threadIndex < 0) { signal = dummySignal; } else { SignalImpl s = signals[threadIndex]; s.threadIndex = threadIndex; s.signaled = false; signal = s; } boolean hasData; try { hasData = poll(signal, null); } finally { signal.signal(); if (threadIndex < 0) lock.release(); } return 0; }
public void getReady() { try { ready.acquire(); ready.release(); } catch (InterruptedException e) { e.printStackTrace(); } }
public void run() { buffer.init(); ready.release(); for (int i = 0; i < 100; i++) { buffer.put(i); } buffer.finish(); }
public void activate() { active = true; microphone.flush(); speaker.flush(); speaker.start(); blocker.release(); microphone.start(); microphone.flush(); }
@Override public boolean setConsumers(List<Consumer<S>> list, long time, TimeUnit unit) throws IllegalArgumentException { if (list == null) throw new IllegalArgumentException("consumer list is not set"); for (Consumer<S> consumer : list) { if (consumer == null) throw new IllegalArgumentException("consumer is null"); } try { if (!lock.tryAcquire(time, unit)) throw new IllegalArgumentException("can't lock producer"); } catch (InterruptedException e) { return false; } try { return setConsumers(list); } catch (InterruptedException e) { logger.warn("setting consumer", e); } finally { lock.release(); } return false; }
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(); }
/** * This should be called whenever state has changed that might cause the agent to do something. */ protected void stateChanged() { stateChange.release(); }
public void resume() { pause.release(); isPaused = false; stateChanged(); }
/** @param f Future that finished. */ private void signalTaskFinished(GridFuture<Object> f) { assert f != null; sem.release(); }
public void pickup() { pickup.release(); }
protected void publishSupport() { DHTTransport transport = dht.getTransport(); if (TESTING || !transport.isReachable()) { DHTTransportContact local_contact = transport.getLocalContact(); // see if the rendezvous has failed and therefore we are required to find a new one boolean force = rendezvous_target != null && failed_rendezvous.containsKey(rendezvous_target.getAddress()); if (rendezvous_local_contact != null && !force) { if (local_contact.getAddress().equals(rendezvous_local_contact.getAddress())) { // already running for the current local contact return; } } DHTTransportContact explicit = (DHTTransportContact) explicit_rendezvous_map.get(local_contact.getAddress()); if (explicit != null) { try { pub_mon.enter(); rendezvous_local_contact = local_contact; rendezvous_target = explicit; runRendezvous(); } finally { pub_mon.exit(); } } else { final DHTTransportContact[] new_rendezvous_target = {null}; DHTTransportContact[] reachables = dht.getTransport().getReachableContacts(); int reachables_tried = 0; int reachables_skipped = 0; final Semaphore sem = plugin_interface.getUtilities().getSemaphore(); for (int i = 0; i < reachables.length; i++) { DHTTransportContact contact = reachables[i]; try { pub_mon.enter(); // see if we've found a good one yet if (new_rendezvous_target[0] != null) { break; } // skip any known bad ones if (failed_rendezvous.containsKey(contact.getAddress())) { reachables_skipped++; sem.release(); continue; } } finally { pub_mon.exit(); } if (i > 0) { try { Thread.sleep(1000); } catch (Throwable e) { } } reachables_tried++; contact.sendPing( new DHTTransportReplyHandlerAdapter() { public void pingReply(DHTTransportContact ok_contact) { trace("Punch:" + ok_contact.getString() + " OK"); try { pub_mon.enter(); if (new_rendezvous_target[0] == null) { new_rendezvous_target[0] = ok_contact; } } finally { pub_mon.exit(); sem.release(); } } public void failed(DHTTransportContact failed_contact, Throwable e) { try { trace("Punch:" + failed_contact.getString() + " Failed"); } finally { sem.release(); } } }); } for (int i = 0; i < reachables.length; i++) { sem.reserve(); try { pub_mon.enter(); if (new_rendezvous_target[0] != null) { rendezvous_target = new_rendezvous_target[0]; rendezvous_local_contact = local_contact; log( "Rendezvous found: " + rendezvous_local_contact.getString() + " -> " + rendezvous_target.getString()); runRendezvous(); break; } } finally { pub_mon.exit(); } } if (new_rendezvous_target[0] == null) { log( "No rendezvous found: candidates=" + reachables.length + ",tried=" + reachables_tried + ",skipped=" + reachables_skipped); try { pub_mon.enter(); rendezvous_local_contact = null; rendezvous_target = null; } finally { pub_mon.exit(); } } } } else { try { pub_mon.enter(); rendezvous_local_contact = null; rendezvous_target = null; } finally { pub_mon.exit(); } } }