jdk/src/solaris/classes/sun/nio/ch/DevPollSelectorImpl.java
author alanb
Sun, 15 Feb 2009 12:25:54 +0000
changeset 2057 3acf8e5e2ca0
parent 1449 2ed6188288d6
child 5506 202f599c92aa
permissions -rw-r--r--
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99 4313887: New I/O: Improved filesystem interface 4607272: New I/O: Support asynchronous I/O Reviewed-by: sherman, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1449
diff changeset
     2
 * Copyright 2001-2009 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        int[] fdes = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        IOUtil.initPipe(fdes, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        fd0 = fdes[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        fd1 = fdes[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        pollWrapper = new DevPollArrayWrapper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        pollWrapper.initInterrupt(fd0, fd1);
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
    74
        fdToKey = new HashMap<Integer,SelectionKeyImpl>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    protected int doSelect(long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new ClosedSelectorException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        processDeregisterQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            pollWrapper.poll(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        processDeregisterQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        int numKeysUpdated = updateSelectedKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (pollWrapper.interrupted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            // Clear the wakeup pipe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            pollWrapper.putReventOps(pollWrapper.interruptedIndex(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            synchronized (interruptLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                pollWrapper.clearInterrupted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                IOUtil.drain(fd0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                interruptTriggered = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return numKeysUpdated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Update the keys whose fd's have been selected by the devpoll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * driver. Add the ready keys to the ready queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private int updateSelectedKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int entries = pollWrapper.updated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int numKeysUpdated = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        for (int i=0; i<entries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            int nextFD = pollWrapper.getDescriptor(i);
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
   112
            SelectionKeyImpl ski = fdToKey.get(Integer.valueOf(nextFD));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            // ski is null in the case of an interrupt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (ski != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                int rOps = pollWrapper.getReventOps(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                if (selectedKeys.contains(ski)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    if (ski.channel.translateAndSetReadyOps(rOps, ski)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        numKeysUpdated++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    ski.channel.translateAndSetReadyOps(rOps, ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        selectedKeys.add(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        numKeysUpdated++;
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return numKeysUpdated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    protected void implClose() throws IOException {
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   133
        if (closed)
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   134
            return;
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   135
        closed = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   137
        // prevent further wakeup
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   138
        synchronized (interruptLock) {
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   139
            interruptTriggered = true;
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   140
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1449
diff changeset
   142
        FileDispatcherImpl.closeIntFD(fd0);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1449
diff changeset
   143
        FileDispatcherImpl.closeIntFD(fd1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   145
        pollWrapper.release(fd0);
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   146
        pollWrapper.closeDevPollFD();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   147
        selectedKeys = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   149
        // Deregister channels
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   150
        Iterator i = keys.iterator();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   151
        while (i.hasNext()) {
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   152
            SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   153
            deregister(ski);
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   154
            SelectableChannel selch = ski.channel();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   155
            if (!selch.isOpen() && !selch.isRegistered())
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   156
                ((SelChImpl)selch).kill();
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   157
            i.remove();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   159
        fd0 = -1;
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   160
        fd1 = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    protected void implRegister(SelectionKeyImpl ski) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        int fd = IOUtil.fdVal(ski.channel.getFD());
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
   165
        fdToKey.put(Integer.valueOf(fd), ski);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        keys.add(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    protected void implDereg(SelectionKeyImpl ski) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        int i = ski.getIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        assert (i >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        int fd = ski.channel.getFDVal();
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
   173
        fdToKey.remove(Integer.valueOf(fd));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        pollWrapper.release(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        ski.setIndex(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        keys.remove(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        selectedKeys.remove(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        deregister((AbstractSelectionKey)ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        SelectableChannel selch = ski.channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (!selch.isOpen() && !selch.isRegistered())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            ((SelChImpl)selch).kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    void putEventOps(SelectionKeyImpl sk, int ops) {
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   185
        if (closed)
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 1247
diff changeset
   186
            throw new ClosedSelectorException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        int fd = IOUtil.fdVal(sk.channel.getFD());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        pollWrapper.setInterest(fd, ops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public Selector wakeup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        synchronized (interruptLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (!interruptTriggered) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                pollWrapper.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                interruptTriggered = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        Util.load();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
}