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