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