Exemplo n.º 1
0
  static void setInterestOps(OioDatagramChannel channel, ChannelFuture future, int interestOps) {

    // Override OP_WRITE flag - a user cannot change this flag.
    interestOps &= ~Channel.OP_WRITE;
    interestOps |= channel.getInterestOps() & Channel.OP_WRITE;

    boolean changed = false;
    try {
      if (channel.getInterestOps() != interestOps) {
        if ((interestOps & Channel.OP_READ) != 0) {
          channel.setInterestOpsNow(Channel.OP_READ);
        } else {
          channel.setInterestOpsNow(Channel.OP_NONE);
        }
        changed = true;
      }

      future.setSuccess();
      if (changed) {
        synchronized (channel.interestOpsLock) {
          channel.setInterestOpsNow(interestOps);

          // Notify the worker so it stops or continues reading.
          Thread currentThread = Thread.currentThread();
          Thread workerThread = channel.workerThread;
          if (workerThread != null && currentThread != workerThread) {
            workerThread.interrupt();
          }
        }

        fireChannelInterestChanged(channel);
      }
    } catch (Throwable t) {
      future.setFailure(t);
      fireExceptionCaught(channel, t);
    }
  }
Exemplo n.º 2
0
  static void setInterestOps(NioSocketChannel channel, ChannelFuture future, int interestOps) {
    boolean changed = false;
    try {
      // interestOps can change at any time and at any thread.
      // Acquire a lock to avoid possible race condition.
      synchronized (channel.interestOpsLock) {
        NioWorker worker = channel.worker;
        Selector selector = worker.selector;
        SelectionKey key = channel.socket.keyFor(selector);

        if (key == null || selector == null) {
          // Not registered to the worker yet.
          // Set the rawInterestOps immediately; RegisterTask will pick it up.
          channel.setRawInterestOpsNow(interestOps);
          return;
        }

        // Override OP_WRITE flag - a user cannot change this flag.
        interestOps &= ~Channel.OP_WRITE;
        interestOps |= channel.getRawInterestOps() & Channel.OP_WRITE;

        switch (CONSTRAINT_LEVEL) {
          case 0:
            if (channel.getRawInterestOps() != interestOps) {
              key.interestOps(interestOps);
              if (Thread.currentThread() != worker.thread
                  && worker.wakenUp.compareAndSet(false, true)) {
                selector.wakeup();
              }
              changed = true;
            }
            break;
          case 1:
          case 2:
            if (channel.getRawInterestOps() != interestOps) {
              if (Thread.currentThread() == worker.thread) {
                key.interestOps(interestOps);
                changed = true;
              } else {
                worker.selectorGuard.readLock().lock();
                try {
                  if (worker.wakenUp.compareAndSet(false, true)) {
                    selector.wakeup();
                  }
                  key.interestOps(interestOps);
                  changed = true;
                } finally {
                  worker.selectorGuard.readLock().unlock();
                }
              }
            }
            break;
          default:
            throw new Error();
        }
      }

      future.setSuccess();
      if (changed) {
        channel.setRawInterestOpsNow(interestOps);
        fireChannelInterestChanged(channel);
      }
    } catch (CancelledKeyException e) {
      // setInterestOps() was called on a closed channel.
      ClosedChannelException cce = new ClosedChannelException();
      future.setFailure(cce);
      fireExceptionCaught(channel, cce);
    } catch (Throwable t) {
      future.setFailure(t);
      fireExceptionCaught(channel, t);
    }
  }