jdk/src/solaris/classes/sun/nio/ch/EPollSelectorImpl.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 895 67f1dc69ad10
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2007 Sun Microsystems, Inc.  All Rights Reserved.
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 Linux 2.6+ kernels that uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * the epoll event notification facility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
class EPollSelectorImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    extends SelectorImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    // File descriptors used for interrupt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    protected int fd0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    protected int fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // The poll object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    EPollArrayWrapper pollWrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // Maps from file descriptors to keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private HashMap fdToKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // True if this Selector has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // Lock for interrupt triggering and clearing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private Object interruptLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private boolean interruptTriggered = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Package private constructor called by factory method in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * the abstract superclass Selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    EPollSelectorImpl(SelectorProvider sp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        int[] fdes = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        IOUtil.initPipe(fdes, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        fd0 = fdes[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        fd1 = fdes[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        pollWrapper = new EPollArrayWrapper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        pollWrapper.initInterrupt(fd0, fd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        fdToKey = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    protected int doSelect(long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throw new ClosedSelectorException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        processDeregisterQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            pollWrapper.poll(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        processDeregisterQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        int numKeysUpdated = updateSelectedKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (pollWrapper.interrupted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            // Clear the wakeup pipe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            pollWrapper.putEventOps(pollWrapper.interruptedIndex(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            synchronized (interruptLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                pollWrapper.clearInterrupted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                IOUtil.drain(fd0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                interruptTriggered = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return numKeysUpdated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Update the keys whose fd's have been selected by the epoll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Add the ready keys to the ready queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private int updateSelectedKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        int entries = pollWrapper.updated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int numKeysUpdated = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        for (int i=0; i<entries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            int nextFD = pollWrapper.getDescriptor(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            SelectionKeyImpl ski = (SelectionKeyImpl) fdToKey.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                new Integer(nextFD));
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.getEventOps(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 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (!closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            // prevent further wakeup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            synchronized (interruptLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                interruptTriggered = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            FileDispatcher.closeIntFD(fd0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            FileDispatcher.closeIntFD(fd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (pollWrapper != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                pollWrapper.release(fd0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                pollWrapper.closeEPollFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                pollWrapper = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                selectedKeys = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                // Deregister channels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                Iterator i = keys.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    deregister(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    SelectableChannel selch = ski.channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    if (!selch.isOpen() && !selch.isRegistered())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        ((SelChImpl)selch).kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            fd0 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            fd1 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    protected void implRegister(SelectionKeyImpl ski) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        int fd = IOUtil.fdVal(ski.channel.getFD());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        fdToKey.put(new Integer(fd), ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        pollWrapper.add(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        keys.add(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    protected void implDereg(SelectionKeyImpl ski) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        assert (ski.getIndex() >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        int fd = ski.channel.getFDVal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        fdToKey.remove(new Integer(fd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        pollWrapper.release(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        ski.setIndex(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        keys.remove(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        selectedKeys.remove(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        deregister((AbstractSelectionKey)ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        SelectableChannel selch = ski.channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (!selch.isOpen() && !selch.isRegistered())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            ((SelChImpl)selch).kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    void putEventOps(SelectionKeyImpl sk, int ops) {
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
}