< prev index next >

src/java.base/unix/classes/sun/nio/ch/PollSelectorImpl.java

Print this page

 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 package sun.nio.ch;
 26 
 27 import java.io.IOException;
 28 import java.nio.channels.ClosedSelectorException;
 29 import java.nio.channels.SelectionKey;
 30 import java.nio.channels.Selector;
 31 import java.nio.channels.spi.SelectorProvider;
 32 import java.util.ArrayDeque;
 33 import java.util.ArrayList;
 34 import java.util.Deque;
 35 import java.util.List;
 36 import java.util.concurrent.TimeUnit;
 37 import java.util.function.Consumer;
 38 
 39 import jdk.internal.misc.Unsafe;
 40 
 41 /**
 42  * Selector implementation based on poll
 43  */
 44 
 45 class PollSelectorImpl extends SelectorImpl {
 46 
 47     // initial capacity of poll array
 48     private static final int INITIAL_CAPACITY = 16;
 49 
 50     // poll array, grows as needed
 51     private int pollArrayCapacity = INITIAL_CAPACITY;
 52     private int pollArraySize;
 53     private AllocatedNativeObject pollArray;
 54 
 55     // file descriptors used for interrupt
 56     private final int fd0;
 57     private final int fd1;
 58 

 94     }
 95 
 96     @Override
 97     protected int doSelect(Consumer<SelectionKey> action, long timeout)
 98         throws IOException
 99     {
100         assert Thread.holdsLock(this);
101 
102         int to = (int) Math.min(timeout, Integer.MAX_VALUE); // max poll timeout
103         boolean blocking = (to != 0);
104         boolean timedPoll = (to > 0);
105 
106         processUpdateQueue();
107         processDeregisterQueue();
108         try {
109             begin(blocking);
110 
111             int numPolled;
112             do {
113                 long startTime = timedPoll ? System.nanoTime() : 0;
114                 numPolled = poll(pollArray.address(), pollArraySize, to);
115                 if (numPolled == IOStatus.INTERRUPTED && timedPoll) {
116                     // timed poll interrupted so need to adjust timeout
117                     long adjust = System.nanoTime() - startTime;
118                     to -= TimeUnit.MILLISECONDS.convert(adjust, TimeUnit.NANOSECONDS);
119                     if (to <= 0) {
120                         // timeout expired so no retry
121                         numPolled = 0;
122                     }
123                 }
124             } while (numPolled == IOStatus.INTERRUPTED);
125             assert numPolled <= pollArraySize;
126 
127         } finally {
128             end(blocking);
129         }
130 
131         processDeregisterQueue();
132         return processEvents(action);
133     }
134 





135     /**
136      * Process changes to the interest ops.
137      */
138     private void processUpdateQueue() {
139         assert Thread.holdsLock(this);
140 
141         synchronized (updateLock) {
142             SelectionKeyImpl ski;
143             while ((ski = updateKeys.pollFirst()) != null) {
144                 int newEvents = ski.translateInterestOps();
145                 if (ski.isValid()) {
146                     int index = ski.getIndex();
147                     assert index >= 0 && index < pollArraySize;
148                     if (index > 0) {
149                         assert pollKeys.get(index) == ski;
150                         if (newEvents == 0) {
151                             remove(ski);
152                         } else {
153                             update(ski, newEvents);
154                         }

 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 package sun.nio.ch;
 26 
 27 import java.io.IOException;
 28 import java.nio.channels.ClosedSelectorException;
 29 import java.nio.channels.SelectionKey;
 30 import java.nio.channels.Selector;
 31 import java.nio.channels.spi.SelectorProvider;
 32 import java.util.ArrayDeque;
 33 import java.util.ArrayList;
 34 import java.util.Deque;
 35 import java.util.List;
 36 import java.util.concurrent.TimeUnit;
 37 import java.util.function.Consumer;

 38 import jdk.internal.misc.Unsafe;
 39 
 40 /**
 41  * Selector implementation based on poll
 42  */
 43 
 44 class PollSelectorImpl extends SelectorImpl {
 45 
 46     // initial capacity of poll array
 47     private static final int INITIAL_CAPACITY = 16;
 48 
 49     // poll array, grows as needed
 50     private int pollArrayCapacity = INITIAL_CAPACITY;
 51     private int pollArraySize;
 52     private AllocatedNativeObject pollArray;
 53 
 54     // file descriptors used for interrupt
 55     private final int fd0;
 56     private final int fd1;
 57 

 93     }
 94 
 95     @Override
 96     protected int doSelect(Consumer<SelectionKey> action, long timeout)
 97         throws IOException
 98     {
 99         assert Thread.holdsLock(this);
100 
101         int to = (int) Math.min(timeout, Integer.MAX_VALUE); // max poll timeout
102         boolean blocking = (to != 0);
103         boolean timedPoll = (to > 0);
104 
105         processUpdateQueue();
106         processDeregisterQueue();
107         try {
108             begin(blocking);
109 
110             int numPolled;
111             do {
112                 long startTime = timedPoll ? System.nanoTime() : 0;
113                 numPolled = poll(to);
114                 if (numPolled == IOStatus.INTERRUPTED && timedPoll) {
115                     // timed poll interrupted so need to adjust timeout
116                     long adjust = System.nanoTime() - startTime;
117                     to -= TimeUnit.MILLISECONDS.convert(adjust, TimeUnit.NANOSECONDS);
118                     if (to <= 0) {
119                         // timeout expired so no retry
120                         numPolled = 0;
121                     }
122                 }
123             } while (numPolled == IOStatus.INTERRUPTED);
124             assert numPolled <= pollArraySize;
125 
126         } finally {
127             end(blocking);
128         }
129 
130         processDeregisterQueue();
131         return processEvents(action);
132     }
133 
134     @Override
135     protected int implPoll(long timeout) throws IOException {
136         return poll(pollArray.address(), pollArraySize, (int) timeout);
137     }
138 
139     /**
140      * Process changes to the interest ops.
141      */
142     private void processUpdateQueue() {
143         assert Thread.holdsLock(this);
144 
145         synchronized (updateLock) {
146             SelectionKeyImpl ski;
147             while ((ski = updateKeys.pollFirst()) != null) {
148                 int newEvents = ski.translateInterestOps();
149                 if (ski.isValid()) {
150                     int index = ski.getIndex();
151                     assert index >= 0 && index < pollArraySize;
152                     if (index > 0) {
153                         assert pollKeys.get(index) == ski;
154                         if (newEvents == 0) {
155                             remove(ski);
156                         } else {
157                             update(ski, newEvents);
158                         }
< prev index next >