src/java.base/share/classes/sun/nio/ch/SelectionKeyImpl.java
author alanb
Fri, 30 Mar 2018 08:28:09 +0100
changeset 49493 814bd31f8da0
parent 49290 07779973cbe2
child 49526 cad4c844902a
permissions -rw-r--r--
8200257: (se) More Selector cleanup Reviewed-by: 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) 2000, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    28
import java.nio.channels.CancelledKeyException;
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    29
import java.nio.channels.SelectableChannel;
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    30
import java.nio.channels.SelectionKey;
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    31
import java.nio.channels.Selector;
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    32
import java.nio.channels.spi.AbstractSelectionKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    36
 * An implementation of SelectionKey.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    39
public final class SelectionKeyImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    extends AbstractSelectionKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    final SelChImpl channel;                            // package-private
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    43
    private final SelectorImpl selector;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private volatile int interestOps;
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
    46
    private volatile int readyOps;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    48
    // registered events in kernel, used by some Selector implementations
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    49
    private int registeredEvents;
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    50
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    51
    // index of key in pollfd array, used by some Selector implementations
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    52
    private int index;
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    53
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    SelectionKeyImpl(SelChImpl ch, SelectorImpl sel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        channel = ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        selector = sel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    59
    private void ensureValid() {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    60
        if (!isValid())
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    61
            throw new CancelledKeyException();
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    62
    }
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    63
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    64
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public SelectableChannel channel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return (SelectableChannel)channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    69
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public Selector selector() {
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
    71
        return selector;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    74
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public int interestOps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return interestOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    80
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public SelectionKey interestOps(int ops) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return nioInterestOps(ops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
    86
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public int readyOps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return readyOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // The nio versions of these operations do not care if a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // has been invalidated. They are for internal use by nio code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 5506
diff changeset
    95
    public void nioReadyOps(int ops) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        readyOps = ops;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 5506
diff changeset
    99
    public int nioReadyOps() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return readyOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 5506
diff changeset
   103
    public SelectionKey nioInterestOps(int ops) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if ((ops & ~channel().validOps()) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new IllegalArgumentException();
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   106
        selector.putEventOps(this, channel.translateInterestOps(ops));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        interestOps = ops;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 5506
diff changeset
   111
    public int nioInterestOps() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return interestOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   115
    void registeredEvents(int events) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   116
        // assert Thread.holdsLock(selector);
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   117
        this.registeredEvents = events;
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   118
    }
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   119
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   120
    int registeredEvents() {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   121
        // assert Thread.holdsLock(selector);
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   122
        return registeredEvents;
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   123
    }
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   124
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   125
    int getIndex() {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   126
        return index;
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   127
    }
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   128
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   129
    void setIndex(int i) {
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   130
        index = i;
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   131
    }
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49290
diff changeset
   132
49248
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   133
    @Override
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   134
    public String toString() {
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   135
        StringBuilder sb = new StringBuilder();
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   136
        sb.append("channel=")
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   137
          .append(channel)
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   138
          .append(", selector=")
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   139
          .append(selector);
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   140
        if (isValid()) {
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   141
            sb.append(", interestOps=")
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   142
              .append(interestOps)
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   143
              .append(", readyOps=")
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   144
              .append(readyOps);
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   145
        } else {
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   146
            sb.append(", invalid");
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   147
        }
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   148
        return sb.toString();
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   149
    }
15a0e60c8b97 8199611: (se) Minor selector implementation clean-up
alanb
parents: 47216
diff changeset
   150
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   151
    // used by Selector implementations to record when the key was selected
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 49248
diff changeset
   152
    int lastPolled;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
}