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