jdk/src/share/classes/sun/nio/ch/AbstractPollSelectorImpl.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1449 2ed6188288d6
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 2001-2004 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 abstract selector impl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
abstract class AbstractPollSelectorImpl
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
    // The poll fd array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    PollArrayWrapper pollWrapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // Initial capacity of the pollfd array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    protected final int INIT_CAP = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // The list of SelectableChannels serviced by this Selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    protected SelectionKeyImpl[] channelArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // In some impls the first entry of channelArray is bogus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    protected int channelOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // The number of valid channels in this Selector's poll array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    protected int totalChannels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // True if this Selector has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    AbstractPollSelectorImpl(SelectorProvider sp, int channels, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.totalChannels = channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        this.channelOffset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    void putEventOps(SelectionKeyImpl sk, int ops) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        pollWrapper.putEventOps(sk.getIndex(), ops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public Selector wakeup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        pollWrapper.interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return this;
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 abstract int doSelect(long timeout) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    protected void implClose() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (!closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            // Deregister channels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            for(int i=channelOffset; i<totalChannels; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                SelectionKeyImpl ski = channelArray[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                assert(ski.getIndex() != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                ski.setIndex(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                deregister(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                SelectableChannel selch = channelArray[i].channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                if (!selch.isOpen() && !selch.isRegistered())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    ((SelChImpl)selch).kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            implCloseInterrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            pollWrapper.free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            pollWrapper = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            selectedKeys = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            channelArray = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            totalChannels = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    protected abstract void implCloseInterrupt() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Copy the information in the pollfd structs into the opss
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * of the corresponding Channels. Add the ready keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * ready queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    protected int updateSelectedKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int numKeysUpdated = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // Skip zeroth entry; it is for interrupts only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        for (int i=channelOffset; i<totalChannels; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            int rOps = pollWrapper.getReventOps(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (rOps != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                SelectionKeyImpl sk = channelArray[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                pollWrapper.putReventOps(i, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                if (selectedKeys.contains(sk)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    if (sk.channel.translateAndSetReadyOps(rOps, sk)) {
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
                    sk.channel.translateAndSetReadyOps(rOps, sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    if ((sk.nioReadyOps() & sk.nioInterestOps()) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        selectedKeys.add(sk);
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 implRegister(SelectionKeyImpl ski) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // Check to see if the array is large enough
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (channelArray.length == totalChannels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            // Make a larger array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            int newSize = pollWrapper.totalChannels * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            SelectionKeyImpl temp[] = new SelectionKeyImpl[newSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            // Copy over
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            for (int i=channelOffset; i<totalChannels; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                temp[i] = channelArray[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            channelArray = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            // Grow the NativeObject poll array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            pollWrapper.grow(newSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        channelArray[totalChannels] = ski;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        ski.setIndex(totalChannels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        pollWrapper.addEntry(ski.channel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        totalChannels++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        keys.add(ski);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    protected void implDereg(SelectionKeyImpl ski) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // Algorithm: Copy the sc from the end of the list and put it into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // the location of the sc to be removed (since order doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // matter). Decrement the sc count. Update the index of the sc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // that is moved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        int i = ski.getIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        assert (i >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (i != totalChannels - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            // Copy end one over it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            SelectionKeyImpl endChannel = channelArray[totalChannels-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            channelArray[i] = endChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            endChannel.setIndex(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            pollWrapper.release(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            PollArrayWrapper.replaceEntry(pollWrapper, totalChannels - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                          pollWrapper, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            pollWrapper.release(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // Destroy the last one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        channelArray[totalChannels-1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        totalChannels--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        pollWrapper.totalChannels--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        ski.setIndex(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // Remove the key from keys and selectedKeys
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        Util.load();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}