author | alanb |
Thu, 15 Mar 2018 10:47:58 +0000 | |
changeset 49248 | 15a0e60c8b97 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49248
15a0e60c8b97
8199611: (se) Minor selector implementation clean-up
alanb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 abstract selector impl. |
|
36 |
*/ |
|
37 |
||
38 |
abstract class AbstractPollSelectorImpl |
|
39 |
extends SelectorImpl |
|
40 |
{ |
|
41 |
||
42 |
// The poll fd array |
|
43 |
PollArrayWrapper pollWrapper; |
|
44 |
||
45 |
// Initial capacity of the pollfd array |
|
46 |
protected final int INIT_CAP = 10; |
|
47 |
||
48 |
// The list of SelectableChannels serviced by this Selector |
|
49 |
protected SelectionKeyImpl[] channelArray; |
|
50 |
||
51 |
// In some impls the first entry of channelArray is bogus |
|
52 |
protected int channelOffset = 0; |
|
53 |
||
54 |
// The number of valid channels in this Selector's poll array |
|
55 |
protected int totalChannels; |
|
56 |
||
57 |
// True if this Selector has been closed |
|
58 |
private boolean closed = false; |
|
59 |
||
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
60 |
// Lock for close and cleanup |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
61 |
private Object closeLock = new Object(); |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
62 |
|
2 | 63 |
AbstractPollSelectorImpl(SelectorProvider sp, int channels, int offset) { |
64 |
super(sp); |
|
65 |
this.totalChannels = channels; |
|
66 |
this.channelOffset = offset; |
|
67 |
} |
|
68 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
5506
diff
changeset
|
69 |
public void putEventOps(SelectionKeyImpl sk, int ops) { |
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
70 |
synchronized (closeLock) { |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
71 |
if (closed) |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
72 |
throw new ClosedSelectorException(); |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
73 |
pollWrapper.putEventOps(sk.getIndex(), ops); |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
74 |
} |
2 | 75 |
} |
76 |
||
77 |
public Selector wakeup() { |
|
78 |
pollWrapper.interrupt(); |
|
79 |
return this; |
|
80 |
} |
|
81 |
||
82 |
protected abstract int doSelect(long timeout) throws IOException; |
|
83 |
||
84 |
protected void implClose() throws IOException { |
|
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
85 |
synchronized (closeLock) { |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
86 |
if (closed) |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
87 |
return; |
2 | 88 |
closed = true; |
89 |
// Deregister channels |
|
90 |
for(int i=channelOffset; i<totalChannels; i++) { |
|
91 |
SelectionKeyImpl ski = channelArray[i]; |
|
92 |
assert(ski.getIndex() != -1); |
|
93 |
ski.setIndex(-1); |
|
94 |
deregister(ski); |
|
95 |
SelectableChannel selch = channelArray[i].channel(); |
|
96 |
if (!selch.isOpen() && !selch.isRegistered()) |
|
97 |
((SelChImpl)selch).kill(); |
|
98 |
} |
|
99 |
implCloseInterrupt(); |
|
100 |
pollWrapper.free(); |
|
101 |
pollWrapper = null; |
|
102 |
channelArray = null; |
|
103 |
totalChannels = 0; |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
protected abstract void implCloseInterrupt() throws IOException; |
|
108 |
||
109 |
/** |
|
110 |
* Copy the information in the pollfd structs into the opss |
|
111 |
* of the corresponding Channels. Add the ready keys to the |
|
112 |
* ready queue. |
|
113 |
*/ |
|
114 |
protected int updateSelectedKeys() { |
|
115 |
int numKeysUpdated = 0; |
|
116 |
// Skip zeroth entry; it is for interrupts only |
|
117 |
for (int i=channelOffset; i<totalChannels; i++) { |
|
118 |
int rOps = pollWrapper.getReventOps(i); |
|
119 |
if (rOps != 0) { |
|
120 |
SelectionKeyImpl sk = channelArray[i]; |
|
121 |
pollWrapper.putReventOps(i, 0); |
|
122 |
if (selectedKeys.contains(sk)) { |
|
123 |
if (sk.channel.translateAndSetReadyOps(rOps, sk)) { |
|
124 |
numKeysUpdated++; |
|
125 |
} |
|
126 |
} else { |
|
127 |
sk.channel.translateAndSetReadyOps(rOps, sk); |
|
128 |
if ((sk.nioReadyOps() & sk.nioInterestOps()) != 0) { |
|
129 |
selectedKeys.add(sk); |
|
130 |
numKeysUpdated++; |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
} |
|
135 |
return numKeysUpdated; |
|
136 |
} |
|
137 |
||
138 |
protected void implRegister(SelectionKeyImpl ski) { |
|
1449
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
139 |
synchronized (closeLock) { |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
140 |
if (closed) |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
141 |
throw new ClosedSelectorException(); |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
142 |
|
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
143 |
// Check to see if the array is large enough |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
144 |
if (channelArray.length == totalChannels) { |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
145 |
// Make a larger array |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
146 |
int newSize = pollWrapper.totalChannels * 2; |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
147 |
SelectionKeyImpl temp[] = new SelectionKeyImpl[newSize]; |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
148 |
// Copy over |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
149 |
for (int i=channelOffset; i<totalChannels; i++) |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
150 |
temp[i] = channelArray[i]; |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
151 |
channelArray = temp; |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
152 |
// Grow the NativeObject poll array |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
153 |
pollWrapper.grow(newSize); |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
154 |
} |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
155 |
channelArray[totalChannels] = ski; |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
156 |
ski.setIndex(totalChannels); |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
157 |
pollWrapper.addEntry(ski.channel); |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
158 |
totalChannels++; |
2ed6188288d6
5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents:
2
diff
changeset
|
159 |
keys.add(ski); |
2 | 160 |
} |
161 |
} |
|
162 |
||
163 |
protected void implDereg(SelectionKeyImpl ski) throws IOException { |
|
164 |
// Algorithm: Copy the sc from the end of the list and put it into |
|
165 |
// the location of the sc to be removed (since order doesn't |
|
166 |
// matter). Decrement the sc count. Update the index of the sc |
|
167 |
// that is moved. |
|
168 |
int i = ski.getIndex(); |
|
169 |
assert (i >= 0); |
|
170 |
if (i != totalChannels - 1) { |
|
171 |
// Copy end one over it |
|
172 |
SelectionKeyImpl endChannel = channelArray[totalChannels-1]; |
|
173 |
channelArray[i] = endChannel; |
|
174 |
endChannel.setIndex(i); |
|
175 |
pollWrapper.release(i); |
|
176 |
PollArrayWrapper.replaceEntry(pollWrapper, totalChannels - 1, |
|
177 |
pollWrapper, i); |
|
178 |
} else { |
|
179 |
pollWrapper.release(i); |
|
180 |
} |
|
181 |
// Destroy the last one |
|
182 |
channelArray[totalChannels-1] = null; |
|
183 |
totalChannels--; |
|
184 |
pollWrapper.totalChannels--; |
|
185 |
ski.setIndex(-1); |
|
186 |
// Remove the key from keys and selectedKeys |
|
187 |
keys.remove(ski); |
|
188 |
selectedKeys.remove(ski); |
|
189 |
deregister((AbstractSelectionKey)ski); |
|
190 |
SelectableChannel selch = ski.channel(); |
|
191 |
if (!selch.isOpen() && !selch.isRegistered()) |
|
192 |
((SelChImpl)selch).kill(); |
|
193 |
} |
|
194 |
} |