1 /*
   2  * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.ch;
  27 
  28 import java.nio.channels.Channel;
  29 import java.io.FileDescriptor;
  30 import java.io.IOException;
  31 
  32 import static java.util.concurrent.TimeUnit.NANOSECONDS;
  33 
  34 import jdk.internal.misc.Strands;
  35 
  36 /**
  37  * An interface that allows translation (and more!).
  38  *
  39  * @since 1.4
  40  */
  41 
  42 public interface SelChImpl extends Channel {
  43 
  44     FileDescriptor getFD();
  45 
  46     int getFDVal();
  47 
  48     /**
  49      * Adds the specified ops if present in interestOps. The specified
  50      * ops are turned on without affecting the other ops.
  51      *
  52      * @return  true iff the new value of sk.readyOps() set by this method
  53      *          contains at least one bit that the previous value did not
  54      *          contain
  55      */
  56     boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl ski);
  57 
  58     /**
  59      * Sets the specified ops if present in interestOps. The specified
  60      * ops are turned on, and all other ops are turned off.
  61      *
  62      * @return  true iff the new value of sk.readyOps() set by this method
  63      *          contains at least one bit that the previous value did not
  64      *          contain
  65      */
  66     boolean translateAndSetReadyOps(int ops, SelectionKeyImpl ski);
  67 
  68     /**
  69      * Translates an interest operation set into a native event set
  70      */
  71     int translateInterestOps(int ops);
  72 
  73     void kill() throws IOException;
  74 
  75     /**
  76      * Disables the current thread or fiber for scheduling purposes until this
  77      * channel is ready for I/O, or asynchronously closed, for up to the
  78      * specified waiting time.
  79      *
  80      * <p> This method does <em>not</em> report which of these caused the
  81      * method to return. Callers should re-check the conditions which caused
  82      * the thread to park.
  83      *
  84      * @param event the event to poll
  85      * @param nanos the timeout to wait; {@code <= 0} to wait indefinitely
  86      */
  87     default void park(int event, long nanos) throws IOException {
  88         Object strand = Strands.currentStrand();
  89         if (PollerProvider.available() && (strand instanceof Fiber)) {
  90             Poller.register(strand, getFDVal(), event);
  91             if (isOpen()) {
  92                 try {
  93                     if (nanos == 0) {
  94                         Strands.parkFiber();
  95                     } else {
  96                         Strands.parkFiber(nanos);
  97                     }
  98                 } finally {
  99                     Poller.deregister(strand, getFDVal(), event);
 100                 }
 101             }
 102         } else {
 103             long millis;
 104             if (nanos == 0) {
 105                 millis = -1;
 106             } else {
 107                 millis = NANOSECONDS.toMillis(nanos);
 108             }
 109             Net.poll(getFD(), event, millis);
 110         }
 111     }
 112 
 113     /**
 114      * Disables the current thread or fiber for scheduling purposes until this
 115      * channel is ready for I/O, or asynchronously closed.
 116      *
 117      * <p> This method does <em>not</em> report which of these caused the
 118      * method to return. Callers should re-check the conditions which caused
 119      * the thread to park.
 120      *
 121      * @param event the event to poll
 122      */
 123     default void park(int event) throws IOException {
 124         park(event, 0L);
 125     }
 126 }