1 /* 2 * Copyright (c) 2001, 2015, 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 package sun.nio.ch; 27 28 import java.io.IOException; 29 import java.nio.channels.*; 30 import java.nio.channels.spi.*; 31 import java.util.*; 32 33 34 /** 35 * An implementation of Selector for Solaris. 36 */ 37 class DevPollSelectorImpl 38 extends SelectorImpl 39 { 40 41 // File descriptors used for interrupt 42 protected int fd0; 43 protected int fd1; 44 45 // The poll object 46 DevPollArrayWrapper pollWrapper; 47 48 // Maps from file descriptors to keys 49 private Map<Integer,SelectionKeyImpl> fdToKey; 50 51 // True if this Selector has been closed 52 private boolean closed = false; 53 54 // Lock for close/cleanup 55 private Object closeLock = new Object(); 56 57 // Lock for interrupt triggering and clearing 58 private Object interruptLock = new Object(); 59 private boolean interruptTriggered = false; 60 61 /** 62 * Package private constructor called by factory method in 63 * the abstract superclass Selector. 64 */ 65 DevPollSelectorImpl(SelectorProvider sp) { 66 super(sp); 67 long pipeFds = IOUtil.makePipe(false); 68 fd0 = (int) (pipeFds >>> 32); 69 fd1 = (int) pipeFds; 70 try { 71 pollWrapper = new DevPollArrayWrapper(); 72 pollWrapper.initInterrupt(fd0, fd1); 73 fdToKey = new HashMap<>(); 74 } catch (Throwable t) { 75 try { 76 FileDispatcherImpl.closeIntFD(fd0); 77 } catch (IOException ioe0) { 78 t.addSuppressed(ioe0); 79 } 80 try { 81 FileDispatcherImpl.closeIntFD(fd1); 82 } catch (IOException ioe1) { 83 t.addSuppressed(ioe1); 84 } 85 throw t; 86 } 87 } 88 89 protected int doSelect(long timeout) 90 throws IOException 91 { 92 if (closed) 93 throw new ClosedSelectorException(); 94 processDeregisterQueue(); 95 try { 96 begin(); 97 pollWrapper.poll(timeout); 98 } finally { 99 end(); 100 } 101 processDeregisterQueue(); 102 int numKeysUpdated = updateSelectedKeys(); 103 if (pollWrapper.interrupted()) { 104 // Clear the wakeup pipe 105 pollWrapper.putReventOps(pollWrapper.interruptedIndex(), 0); 106 synchronized (interruptLock) { 107 pollWrapper.clearInterrupted(); 108 IOUtil.drain(fd0); 109 interruptTriggered = false; 110 } 111 } 112 return numKeysUpdated; 113 } 114 115 /** 116 * Update the keys whose fd's have been selected by the devpoll 117 * driver. Add the ready keys to the ready queue. 118 */ 119 private int updateSelectedKeys() { 120 int entries = pollWrapper.updated; 121 int numKeysUpdated = 0; 122 for (int i=0; i<entries; i++) { 123 int nextFD = pollWrapper.getDescriptor(i); 124 SelectionKeyImpl ski = fdToKey.get(Integer.valueOf(nextFD)); 125 // ski is null in the case of an interrupt 126 if (ski != null) { 127 int rOps = pollWrapper.getReventOps(i); 128 if (selectedKeys.contains(ski)) { 129 if (ski.channel.translateAndSetReadyOps(rOps, ski)) { 130 numKeysUpdated++; 131 } 132 } else { 133 ski.channel.translateAndSetReadyOps(rOps, ski); 134 if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) { 135 selectedKeys.add(ski); 136 numKeysUpdated++; 137 } 138 } 139 } 140 } 141 return numKeysUpdated; 142 } 143 144 protected void implClose() throws IOException { 145 if (closed) 146 return; 147 closed = true; 148 149 // prevent further wakeup 150 synchronized (interruptLock) { 151 interruptTriggered = true; 152 } 153 154 FileDispatcherImpl.closeIntFD(fd0); 155 FileDispatcherImpl.closeIntFD(fd1); 156 157 pollWrapper.release(fd0); 158 pollWrapper.closeDevPollFD(); 159 selectedKeys = null; 160 161 // Deregister channels 162 Iterator<SelectionKey> i = keys.iterator(); 163 while (i.hasNext()) { 164 SelectionKeyImpl ski = (SelectionKeyImpl)i.next(); 165 deregister(ski); 166 SelectableChannel selch = ski.channel(); 167 if (!selch.isOpen() && !selch.isRegistered()) 168 ((SelChImpl)selch).kill(); 169 i.remove(); 170 } 171 fd0 = -1; 172 fd1 = -1; 173 } 174 175 protected void implRegister(SelectionKeyImpl ski) { 176 int fd = IOUtil.fdVal(ski.channel.getFD()); 177 fdToKey.put(Integer.valueOf(fd), ski); 178 keys.add(ski); 179 } 180 181 protected void implDereg(SelectionKeyImpl ski) throws IOException { 182 int i = ski.getIndex(); 183 assert (i >= 0); 184 int fd = ski.channel.getFDVal(); 185 fdToKey.remove(Integer.valueOf(fd)); 186 pollWrapper.release(fd); 187 ski.setIndex(-1); 188 keys.remove(ski); 189 selectedKeys.remove(ski); 190 deregister((AbstractSelectionKey)ski); 191 SelectableChannel selch = ski.channel(); 192 if (!selch.isOpen() && !selch.isRegistered()) 193 ((SelChImpl)selch).kill(); 194 } 195 196 public void putEventOps(SelectionKeyImpl sk, int ops) { 197 if (closed) 198 throw new ClosedSelectorException(); 199 int fd = IOUtil.fdVal(sk.channel.getFD()); 200 pollWrapper.setInterest(fd, ops); 201 } 202 203 public Selector wakeup() { 204 synchronized (interruptLock) { 205 if (!interruptTriggered) { 206 pollWrapper.interrupt(); 207 interruptTriggered = true; 208 } 209 } 210 return this; 211 } 212 }