Refine search
public boolean isWriteResumed() { final int state = this.state; return allAreSet(state, FLAG_RESUMED) && allAreClear(state, FLAG_CLOSED); }
public void suspendWrites() { int oldVal, newVal; do { oldVal = state; if (allAreClear(oldVal, FLAG_RESUMED) || allAreSet(oldVal, FLAG_CLOSED)) { return; } newVal = oldVal & ~FLAG_RESUMED; } while (! stateUpdater.compareAndSet(this, oldVal, newVal)); }
public void shutdownReads() throws IOException { final int state = this.state; if (allAreClear(state, FLAG_CLOSED)) try { this.state = state | FLAG_CLOSED | FLAG_FINISHED; if (allAreSet(state, FLAG_PASS_CLOSE)) { delegate.shutdownReads(); } } finally { if (allAreClear(state, FLAG_FINISHED)) callFinish(); callClosed(); } }
public void suspendReads() { int state = this.state; try { if (allAreSet(state, READ_FLAG_RESUMED)) { state &= ~READ_FLAG_RESUMED; if (allAreSet(state, READ_FLAG_UP_RESUMED) && allAreClear(state, WRITE_FLAG_NEEDS_READ)) { // upstream reads were resumed but now may be suspended state &= ~READ_FLAG_UP_RESUMED; sourceConduit.suspendReads(); } if (allAreSet(state, WRITE_FLAG_UP_RESUMED) && allAreClear(state, WRITE_FLAG_RESUMED)) { // writes were likely resumed because of us; clear it state &= ~WRITE_FLAG_UP_RESUMED; sinkConduit.suspendWrites(); } } } finally { this.state = state; } }
public void suspendWrites() { int state = this.state; try { if (allAreSet(state, WRITE_FLAG_RESUMED)) { state &= ~WRITE_FLAG_RESUMED; if (allAreSet(state, WRITE_FLAG_UP_RESUMED) && allAreClear(state, READ_FLAG_NEEDS_WRITE)) { // upstream writes were resumed but now may be suspended state &= ~WRITE_FLAG_UP_RESUMED; sinkConduit.suspendWrites(); } if (allAreSet(state, READ_FLAG_UP_RESUMED) && allAreClear(state, READ_FLAG_RESUMED)) { // reads were likely resumed because of us; clear it state &= ~READ_FLAG_UP_RESUMED; sourceConduit.suspendReads(); } } } finally { this.state = state; } }
public void resumeReads() { int state = this.state; boolean queueTask = false; if (allAreClear(state, READ_FLAG_RESUMED)) try { state |= READ_FLAG_RESUMED; // in the absence of a writer, we need to wake up the reader to make sure that handshake will take place // (the read listener could never be called if this is the side that is supposed to make the first wrap // for handshake; if that happens, this side of the connection would starve) if (allAreClear(state, WRITE_FLAG_RESUMED)) { state |= READ_FLAG_READY; } if (allAreSet(state, READ_FLAG_READY)) { if (queueTask = allAreClear(state, FLAG_TASK_QUEUED)) { state |= FLAG_TASK_QUEUED; } } else if (allAreSet(state, READ_FLAG_NEEDS_WRITE) && allAreClear(state, WRITE_FLAG_UP_RESUMED)) { // need to resume writes to make this happen state |= WRITE_FLAG_UP_RESUMED; sinkConduit.resumeWrites(); } else if (allAreClear(state, READ_FLAG_UP_RESUMED)) { // upstream reads were not resumed state |= READ_FLAG_UP_RESUMED; sourceConduit.resumeReads(); } } finally { this.state = state; // execute this on read thread only after updating the state if (queueTask) getReadThread().execute(this); } }
/** * Indicate that the channel no longer requires writability for reads to proceed. */ protected void clearReadRequiresWrite() { int oldState = clearFlags(READ_REQUIRES_WRITE); if (allAreClear(oldState, READ_REQUIRES_WRITE)) { // idempotent return; } if (allAreClear(oldState, READ_REQUIRES_EXT) && allAreSet(oldState, READ_REQUESTED)) { if (allAreSet(oldState, READ_READY)) { channel.wakeupReads(); } else { channel.resumeReads(); } } }
/** * Indicate that the channel no longer requires writability for writes to proceed. */ protected void clearWriteRequiresRead() { int oldState = clearFlags(WRITE_REQUIRES_READ); if (allAreClear(oldState, WRITE_REQUIRES_READ)) { // idempotent return; } if (allAreClear(oldState, WRITE_REQUIRES_EXT) && allAreSet(oldState, WRITE_REQUESTED)) { if (allAreSet(oldState, WRITE_READY)) { channel.wakeupWrites(); } else { channel.resumeWrites(); } } }
public int writeFinal(final ByteBuffer src) throws IOException { if (allAreSet(state, WRITE_FLAG_SHUTDOWN)) { throw new ClosedChannelException(); } if (allAreClear(state, FLAG_TLS)) { return sinkConduit.writeFinal(src); } else { final ByteBuffer[] writeBufferHolder = this.writeBufferHolder; writeBufferHolder[0] = src; try { return (int) writeFinal(writeBufferHolder, 0, 1); } finally { writeBufferHolder[0] = null; } } }
/** * Indicate that the channel will not be writable until the read handler is called. */ protected void setWriteRequiresRead() { int oldState = setFlags(WRITE_REQUIRES_READ); if (allAreSet(oldState, WRITE_REQUIRES_READ)) { // not the first caller return; } if (allAreClear(oldState, WRITE_READY | WRITE_REQUIRES_EXT)) { // write cannot proceed until read does channel.resumeReads(); } }
public int write(final ByteBuffer src) throws IOException { if (allAreSet(state, WRITE_FLAG_SHUTDOWN)) { throw new ClosedChannelException(); } if (allAreClear(state, FLAG_TLS)) { return sinkConduit.write(src); } else { final ByteBuffer[] writeBufferHolder = this.writeBufferHolder; writeBufferHolder[0] = src; try { return (int) write(writeBufferHolder, 0, 1); } finally { writeBufferHolder[0] = null; } } }
/** * Indicate that the channel will not be readable until the write handler is called. */ protected void setReadRequiresWrite() { int oldState = setFlags(READ_REQUIRES_WRITE); if (allAreSet(oldState, READ_REQUIRES_WRITE)) { // not the first caller return; } if (allAreClear(oldState, READ_READY | READ_REQUIRES_EXT)) { // read cannot proceed until write does channel.resumeWrites(); } }
public void awaitWritable() throws IOException { int state = this.state; while (allAreSet(state, FLAG_NEED_ENGINE_TASK)) { synchronized (this) { while (tasks != 0) { try { wait(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new InterruptedIOException(); } } state &= ~FLAG_NEED_ENGINE_TASK; this.state = state; } } if (allAreClear(state, WRITE_FLAG_READY)) { if (allAreSet(state, WRITE_FLAG_NEEDS_READ)) { sourceConduit.awaitReadable(); } else { sinkConduit.awaitWritable(); } } }
public void awaitReadable() throws IOException { int state = this.state; while (allAreSet(state, FLAG_NEED_ENGINE_TASK)) { synchronized (this) { while (tasks != 0) { try { wait(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new InterruptedIOException(); } } state &= ~FLAG_NEED_ENGINE_TASK; this.state = state; } } if (allAreClear(state, READ_FLAG_READY)) { if (allAreSet(state, READ_FLAG_NEEDS_WRITE)) { sinkConduit.awaitWritable(); } else { sourceConduit.awaitReadable(); } } }
public boolean flush() throws IOException { int oldVal, newVal; oldVal = state; if (allAreSet(oldVal, FLAG_CLOSE_COMP)) { return true; } final boolean flushed = delegate.flush(); if (! flushed) { return false; } do { if (allAreClear(oldVal, FLAG_CLOSE_REQ)) { return true; } newVal = oldVal | FLAG_CLOSE_COMP; } while (! stateUpdater.compareAndSet(this, oldVal, newVal)); invokeChannelListener(this, closeListener); return true; }
public boolean awaitTermination(final long timeout, final TimeUnit unit) throws InterruptedException { int oldState = state; if (Bits.allAreSet(oldState, CLOSE_COMP)) { return true; } long then = System.nanoTime(); long duration = unit.toNanos(timeout); final Thread myThread = Thread.currentThread(); while (Bits.allAreClear(oldState = state, CLOSE_COMP)) { final Thread oldThread = shutdownWaiterUpdater.getAndSet(this, myThread); try { if (Bits.allAreSet(oldState = state, CLOSE_COMP)) { break; } LockSupport.parkNanos(this, duration); if (Thread.interrupted()) { throw new InterruptedException(); } long now = System.nanoTime(); duration -= now - then; if (duration < 0L) { oldState = state; break; } } finally { safeUnpark(oldThread); } } return Bits.allAreSet(oldState, CLOSE_COMP); }
public void resumeWrites() { int state = this.state; if (allAreClear(state, WRITE_FLAG_RESUMED)) { if (allAreSet(state, WRITE_FLAG_FINISHED)) { try { state |= WRITE_FLAG_RESUMED; if (allAreSet(state, WRITE_FLAG_READY)) { if (queueTask = allAreClear(state, FLAG_TASK_QUEUED)) { state |= FLAG_TASK_QUEUED; } else if (allAreSet(state, WRITE_FLAG_NEEDS_READ) && allAreClear(state, READ_FLAG_UP_RESUMED)) { } else if (allAreClear(state, WRITE_FLAG_UP_RESUMED)) {
public void awaitTermination() throws InterruptedException { int oldState = state; if (Bits.allAreSet(oldState, CLOSE_COMP)) { return; } final Thread myThread = Thread.currentThread(); while (Bits.allAreClear(state, CLOSE_COMP)) { final Thread oldThread = shutdownWaiterUpdater.getAndSet(this, myThread); try { if (Bits.allAreSet(state, CLOSE_COMP)) { break; } LockSupport.park(this); if (Thread.interrupted()) { throw new InterruptedException(); } } finally { safeUnpark(oldThread); } } }
/** * Called by the upstream conduit when writes are ready. */ public void writeReady() { int state = this.state; state |= WRITE_FLAG_READY; if (allAreSet(state, READ_FLAG_NEEDS_WRITE)) { state |= READ_FLAG_READY; } this.state = state; // avoid double-fire if (allAreClear(state, FLAG_TASK_QUEUED)) { run(); } state = this.state; if (sinkConduit.isWriteResumed() && allAreClear(state, WRITE_FLAG_RESUMED | READ_FLAG_NEEDS_WRITE)) { sinkConduit.suspendWrites(); } if (sourceConduit.isReadResumed() && allAreClear(state, READ_FLAG_RESUMED | WRITE_FLAG_NEEDS_READ)) { sourceConduit.suspendReads(); } }
/** * Called by the upstream conduit when reads are ready. */ public void readReady() { int state = this.state; state |= READ_FLAG_READY; if (allAreSet(state, WRITE_FLAG_NEEDS_READ)) { state |= WRITE_FLAG_READY; } this.state = state; // avoid double-fire if (allAreClear(state, FLAG_TASK_QUEUED)) { run(); } state = this.state; if (sourceConduit.isReadResumed() && allAreClear(state, READ_FLAG_RESUMED | WRITE_FLAG_NEEDS_READ)) { sourceConduit.suspendReads(); } if (sinkConduit.isWriteResumed() && allAreClear(state, WRITE_FLAG_RESUMED | READ_FLAG_NEEDS_WRITE)) { sinkConduit.suspendWrites(); } }