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 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 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 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(); } } }
/** * 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 boolean flush() throws IOException { int state = this.state; if (allAreSet(state, WRITE_FLAG_FINISHED)) { return true; } else if (allAreSet(state, WRITE_FLAG_SHUTDOWN3)) { // just waiting for upstream flush if (sinkConduit.flush()) { this.state = state | WRITE_FLAG_FINISHED; return true; } else { return false; } } else if (allAreClear(state, FLAG_TLS)) { final boolean flushed = sinkConduit.flush(); if (allAreSet(state, WRITE_FLAG_SHUTDOWN) && flushed) { this.state = state | WRITE_FLAG_SHUTDOWN2 | WRITE_FLAG_SHUTDOWN3 | WRITE_FLAG_FINISHED; } return flushed; } else if (allAreSet(state, WRITE_FLAG_SHUTDOWN)) { // waiting for final wrap, then upstream shutdown & flush return performIO(IO_GOAL_FLUSH, NO_BUFFERS, 0, 0, NO_BUFFERS, 0, 0) != 0L; } else { // regular flush return performIO(IO_GOAL_FLUSH, NO_BUFFERS, 0, 0, NO_BUFFERS, 0, 0) != 0L; } }
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; } } }
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; } } }
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); } } }
/** * 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(); } }
/** * 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 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 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 long transferFrom(final StreamSourceChannel source, final long count, final ByteBuffer throughBuffer) throws IOException { if (count == 0L) return 0L; long val = state; if (allAreSet(val, FLAG_CLOSE_REQUESTED)) { throw new ClosedChannelException(); } if (allAreClear(val, MASK_COUNT)) { throw new FixedLengthOverflowException(); } long res = 0L; try { return res = next.transferFrom(source, min(count, (val & MASK_COUNT)), throughBuffer); } catch (IOException | RuntimeException | Error e) { broken = true; throw e; } finally { exitWrite(val, res); } }
public long transferFrom(final FileChannel src, final long position, final long count) throws IOException { if (count == 0L) return 0L; long val = state; if (allAreSet(val, FLAG_CLOSE_REQUESTED)) { throw new ClosedChannelException(); } if (allAreClear(val, MASK_COUNT)) { throw new FixedLengthOverflowException(); } long res = 0L; try { return res = next.transferFrom(src, position, min(count, (val & MASK_COUNT))); } catch (IOException | RuntimeException | Error e) { broken = true; throw e; } finally { exitWrite(val, res); } }
public void run() { ChannelListener<? super EmptyStreamSourceChannel> listener = readListener; if (listener == null) { suspendReads(); return; } ChannelListeners.invokeChannelListener(EmptyStreamSourceChannel.this, listener); final int oldVal = state; if (allAreSet(oldVal, RESUMED) && allAreClear(oldVal, EMPTIED | CLOSED)) { thread.execute(this); } } };