src/java.base/linux/classes/sun/nio/ch/EPollSelectorImpl.java
author alanb
Fri, 23 Mar 2018 14:18:18 +0000
changeset 49290 07779973cbe2
parent 49248 15a0e60c8b97
child 49493 814bd31f8da0
permissions -rw-r--r--
8199791: (se) More Selector cleanup Reviewed-by: redestad, bpb
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) 2005, 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: 2433
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: 2433
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: 2433
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2433
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2433
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    29
import java.nio.channels.ClosedSelectorException;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    30
import java.nio.channels.SelectableChannel;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    31
import java.nio.channels.SelectionKey;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    32
import java.nio.channels.Selector;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    33
import java.nio.channels.spi.SelectorProvider;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    34
import java.util.ArrayDeque;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    35
import java.util.BitSet;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    36
import java.util.Deque;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    37
import java.util.HashMap;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    38
import java.util.Iterator;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    39
import java.util.Map;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    40
import java.util.concurrent.TimeUnit;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    41
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    42
import static sun.nio.ch.EPoll.EPOLLIN;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    43
import static sun.nio.ch.EPoll.EPOLL_CTL_ADD;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    44
import static sun.nio.ch.EPoll.EPOLL_CTL_DEL;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    45
import static sun.nio.ch.EPoll.EPOLL_CTL_MOD;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    46
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    49
 * Linux epoll based Selector implementation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    51
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    52
class EPollSelectorImpl extends SelectorImpl {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    53
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    54
    // maximum number of events to poll in one call to epoll_wait
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    55
    private static final int NUM_EPOLLEVENTS = Math.min(IOUtil.fdLimit(), 1024);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    56
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    57
    // epoll file descriptor
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    58
    private final int epfd;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    59
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    60
    // address of poll array when polling with epoll_wait
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    61
    private final long pollArrayAddress;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    62
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    63
    // file descriptors used for interrupt
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    64
    private final int fd0;
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    65
    private final int fd1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    67
    // maps file descriptor to selection key, synchronize on selector
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    68
    private final Map<Integer, SelectionKeyImpl> fdToKey = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    70
    // file descriptors registered with epoll, synchronize on selector
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    71
    private final BitSet registered = new BitSet();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    73
    // pending new registrations/updates, queued by implRegister and putEventOps
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    74
    private final Object updateLock = new Object();
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    75
    private final Deque<SelectionKeyImpl> newKeys = new ArrayDeque<>();
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    76
    private final Deque<SelectionKeyImpl> updateKeys = new ArrayDeque<>();
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    77
    private final Deque<Integer> updateOps = new ArrayDeque<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    79
    // interrupt triggering and clearing
16474
93db93a49848 8009751: (se) Selector spin when select, close and interestOps(0) invoked at same time (lnx)
alanb
parents: 14342
diff changeset
    80
    private final Object interruptLock = new Object();
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    81
    private boolean interruptTriggered;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Package private constructor called by factory method in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * the abstract superclass Selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
16474
93db93a49848 8009751: (se) Selector spin when select, close and interestOps(0) invoked at same time (lnx)
alanb
parents: 14342
diff changeset
    87
    EPollSelectorImpl(SelectorProvider sp) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        super(sp);
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    89
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    90
        this.epfd = EPoll.create();
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    91
        this.pollArrayAddress = EPoll.allocatePollArray(NUM_EPOLLEVENTS);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    92
32856
c654cdf1d685 8138819: (se) File descriptor leak when Selector.open fails
igerasim
parents: 25859
diff changeset
    93
        try {
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    94
            long fds = IOUtil.makePipe(false);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    95
            this.fd0 = (int) (fds >>> 32);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    96
            this.fd1 = (int) fds;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    97
        } catch (IOException ioe) {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    98
            EPoll.freePollArray(pollArrayAddress);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    99
            FileDispatcherImpl.closeIntFD(epfd);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   100
            throw ioe;
32856
c654cdf1d685 8138819: (se) File descriptor leak when Selector.open fails
igerasim
parents: 25859
diff changeset
   101
        }
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   102
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   103
        // register one end of the socket pair for wakeups
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   104
        EPoll.ctl(epfd, EPOLL_CTL_ADD, fd0, EPOLLIN);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   107
    private void ensureOpen() {
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   108
        if (!isOpen())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new ClosedSelectorException();
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   110
    }
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   111
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   112
    @Override
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   113
    protected int doSelect(long timeout) throws IOException {
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   114
        assert Thread.holdsLock(this);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   115
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   116
        int numEntries;
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   117
        processUpdateQueue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        processDeregisterQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            begin();
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   121
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   122
            // epoll_wait timeout is int
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   123
            int to = (int) Math.min(timeout, Integer.MAX_VALUE);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   124
            boolean timedPoll = (to > 0);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   125
            do {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   126
                long startTime = timedPoll ? System.nanoTime() : 0;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   127
                numEntries = EPoll.wait(epfd, pollArrayAddress, NUM_EPOLLEVENTS, to);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   128
                if (numEntries == IOStatus.INTERRUPTED && timedPoll) {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   129
                    // timed poll interrupted so need to adjust timeout
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   130
                    long adjust = System.nanoTime() - startTime;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   131
                    to -= TimeUnit.MILLISECONDS.convert(adjust, TimeUnit.NANOSECONDS);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   132
                    if (to <= 0) {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   133
                        // timeout expired so no retry
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   134
                        numEntries = 0;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   135
                    }
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   136
                }
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   137
            } while (numEntries == IOStatus.INTERRUPTED);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   138
            assert IOStatus.check(numEntries);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   139
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        processDeregisterQueue();
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   144
        return updateSelectedKeys(numEntries);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   148
     * Process new registrations and changes to the interest ops.
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   149
     */
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   150
    private void processUpdateQueue() {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   151
        assert Thread.holdsLock(this);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   152
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   153
        synchronized (updateLock) {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   154
            SelectionKeyImpl ski;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   155
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   156
            // new registrations
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   157
            while ((ski = newKeys.pollFirst()) != null) {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   158
                if (ski.isValid()) {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   159
                    SelChImpl ch = ski.channel;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   160
                    int fd = ch.getFDVal();
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   161
                    SelectionKeyImpl previous = fdToKey.put(fd, ski);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   162
                    assert previous == null;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   163
                    assert registered.get(fd) == false;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   164
                }
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   165
            }
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   166
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   167
            // changes to interest ops
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   168
            assert updateKeys.size() == updateOps.size();
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   169
            while ((ski = updateKeys.pollFirst()) != null) {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   170
                int ops = updateOps.pollFirst();
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   171
                int fd = ski.channel.getFDVal();
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   172
                if (ski.isValid() && fdToKey.containsKey(fd)) {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   173
                    if (registered.get(fd)) {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   174
                        if (ops == 0) {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   175
                            // remove from epoll
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   176
                            EPoll.ctl(epfd, EPOLL_CTL_DEL, fd, 0);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   177
                            registered.clear(fd);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   178
                        } else {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   179
                            // modify events
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   180
                            EPoll.ctl(epfd, EPOLL_CTL_MOD, fd, ops);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   181
                        }
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   182
                    } else if (ops != 0) {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   183
                        // add to epoll
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   184
                        EPoll.ctl(epfd, EPOLL_CTL_ADD, fd, ops);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   185
                        registered.set(fd);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   186
                    }
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   187
                }
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   188
            }
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   189
        }
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   190
    }
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   191
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   192
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Update the keys whose fd's have been selected by the epoll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Add the ready keys to the ready queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   196
    private int updateSelectedKeys(int numEntries) throws IOException {
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   197
        assert Thread.holdsLock(this);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   198
        assert Thread.holdsLock(nioSelectedKeys());
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   199
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   200
        boolean interrupted = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        int numKeysUpdated = 0;
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   202
        for (int i=0; i<numEntries; i++) {
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   203
            long event = EPoll.getEvent(pollArrayAddress, i);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   204
            int fd = EPoll.getDescriptor(event);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   205
            if (fd == fd0) {
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   206
                interrupted = true;
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   207
            } else {
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   208
                SelectionKeyImpl ski = fdToKey.get(fd);
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   209
                if (ski != null) {
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   210
                    int rOps = EPoll.getEvents(event);
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   211
                    if (selectedKeys.contains(ski)) {
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   212
                        if (ski.channel.translateAndSetReadyOps(rOps, ski)) {
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   213
                            numKeysUpdated++;
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   214
                        }
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   215
                    } else {
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   216
                        ski.channel.translateAndSetReadyOps(rOps, ski);
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   217
                        if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) {
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   218
                            selectedKeys.add(ski);
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   219
                            numKeysUpdated++;
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   220
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   225
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   226
        if (interrupted) {
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   227
            clearInterrupt();
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   228
        }
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   229
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return numKeysUpdated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   233
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    protected void implClose() throws IOException {
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   235
        assert Thread.holdsLock(this);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   236
        assert Thread.holdsLock(nioKeys());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   238
        // prevent further wakeup
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   239
        synchronized (interruptLock) {
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   240
            interruptTriggered = true;
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   241
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   243
        FileDispatcherImpl.closeIntFD(epfd);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   244
        EPoll.freePollArray(pollArrayAddress);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   245
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1639
diff changeset
   246
        FileDispatcherImpl.closeIntFD(fd0);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1639
diff changeset
   247
        FileDispatcherImpl.closeIntFD(fd1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   249
        // Deregister channels
6520
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   250
        Iterator<SelectionKey> i = keys.iterator();
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   251
        while (i.hasNext()) {
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   252
            SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   253
            deregister(ski);
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   254
            SelectableChannel selch = ski.channel();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   255
            if (!selch.isOpen() && !selch.isRegistered())
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   256
                ((SelChImpl)selch).kill();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   257
            i.remove();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   261
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    protected void implRegister(SelectionKeyImpl ski) {
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   263
        assert Thread.holdsLock(nioKeys());
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   264
        ensureOpen();
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   265
        synchronized (updateLock) {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   266
            newKeys.addLast(ski);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   267
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        keys.add(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   271
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    protected void implDereg(SelectionKeyImpl ski) throws IOException {
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   273
        assert !ski.isValid();
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   274
        assert Thread.holdsLock(this);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   275
        assert Thread.holdsLock(nioKeys());
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   276
        assert Thread.holdsLock(nioSelectedKeys());
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   277
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   278
        int fd = ski.channel.getFDVal();
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   279
        fdToKey.remove(fd);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   280
        if (registered.get(fd)) {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   281
            EPoll.ctl(epfd, EPOLL_CTL_DEL, fd, 0);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   282
            registered.clear(fd);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   283
        }
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   284
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   285
        selectedKeys.remove(ski);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        keys.remove(ski);
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   287
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   288
        // remove from channel's key set
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   289
        deregister(ski);
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   290
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        SelectableChannel selch = ski.channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (!selch.isOpen() && !selch.isRegistered())
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   293
            ((SelChImpl) selch).kill();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   296
    @Override
16474
93db93a49848 8009751: (se) Selector spin when select, close and interestOps(0) invoked at same time (lnx)
alanb
parents: 14342
diff changeset
   297
    public void putEventOps(SelectionKeyImpl ski, int ops) {
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   298
        ensureOpen();
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   299
        synchronized (updateLock) {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   300
            updateOps.addLast(ops);   // ops first in case adding the key fails
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   301
            updateKeys.addLast(ski);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   302
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   305
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public Selector wakeup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        synchronized (interruptLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            if (!interruptTriggered) {
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   309
                try {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   310
                    IOUtil.write1(fd1, (byte)0);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   311
                } catch (IOException ioe) {
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   312
                    throw new InternalError(ioe);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   313
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                interruptTriggered = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   319
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   320
    private void clearInterrupt() throws IOException {
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   321
        synchronized (interruptLock) {
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   322
            IOUtil.drain(fd0);
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   323
            interruptTriggered = false;
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   324
        }
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   325
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
}