jdk/src/solaris/classes/sun/nio/ch/DevPollSelectorImpl.java
author alanb
Wed, 28 Aug 2013 15:50:03 +0100
changeset 19607 bee007586d06
parent 14342 8435a30053c1
child 23010 6dadb192ad81
permissions -rw-r--r--
8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 11823
diff changeset
     2
 * Copyright (c) 2001, 2012, 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: 2057
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: 2057
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: 2057
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.channels.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.misc.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * An implementation of Selector for Solaris.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
class DevPollSelectorImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    extends SelectorImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    // File descriptors used for interrupt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    protected int fd0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    protected int fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // The poll object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    DevPollArrayWrapper pollWrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // Maps from file descriptors to keys
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
    50
    private Map<Integer,SelectionKeyImpl> fdToKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // True if this Selector has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
    55
    // Lock for close/cleanup
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
    56
    private Object closeLock = new Object();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
    57
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // Lock for interrupt triggering and clearing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private Object interruptLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private boolean interruptTriggered = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Package private constructor called by factory method in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * the abstract superclass Selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    DevPollSelectorImpl(SelectorProvider sp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        super(sp);
6520
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
    68
        long pipeFds = IOUtil.makePipe(false);
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
    69
        fd0 = (int) (pipeFds >>> 32);
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
    70
        fd1 = (int) pipeFds;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        pollWrapper = new DevPollArrayWrapper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        pollWrapper.initInterrupt(fd0, fd1);
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
    73
        fdToKey = new HashMap<Integer,SelectionKeyImpl>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected int doSelect(long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new ClosedSelectorException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        processDeregisterQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            pollWrapper.poll(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        processDeregisterQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        int numKeysUpdated = updateSelectedKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (pollWrapper.interrupted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            // Clear the wakeup pipe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            pollWrapper.putReventOps(pollWrapper.interruptedIndex(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            synchronized (interruptLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                pollWrapper.clearInterrupted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                IOUtil.drain(fd0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                interruptTriggered = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return numKeysUpdated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Update the keys whose fd's have been selected by the devpoll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * driver. Add the ready keys to the ready queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private int updateSelectedKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int entries = pollWrapper.updated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int numKeysUpdated = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        for (int i=0; i<entries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            int nextFD = pollWrapper.getDescriptor(i);
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
   111
            SelectionKeyImpl ski = fdToKey.get(Integer.valueOf(nextFD));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // ski is null in the case of an interrupt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (ski != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                int rOps = pollWrapper.getReventOps(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                if (selectedKeys.contains(ski)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    if (ski.channel.translateAndSetReadyOps(rOps, ski)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        numKeysUpdated++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    ski.channel.translateAndSetReadyOps(rOps, ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        selectedKeys.add(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        numKeysUpdated++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return numKeysUpdated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    protected void implClose() throws IOException {
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   132
        if (closed)
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   133
            return;
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   134
        closed = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   136
        // prevent further wakeup
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   137
        synchronized (interruptLock) {
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   138
            interruptTriggered = true;
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   139
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1449
diff changeset
   141
        FileDispatcherImpl.closeIntFD(fd0);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1449
diff changeset
   142
        FileDispatcherImpl.closeIntFD(fd1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   144
        pollWrapper.release(fd0);
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   145
        pollWrapper.closeDevPollFD();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   146
        selectedKeys = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   148
        // Deregister channels
6520
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   149
        Iterator<SelectionKey> i = keys.iterator();
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   150
        while (i.hasNext()) {
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   151
            SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   152
            deregister(ski);
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   153
            SelectableChannel selch = ski.channel();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   154
            if (!selch.isOpen() && !selch.isRegistered())
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   155
                ((SelChImpl)selch).kill();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   156
            i.remove();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   158
        fd0 = -1;
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   159
        fd1 = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    protected void implRegister(SelectionKeyImpl ski) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        int fd = IOUtil.fdVal(ski.channel.getFD());
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
   164
        fdToKey.put(Integer.valueOf(fd), ski);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        keys.add(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    protected void implDereg(SelectionKeyImpl ski) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        int i = ski.getIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        assert (i >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int fd = ski.channel.getFDVal();
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
   172
        fdToKey.remove(Integer.valueOf(fd));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        pollWrapper.release(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        ski.setIndex(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        keys.remove(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        selectedKeys.remove(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        deregister((AbstractSelectionKey)ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        SelectableChannel selch = ski.channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (!selch.isOpen() && !selch.isRegistered())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            ((SelChImpl)selch).kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 7668
diff changeset
   183
    public void putEventOps(SelectionKeyImpl sk, int ops) {
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   184
        if (closed)
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   185
            throw new ClosedSelectorException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        int fd = IOUtil.fdVal(sk.channel.getFD());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        pollWrapper.setInterest(fd, ops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public Selector wakeup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        synchronized (interruptLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (!interruptTriggered) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                pollWrapper.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                interruptTriggered = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
}