jdk/src/java.base/windows/classes/sun/nio/ch/WindowsSelectorImpl.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 29920 f81c14f472ab
child 34716 7477a052aecc
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16004
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents: 14342
diff changeset
     2
 * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2445
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2445
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2445
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2445
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2445
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
package sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.spi.SelectorProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.channels.Selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.channels.ClosedSelectorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.channels.Pipe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.nio.channels.SelectableChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
16004
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents: 14342
diff changeset
    38
import java.nio.channels.CancelledKeyException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Iterator;
29920
f81c14f472ab 8042322: Enhance thread contexts in networking and nio
chegar
parents: 25859
diff changeset
    43
import sun.misc.ManagedLocalsThread;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * A multi-threaded implementation of Selector for Windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Konstantin Kladko
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
final class WindowsSelectorImpl extends SelectorImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // Initial capacity of the poll array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final int INIT_CAP = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // Maximum number of sockets for select().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // Should be INIT_CAP times a power of 2
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29920
diff changeset
    57
    private static final int MAX_SELECTABLE_FDS = 1024;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // The list of SelectableChannels serviced by this Selector. Every mod
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // MAX_SELECTABLE_FDS entry is bogus, to align this array with the poll
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // array,  where the corresponding entry is occupied by the wakeupSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private SelectionKeyImpl[] channelArray = new SelectionKeyImpl[INIT_CAP];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // The global native poll array holds file decriptors and event masks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private PollArrayWrapper pollWrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // The number of valid entries in  poll array, including entries occupied
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // by wakeup socket handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private int totalChannels = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // Number of helper threads needed for select. We need one thread per
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // each additional set of MAX_SELECTABLE_FDS - 1 channels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private int threadsCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // A list of helper threads for select.
2445
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
    76
    private final List<SelectThread> threads = new ArrayList<SelectThread>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    //Pipe used as a wakeup object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private final Pipe wakeupPipe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // File descriptors corresponding to source and sink
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private final int wakeupSourceFd, wakeupSinkFd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
    84
    // Lock for close cleanup
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
    85
    private Object closeLock = new Object();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
    86
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // Maps file descriptors to their indices in  pollArray
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29920
diff changeset
    88
    private static final class FdMap extends HashMap<Integer, MapEntry> {
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
    89
        static final long serialVersionUID = 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        private MapEntry get(int desc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return get(new Integer(desc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        private MapEntry put(SelectionKeyImpl ski) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return put(new Integer(ski.channel.getFDVal()), new MapEntry(ski));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        private MapEntry remove(SelectionKeyImpl ski) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            Integer fd = new Integer(ski.channel.getFDVal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            MapEntry x = get(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if ((x != null) && (x.ski.channel == ski.channel))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                return remove(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // class for fdMap entries
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29920
diff changeset
   106
    private static final class MapEntry {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        SelectionKeyImpl ski;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        long updateCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        long clearedCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        MapEntry(SelectionKeyImpl ski) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            this.ski = ski;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private final FdMap fdMap = new FdMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // SubSelector for the main thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private final SubSelector subSelector = new SubSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private long timeout; //timeout for poll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    // Lock for interrupt triggering and clearing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private final Object interruptLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private volatile boolean interruptTriggered = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    WindowsSelectorImpl(SelectorProvider sp) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        pollWrapper = new PollArrayWrapper(INIT_CAP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        wakeupPipe = Pipe.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        wakeupSourceFd = ((SelChImpl)wakeupPipe.source()).getFDVal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // Disable the Nagle algorithm so that the wakeup is more immediate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        SinkChannelImpl sink = (SinkChannelImpl)wakeupPipe.sink();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        (sink.sc).socket().setTcpNoDelay(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        wakeupSinkFd = ((SelChImpl)sink).getFDVal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        pollWrapper.addWakeupSocket(wakeupSourceFd, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    protected int doSelect(long timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (channelArray == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            throw new ClosedSelectorException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        this.timeout = timeout; // set selector timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        processDeregisterQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (interruptTriggered) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            resetWakeupSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // Calculate number of helper threads needed for poll. If necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // threads are created here and start waiting on startLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        adjustThreadsCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        finishLock.reset(); // reset finishLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // Wakeup helper threads, waiting on startLock, so they start polling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // Redundant threads will exit here after wakeup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        startLock.startThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // do polling in the main thread. Main thread is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // first MAX_SELECTABLE_FDS entries in pollArray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                subSelector.poll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                finishLock.setException(e); // Save this exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            // Main thread is out of poll(). Wakeup others and wait for them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (threads.size() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                finishLock.waitForHelperThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
          } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
              end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // Done with poll(). Set wakeupSocket to nonsignaled  for the next run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        finishLock.checkForException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        processDeregisterQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        int updated = updateSelectedKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // Done with poll(). Set wakeupSocket to nonsignaled  for the next run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        resetWakeupSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return updated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // Helper threads wait on this lock for the next poll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private final StartLock startLock = new StartLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private final class StartLock {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // A variable which distinguishes the current run of doSelect from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // previous one. Incrementing runsCounter and notifying threads will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // trigger another round of poll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        private long runsCounter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
       // Triggers threads, waiting on this lock to start polling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        private synchronized void startThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            runsCounter++; // next run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            notifyAll(); // wake up threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // This function is called by a helper thread to wait for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // next round of poll(). It also checks, if this thread became
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // redundant. If yes, it returns true, notifying the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // that it should exit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        private synchronized boolean waitForStart(SelectThread thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                while (runsCounter == thread.lastRun) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                        startLock.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
2445
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   205
                if (thread.isZombie()) { // redundant thread
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    return true; // will cause run() to exit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    thread.lastRun = runsCounter; // update lastRun
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    return false; //   will cause run() to poll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    // Main thread waits on this lock, until all helper threads are done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    // with poll().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private final FinishLock finishLock = new FinishLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private final class FinishLock  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // Number of helper threads, that did not finish yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        private int threadsToFinish;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19607
diff changeset
   223
        // IOException which occurred during the last run.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        IOException exception = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        // Called before polling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        private void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            threadsToFinish = threads.size(); // helper threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        // Each helper thread invokes this function on finishLock, when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // the thread is done with poll().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        private synchronized void threadFinished() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            if (threadsToFinish == threads.size()) { // finished poll() first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                // if finished first, wakeup others
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                wakeup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            threadsToFinish--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (threadsToFinish == 0) // all helper threads finished poll().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                notify();             // notify the main thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // The main thread invokes this function on finishLock to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // for helper threads to finish poll().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        private synchronized void waitForHelperThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (threadsToFinish == threads.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                // no helper threads finished yet. Wakeup them up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                wakeup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            while (threadsToFinish != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    finishLock.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    // Interrupted - set interrupted state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // sets IOException for this run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        private synchronized void setException(IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            exception = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        // Checks if there was any exception during the last run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        // If yes, throws it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        private void checkForException() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            if (exception == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                return;
21591
35320b590d9b 8026491: Typos in string literals
malenkov
parents: 21278
diff changeset
   270
            StringBuffer message =  new StringBuffer("An exception occurred" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                       " during the execution of select(): \n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            message.append(exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            message.append('\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            exception = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throw new IOException(message.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private final class SubSelector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        private final int pollArrayIndex; // starting index in pollArray to poll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // These arrays will hold result of native select().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // The first element of each array is the number of selected sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // Other elements are file descriptors of selected sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        private final int[] readFds = new int [MAX_SELECTABLE_FDS + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        private final int[] writeFds = new int [MAX_SELECTABLE_FDS + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        private final int[] exceptFds = new int [MAX_SELECTABLE_FDS + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        private SubSelector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            this.pollArrayIndex = 0; // main thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        private SubSelector(int threadIndex) { // helper threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            this.pollArrayIndex = (threadIndex + 1) * MAX_SELECTABLE_FDS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        private int poll() throws IOException{ // poll for the main thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            return poll0(pollWrapper.pollArrayAddress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                         Math.min(totalChannels, MAX_SELECTABLE_FDS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                         readFds, writeFds, exceptFds, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        private int poll(int index) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            // poll for helper threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return  poll0(pollWrapper.pollArrayAddress +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                     (pollArrayIndex * PollArrayWrapper.SIZE_POLLFD),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                     Math.min(MAX_SELECTABLE_FDS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                             totalChannels - (index + 1) * MAX_SELECTABLE_FDS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                     readFds, writeFds, exceptFds, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        private native int poll0(long pollAddress, int numfds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
             int[] readFds, int[] writeFds, int[] exceptFds, long timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        private int processSelectedKeys(long updateCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            int numKeysUpdated = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            numKeysUpdated += processFDSet(updateCount, readFds,
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 21591
diff changeset
   317
                                           Net.POLLIN,
5983
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   318
                                           false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            numKeysUpdated += processFDSet(updateCount, writeFds,
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 21591
diff changeset
   320
                                           Net.POLLCONN |
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 21591
diff changeset
   321
                                           Net.POLLOUT,
5983
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   322
                                           false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            numKeysUpdated += processFDSet(updateCount, exceptFds,
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 21591
diff changeset
   324
                                           Net.POLLIN |
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 21591
diff changeset
   325
                                           Net.POLLCONN |
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 21591
diff changeset
   326
                                           Net.POLLOUT,
5983
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   327
                                           true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return numKeysUpdated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         * Note, clearedCount is used to determine if the readyOps have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         * been reset in this select operation. updateCount is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         * tell if a key has been counted as updated in this select
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         * operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         * me.updateCount <= me.clearedCount <= updateCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         */
5983
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   339
        private int processFDSet(long updateCount, int[] fds, int rOps,
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   340
                                 boolean isExceptFds)
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   341
        {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            int numKeysUpdated = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            for (int i = 1; i <= fds[0]; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                int desc = fds[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                if (desc == wakeupSourceFd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    synchronized (interruptLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        interruptTriggered = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                MapEntry me = fdMap.get(desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                // If me is null, the key was deregistered in the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                // processDeregisterQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                if (me == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                SelectionKeyImpl sk = me.ski;
5983
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   357
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   358
                // The descriptor may be in the exceptfds set because there is
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   359
                // OOB data queued to the socket. If there is OOB data then it
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   360
                // is discarded and the key is not added to the selected set.
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   361
                if (isExceptFds &&
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   362
                    (sk.channel() instanceof SocketChannelImpl) &&
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   363
                    discardUrgentData(desc))
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   364
                {
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   365
                    continue;
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   366
                }
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   367
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                if (selectedKeys.contains(sk)) { // Key in selected set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    if (me.clearedCount != updateCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        if (sk.channel.translateAndSetReadyOps(rOps, sk) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                            (me.updateCount != updateCount)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                            me.updateCount = updateCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                            numKeysUpdated++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    } else { // The readyOps have been set; now add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        if (sk.channel.translateAndUpdateReadyOps(rOps, sk) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                            (me.updateCount != updateCount)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                            me.updateCount = updateCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                            numKeysUpdated++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    me.clearedCount = updateCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                } else { // Key is not in selected set yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    if (me.clearedCount != updateCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                        sk.channel.translateAndSetReadyOps(rOps, sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                        if ((sk.nioReadyOps() & sk.nioInterestOps()) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                            selectedKeys.add(sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                            me.updateCount = updateCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                            numKeysUpdated++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    } else { // The readyOps have been set; now add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                        sk.channel.translateAndUpdateReadyOps(rOps, sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                        if ((sk.nioReadyOps() & sk.nioInterestOps()) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                            selectedKeys.add(sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                            me.updateCount = updateCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                            numKeysUpdated++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    me.clearedCount = updateCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            return numKeysUpdated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    // Represents a helper thread used for select.
29920
f81c14f472ab 8042322: Enhance thread contexts in networking and nio
chegar
parents: 25859
diff changeset
   407
    private final class SelectThread extends ManagedLocalsThread {
2445
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   408
        private final int index; // index of this thread
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   409
        final SubSelector subSelector;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        private long lastRun = 0; // last run number
2445
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   411
        private volatile boolean zombie;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        // Creates a new thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        private SelectThread(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            this.index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            this.subSelector = new SubSelector(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            //make sure we wait for next round of poll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            this.lastRun = startLock.runsCounter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
2445
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   419
        void makeZombie() {
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   420
            zombie = true;
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   421
        }
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   422
        boolean isZombie() {
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   423
            return zombie;
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   424
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            while (true) { // poll loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                // wait for the start of poll. If this thread has become
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                // redundant, then exit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                if (startLock.waitForStart(this))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                // call poll()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    subSelector.poll(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    // Save this exception and let other threads finish.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    finishLock.setException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                // notify main thread, that this thread has finished, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                // wakeup others, if this thread is the first to finish.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                finishLock.threadFinished();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    // After some channels registered/deregistered, the number of required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    // helper threads may have changed. Adjust this number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    private void adjustThreadsCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        if (threadsCount > threads.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            // More threads needed. Start more threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            for (int i = threads.size(); i < threadsCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                SelectThread newThread = new SelectThread(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                threads.add(newThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                newThread.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                newThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        } else if (threadsCount < threads.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            // Some threads become redundant. Remove them from the threads List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            for (int i = threads.size() - 1 ; i >= threadsCount; i--)
2445
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   459
                threads.remove(i).makeZombie();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    // Sets Windows wakeup socket to a signaled state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    private void setWakeupSocket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        setWakeupSocket0(wakeupSinkFd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    private native void setWakeupSocket0(int wakeupSinkFd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    // Sets Windows wakeup socket to a non-signaled state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    private void resetWakeupSocket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        synchronized (interruptLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            if (interruptTriggered == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            resetWakeupSocket0(wakeupSourceFd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            interruptTriggered = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    private native void resetWakeupSocket0(int wakeupSourceFd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
5983
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   481
    private native boolean discardUrgentData(int fd);
b5bc332cd233 6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
alanb
parents: 5506
diff changeset
   482
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    // We increment this counter on each call to updateSelectedKeys()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    // each entry in  SubSelector.fdsMap has a memorized value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    // updateCount. When we increment numKeysUpdated we set updateCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    // for the corresponding entry to its current value. This is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    // avoid counting the same key more than once - the same key can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    // appear in readfds and writefds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    private long updateCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    // Update ops of the corresponding Channels. Add the ready keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    // ready queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    private int updateSelectedKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        updateCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        int numKeysUpdated = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        numKeysUpdated += subSelector.processSelectedKeys(updateCount);
2445
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   497
        for (SelectThread t: threads) {
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   498
            numKeysUpdated += t.subSelector.processSelectedKeys(updateCount);
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   499
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        return numKeysUpdated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    protected void implClose() throws IOException {
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   504
        synchronized (closeLock) {
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   505
            if (channelArray != null) {
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   506
                if (pollWrapper != null) {
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   507
                    // prevent further wakeup
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   508
                    synchronized (interruptLock) {
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   509
                        interruptTriggered = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    }
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   511
                    wakeupPipe.sink().close();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   512
                    wakeupPipe.source().close();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   513
                    for(int i = 1; i < totalChannels; i++) { // Deregister channels
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   514
                        if (i % MAX_SELECTABLE_FDS != 0) { // skip wakeupEvent
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   515
                            deregister(channelArray[i]);
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   516
                            SelectableChannel selch = channelArray[i].channel();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   517
                            if (!selch.isOpen() && !selch.isRegistered())
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   518
                                ((SelChImpl)selch).kill();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   519
                        }
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   520
                    }
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   521
                    pollWrapper.free();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   522
                    pollWrapper = null;
2445
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   523
                    selectedKeys = null;
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   524
                    channelArray = null;
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   525
                    // Make all remaining helper threads exit
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   526
                    for (SelectThread t: threads)
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   527
                         t.makeZombie();
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   528
                    startLock.startThreads();
a1fa6863fc50 6823609: (se) Selector.select hangs on Windows under load
alanb
parents: 1639
diff changeset
   529
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    protected void implRegister(SelectionKeyImpl ski) {
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   535
        synchronized (closeLock) {
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   536
            if (pollWrapper == null)
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   537
                throw new ClosedSelectorException();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   538
            growIfNeeded();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   539
            channelArray[totalChannels] = ski;
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   540
            ski.setIndex(totalChannels);
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   541
            fdMap.put(ski);
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   542
            keys.add(ski);
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   543
            pollWrapper.addEntry(totalChannels, ski);
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   544
            totalChannels++;
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   545
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    private void growIfNeeded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        if (channelArray.length == totalChannels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            int newSize = totalChannels * 2; // Make a larger array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            SelectionKeyImpl temp[] = new SelectionKeyImpl[newSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            System.arraycopy(channelArray, 1, temp, 1, totalChannels - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            channelArray = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            pollWrapper.grow(newSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (totalChannels % MAX_SELECTABLE_FDS == 0) { // more threads needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            pollWrapper.addWakeupSocket(wakeupSourceFd, totalChannels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            totalChannels++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            threadsCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    protected void implDereg(SelectionKeyImpl ski) throws IOException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        int i = ski.getIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        assert (i >= 0);
16004
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents: 14342
diff changeset
   566
        synchronized (closeLock) {
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents: 14342
diff changeset
   567
            if (i != totalChannels - 1) {
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents: 14342
diff changeset
   568
                // Copy end one over it
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents: 14342
diff changeset
   569
                SelectionKeyImpl endChannel = channelArray[totalChannels-1];
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents: 14342
diff changeset
   570
                channelArray[i] = endChannel;
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents: 14342
diff changeset
   571
                endChannel.setIndex(i);
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents: 14342
diff changeset
   572
                pollWrapper.replaceEntry(pollWrapper, totalChannels - 1,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                                                                pollWrapper, i);
16004
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents: 14342
diff changeset
   574
            }
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents: 14342
diff changeset
   575
            ski.setIndex(-1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        channelArray[totalChannels - 1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        totalChannels--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        if ( totalChannels != 1 && totalChannels % MAX_SELECTABLE_FDS == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            totalChannels--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            threadsCount--; // The last thread has become redundant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        fdMap.remove(ski); // Remove the key from fdMap, keys and selectedKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        keys.remove(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        selectedKeys.remove(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        deregister(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        SelectableChannel selch = ski.channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        if (!selch.isOpen() && !selch.isRegistered())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            ((SelChImpl)selch).kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 7668
diff changeset
   592
    public void putEventOps(SelectionKeyImpl sk, int ops) {
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   593
        synchronized (closeLock) {
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   594
            if (pollWrapper == null)
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   595
                throw new ClosedSelectorException();
16004
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents: 14342
diff changeset
   596
            // make sure this sk has not been removed yet
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents: 14342
diff changeset
   597
            int index = sk.getIndex();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents: 14342
diff changeset
   598
            if (index == -1)
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents: 14342
diff changeset
   599
                throw new CancelledKeyException();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents: 14342
diff changeset
   600
            pollWrapper.putEventOps(index, ops);
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   601
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    public Selector wakeup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        synchronized (interruptLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            if (!interruptTriggered) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                setWakeupSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                interruptTriggered = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    static {
19607
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16004
diff changeset
   615
        IOUtil.load();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
}