jdk/src/macosx/classes/sun/nio/ch/KQueueSelectorImpl.java
author alanb
Mon, 16 Apr 2012 20:12:20 +0100
changeset 12438 c1183d4a80d4
parent 12047 320a714614e9
child 13585 4e446fa0c1c0
permissions -rw-r--r--
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX Reviewed-by: michaelm, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     1
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     4
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
12438
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    10
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    15
 * accompanied this code).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    16
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    20
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    23
 * questions.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    24
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    25
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    26
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    27
 * KQueueSelectorImpl.java
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    28
 * Implementation of Selector using FreeBSD / Mac OS X kqueues
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    29
 * Derived from Sun's DevPollSelectorImpl
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    30
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    31
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    32
package sun.nio.ch;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    33
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    34
import java.io.IOException;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    35
import java.io.FileDescriptor;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    36
import java.nio.channels.*;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    37
import java.nio.channels.spi.*;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    38
import java.util.*;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    39
import sun.misc.*;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    40
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    41
class KQueueSelectorImpl
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    42
    extends SelectorImpl
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    43
{
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    44
    // File descriptors used for interrupt
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    45
    protected int fd0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    46
    protected int fd1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    47
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    48
    // The kqueue manipulator
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    49
    KQueueArrayWrapper kqueueWrapper;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    50
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    51
    // Count of registered descriptors (including interrupt)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    52
    private int totalChannels;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    53
12438
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
    54
    // Map from a file descriptor to an entry containing the selection key
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
    55
    private HashMap<Integer,MapEntry> fdMap;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    56
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    57
    // True if this Selector has been closed
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    58
    private boolean closed = false;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    59
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    60
    // Lock for interrupt triggering and clearing
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    61
    private Object interruptLock = new Object();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    62
    private boolean interruptTriggered = false;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    63
12438
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
    64
    // used by updateSelectedKeys to handle cases where the same file
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
    65
    // descriptor is polled by more than one filter
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
    66
    private long updateCount;
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
    67
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
    68
    // Used to map file descriptors to a selection key and "update count"
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
    69
    // (see updateSelectedKeys for usage).
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
    70
    private static class MapEntry {
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
    71
        SelectionKeyImpl ski;
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
    72
        long updateCount;
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
    73
        MapEntry(SelectionKeyImpl ski) {
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
    74
            this.ski = ski;
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
    75
        }
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
    76
    }
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
    77
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    78
    /**
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    79
     * Package private constructor called by factory method in
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    80
     * the abstract superclass Selector.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    81
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    82
    KQueueSelectorImpl(SelectorProvider sp) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    83
        super(sp);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    84
        long fds = IOUtil.makePipe(false);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    85
        fd0 = (int)(fds >>> 32);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    86
        fd1 = (int)fds;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    87
        kqueueWrapper = new KQueueArrayWrapper();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    88
        kqueueWrapper.initInterrupt(fd0, fd1);
12438
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
    89
        fdMap = new HashMap<>();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    90
        totalChannels = 1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    91
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    92
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    93
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    94
    protected int doSelect(long timeout)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    95
        throws IOException
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    96
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    97
        int entries = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    98
        if (closed)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    99
            throw new ClosedSelectorException();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   100
        processDeregisterQueue();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   101
        try {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   102
            begin();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   103
            entries = kqueueWrapper.poll(timeout);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   104
        } finally {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   105
            end();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   106
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   107
        processDeregisterQueue();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   108
        return updateSelectedKeys(entries);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   109
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   110
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   111
    /**
12438
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   112
     * Update the keys whose fd's have been selected by kqueue.
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   113
     * Add the ready keys to the selected key set.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   114
     * If the interrupt fd has been selected, drain it and clear the interrupt.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   115
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   116
    private int updateSelectedKeys(int entries)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   117
        throws IOException
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   118
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   119
        int numKeysUpdated = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   120
        boolean interrupted = false;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   121
12438
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   122
        // A file descriptor may be registered with kqueue with more than one
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   123
        // filter and so there may be more than one event for a fd. The update
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   124
        // count in the MapEntry tracks when the fd was last updated and this
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   125
        // ensures that the ready ops are updated rather than replaced by a
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   126
        // second or subsequent event.
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   127
        updateCount++;
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   128
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   129
        for (int i = 0; i < entries; i++) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   130
            int nextFD = kqueueWrapper.getDescriptor(i);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   131
            if (nextFD == fd0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   132
                interrupted = true;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   133
            } else {
12438
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   134
                MapEntry me = fdMap.get(Integer.valueOf(nextFD));
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   135
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   136
                // entry is null in the case of an interrupt
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   137
                if (me != null) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   138
                    int rOps = kqueueWrapper.getReventOps(i);
12438
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   139
                    SelectionKeyImpl ski = me.ski;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   140
                    if (selectedKeys.contains(ski)) {
12438
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   141
                        // first time this file descriptor has been encountered on this
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   142
                        // update?
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   143
                        if (me.updateCount != updateCount) {
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   144
                            if (ski.channel.translateAndSetReadyOps(rOps, ski)) {
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   145
                                numKeysUpdated++;
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   146
                                me.updateCount = updateCount;
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   147
                            }
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   148
                        } else {
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   149
                            // ready ops have already been set on this update
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   150
                            ski.channel.translateAndUpdateReadyOps(rOps, ski);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   151
                        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   152
                    } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   153
                        ski.channel.translateAndSetReadyOps(rOps, ski);
12438
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   154
                        if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   155
                            selectedKeys.add(ski);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   156
                            numKeysUpdated++;
12438
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   157
                            me.updateCount = updateCount;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   158
                        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   159
                    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   160
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   161
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   162
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   163
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   164
        if (interrupted) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   165
            // Clear the wakeup pipe
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   166
            synchronized (interruptLock) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   167
                IOUtil.drain(fd0);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   168
                interruptTriggered = false;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   169
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   170
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   171
        return numKeysUpdated;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   172
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   173
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   174
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   175
    protected void implClose() throws IOException {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   176
        if (!closed) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   177
            closed = true;
12438
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   178
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   179
            // prevent further wakeup
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   180
            synchronized (interruptLock) {
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   181
                interruptTriggered = true;
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   182
            }
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   183
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   184
            FileDispatcherImpl.closeIntFD(fd0);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   185
            FileDispatcherImpl.closeIntFD(fd1);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   186
            if (kqueueWrapper != null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   187
                kqueueWrapper.release(fd0);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   188
                kqueueWrapper.close();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   189
                kqueueWrapper = null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   190
                selectedKeys = null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   191
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   192
                // Deregister channels
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   193
                Iterator<SelectionKey> i = keys.iterator();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   194
                while (i.hasNext()) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   195
                    SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   196
                    deregister(ski);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   197
                    SelectableChannel selch = ski.channel();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   198
                    if (!selch.isOpen() && !selch.isRegistered())
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   199
                        ((SelChImpl)selch).kill();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   200
                    i.remove();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   201
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   202
                totalChannels = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   203
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   204
            fd0 = -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   205
            fd1 = -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   206
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   207
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   208
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   209
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   210
    protected void implRegister(SelectionKeyImpl ski) {
12438
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   211
        if (closed)
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   212
            throw new ClosedSelectorException();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   213
        int fd = IOUtil.fdVal(ski.channel.getFD());
12438
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   214
        fdMap.put(Integer.valueOf(fd), new MapEntry(ski));
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   215
        totalChannels++;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   216
        keys.add(ski);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   217
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   218
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   219
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   220
    protected void implDereg(SelectionKeyImpl ski) throws IOException {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   221
        int fd = ski.channel.getFDVal();
12438
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   222
        fdMap.remove(Integer.valueOf(fd));
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   223
        kqueueWrapper.release(fd);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   224
        totalChannels--;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   225
        keys.remove(ski);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   226
        selectedKeys.remove(ski);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   227
        deregister((AbstractSelectionKey)ski);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   228
        SelectableChannel selch = ski.channel();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   229
        if (!selch.isOpen() && !selch.isRegistered())
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   230
            ((SelChImpl)selch).kill();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   231
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   232
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   233
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   234
    public void putEventOps(SelectionKeyImpl ski, int ops) {
12438
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   235
        if (closed)
c1183d4a80d4 7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents: 12047
diff changeset
   236
            throw new ClosedSelectorException();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   237
        int fd = IOUtil.fdVal(ski.channel.getFD());
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   238
        kqueueWrapper.setInterest(fd, ops);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   239
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   240
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   241
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   242
    public Selector wakeup() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   243
        synchronized (interruptLock) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   244
            if (!interruptTriggered) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   245
                kqueueWrapper.interrupt();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   246
                interruptTriggered = true;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   247
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   248
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   249
        return this;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   250
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   251
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   252
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   253
    static {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   254
        Util.load();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   255
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   256
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   257