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