jdk/src/share/classes/sun/nio/ch/SelectorImpl.java
author alanb
Mon, 09 May 2011 18:45:33 +0100
changeset 9679 d98ae8bc45fc
parent 5506 202f599c92aa
child 10137 d92637d3d673
permissions -rw-r--r--
7042979: Rename StandardSocketOption and StandardWatchEventKind Reviewed-by: forax, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     2
 * Copyright (c) 2000, 2008, 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: 1247
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: 1247
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: 1247
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
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.net.SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.misc.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Base Selector implementation class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
abstract class SelectorImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    extends AbstractSelector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // The set of keys with data ready for an operation
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 843
diff changeset
    45
    protected Set<SelectionKey> selectedKeys;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // The set of keys registered with this Selector
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 843
diff changeset
    48
    protected HashSet<SelectionKey> keys;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // Public views of the key sets
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 843
diff changeset
    51
    private Set<SelectionKey> publicKeys;             // Immutable
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 843
diff changeset
    52
    private Set<SelectionKey> publicSelectedKeys;     // Removal allowed, but not addition
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    protected SelectorImpl(SelectorProvider sp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        super(sp);
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 843
diff changeset
    56
        keys = new HashSet<SelectionKey>();
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 843
diff changeset
    57
        selectedKeys = new HashSet<SelectionKey>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (Util.atBugLevel("1.4")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            publicKeys = keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            publicSelectedKeys = selectedKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            publicKeys = Collections.unmodifiableSet(keys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            publicSelectedKeys = Util.ungrowableSet(selectedKeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 843
diff changeset
    67
    public Set<SelectionKey> keys() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (!isOpen() && !Util.atBugLevel("1.4"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            throw new ClosedSelectorException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return publicKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 843
diff changeset
    73
    public Set<SelectionKey> selectedKeys() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (!isOpen() && !Util.atBugLevel("1.4"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new ClosedSelectorException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        return publicSelectedKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    protected abstract int doSelect(long timeout) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private int lockAndDoSelect(long timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                throw new ClosedSelectorException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            synchronized (publicKeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                synchronized (publicSelectedKeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    return doSelect(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public int select(long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new IllegalArgumentException("Negative timeout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return lockAndDoSelect((timeout == 0) ? -1 : timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public int select() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return select(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public int selectNow() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return lockAndDoSelect(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public void implCloseSelector() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        wakeup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            synchronized (publicKeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                synchronized (publicSelectedKeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    implClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    protected abstract void implClose() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    void putEventOps(SelectionKeyImpl sk, int ops) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    protected final SelectionKey register(AbstractSelectableChannel ch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                          int ops,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                          Object attachment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (!(ch instanceof SelChImpl))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new IllegalSelectorException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        SelectionKeyImpl k = new SelectionKeyImpl((SelChImpl)ch, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        k.attach(attachment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        synchronized (publicKeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            implRegister(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        k.interestOps(ops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    protected abstract void implRegister(SelectionKeyImpl ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    void processDeregisterQueue() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // Precondition: Synchronized on this, keys, and selectedKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        Set cks = cancelledKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        synchronized (cks) {
843
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   145
            if (!cks.isEmpty()) {
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   146
                Iterator i = cks.iterator();
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   147
                while (i.hasNext()) {
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   148
                    SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   149
                    try {
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   150
                        implDereg(ski);
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   151
                    } catch (SocketException se) {
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   152
                        IOException ioe = new IOException(
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   153
                            "Error deregistering key");
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   154
                        ioe.initCause(se);
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   155
                        throw ioe;
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   156
                    } finally {
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   157
                        i.remove();
e50aa264deca 6429289: (se) sun.nio.ch.SelectorImpl.processDeregisterQueue creates excessive garbage
sherman
parents: 2
diff changeset
   158
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    protected abstract void implDereg(SelectionKeyImpl ski) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    abstract public Selector wakeup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}