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
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);
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;
|
1 /*
2 * Copyright (c) 2001, 2018, 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
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 final DevPollArrayWrapper pollWrapper;
47
48 // Maps from file descriptors to keys
49 private final Map<Integer,SelectionKeyImpl> fdToKey;
50
51 // True if this Selector has been closed
52 private boolean closed = false;
53
54 // Lock for interrupt triggering and clearing
55 private final Object interruptLock = new Object();
56 private boolean interruptTriggered = false;
57
58 /**
59 * Package private constructor called by factory method in
60 * the abstract superclass Selector.
61 */
62 DevPollSelectorImpl(SelectorProvider sp) {
63 super(sp);
64 long pipeFds = IOUtil.makePipe(false);
65 fd0 = (int) (pipeFds >>> 32);
66 fd1 = (int) pipeFds;
67 try {
68 pollWrapper = new DevPollArrayWrapper();
69 pollWrapper.initInterrupt(fd0, fd1);
70 fdToKey = new HashMap<>();
71 } catch (Throwable t) {
72 try {
73 FileDispatcherImpl.closeIntFD(fd0);
74 } catch (IOException ioe0) {
75 t.addSuppressed(ioe0);
136 }
137 }
138 return numKeysUpdated;
139 }
140
141 protected void implClose() throws IOException {
142 if (closed)
143 return;
144 closed = true;
145
146 // prevent further wakeup
147 synchronized (interruptLock) {
148 interruptTriggered = true;
149 }
150
151 FileDispatcherImpl.closeIntFD(fd0);
152 FileDispatcherImpl.closeIntFD(fd1);
153
154 pollWrapper.release(fd0);
155 pollWrapper.closeDevPollFD();
156 selectedKeys.clear();
157
158 // Deregister channels
159 Iterator<SelectionKey> i = keys.iterator();
160 while (i.hasNext()) {
161 SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
162 deregister(ski);
163 SelectableChannel selch = ski.channel();
164 if (!selch.isOpen() && !selch.isRegistered())
165 ((SelChImpl)selch).kill();
166 i.remove();
167 }
168 fd0 = -1;
169 fd1 = -1;
170 }
171
172 protected void implRegister(SelectionKeyImpl ski) {
173 int fd = IOUtil.fdVal(ski.channel.getFD());
174 fdToKey.put(Integer.valueOf(fd), ski);
175 keys.add(ski);
176 }
177
178 protected void implDereg(SelectionKeyImpl ski) throws IOException {
179 int i = ski.getIndex();
180 assert (i >= 0);
181 int fd = ski.channel.getFDVal();
182 fdToKey.remove(Integer.valueOf(fd));
183 pollWrapper.release(fd);
184 ski.setIndex(-1);
185 keys.remove(ski);
186 selectedKeys.remove(ski);
187 deregister(ski);
188 SelectableChannel selch = ski.channel();
189 if (!selch.isOpen() && !selch.isRegistered())
190 ((SelChImpl)selch).kill();
191 }
192
193 public void putEventOps(SelectionKeyImpl sk, int ops) {
194 if (closed)
195 throw new ClosedSelectorException();
196 int fd = IOUtil.fdVal(sk.channel.getFD());
197 pollWrapper.setInterest(fd, ops);
198 }
199
200 public Selector wakeup() {
201 synchronized (interruptLock) {
202 if (!interruptTriggered) {
203 pollWrapper.interrupt();
204 interruptTriggered = true;
205 }
206 }
207 return this;
|