2
|
1 |
/*
|
|
2 |
* Copyright 2001-2004 Sun Microsystems, Inc. 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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any 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 |
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 |
|
|
61 |
AbstractPollSelectorImpl(SelectorProvider sp, int channels, int offset) {
|
|
62 |
super(sp);
|
|
63 |
this.totalChannels = channels;
|
|
64 |
this.channelOffset = offset;
|
|
65 |
}
|
|
66 |
|
|
67 |
void putEventOps(SelectionKeyImpl sk, int ops) {
|
|
68 |
pollWrapper.putEventOps(sk.getIndex(), ops);
|
|
69 |
}
|
|
70 |
|
|
71 |
public Selector wakeup() {
|
|
72 |
pollWrapper.interrupt();
|
|
73 |
return this;
|
|
74 |
}
|
|
75 |
|
|
76 |
protected abstract int doSelect(long timeout) throws IOException;
|
|
77 |
|
|
78 |
protected void implClose() throws IOException {
|
|
79 |
if (!closed) {
|
|
80 |
closed = true;
|
|
81 |
// Deregister channels
|
|
82 |
for(int i=channelOffset; i<totalChannels; i++) {
|
|
83 |
SelectionKeyImpl ski = channelArray[i];
|
|
84 |
assert(ski.getIndex() != -1);
|
|
85 |
ski.setIndex(-1);
|
|
86 |
deregister(ski);
|
|
87 |
SelectableChannel selch = channelArray[i].channel();
|
|
88 |
if (!selch.isOpen() && !selch.isRegistered())
|
|
89 |
((SelChImpl)selch).kill();
|
|
90 |
}
|
|
91 |
implCloseInterrupt();
|
|
92 |
pollWrapper.free();
|
|
93 |
pollWrapper = null;
|
|
94 |
selectedKeys = null;
|
|
95 |
channelArray = null;
|
|
96 |
totalChannels = 0;
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
protected abstract void implCloseInterrupt() throws IOException;
|
|
101 |
|
|
102 |
/**
|
|
103 |
* Copy the information in the pollfd structs into the opss
|
|
104 |
* of the corresponding Channels. Add the ready keys to the
|
|
105 |
* ready queue.
|
|
106 |
*/
|
|
107 |
protected int updateSelectedKeys() {
|
|
108 |
int numKeysUpdated = 0;
|
|
109 |
// Skip zeroth entry; it is for interrupts only
|
|
110 |
for (int i=channelOffset; i<totalChannels; i++) {
|
|
111 |
int rOps = pollWrapper.getReventOps(i);
|
|
112 |
if (rOps != 0) {
|
|
113 |
SelectionKeyImpl sk = channelArray[i];
|
|
114 |
pollWrapper.putReventOps(i, 0);
|
|
115 |
if (selectedKeys.contains(sk)) {
|
|
116 |
if (sk.channel.translateAndSetReadyOps(rOps, sk)) {
|
|
117 |
numKeysUpdated++;
|
|
118 |
}
|
|
119 |
} else {
|
|
120 |
sk.channel.translateAndSetReadyOps(rOps, sk);
|
|
121 |
if ((sk.nioReadyOps() & sk.nioInterestOps()) != 0) {
|
|
122 |
selectedKeys.add(sk);
|
|
123 |
numKeysUpdated++;
|
|
124 |
}
|
|
125 |
}
|
|
126 |
}
|
|
127 |
}
|
|
128 |
return numKeysUpdated;
|
|
129 |
}
|
|
130 |
|
|
131 |
protected void implRegister(SelectionKeyImpl ski) {
|
|
132 |
// Check to see if the array is large enough
|
|
133 |
if (channelArray.length == totalChannels) {
|
|
134 |
// Make a larger array
|
|
135 |
int newSize = pollWrapper.totalChannels * 2;
|
|
136 |
SelectionKeyImpl temp[] = new SelectionKeyImpl[newSize];
|
|
137 |
// Copy over
|
|
138 |
for (int i=channelOffset; i<totalChannels; i++)
|
|
139 |
temp[i] = channelArray[i];
|
|
140 |
channelArray = temp;
|
|
141 |
// Grow the NativeObject poll array
|
|
142 |
pollWrapper.grow(newSize);
|
|
143 |
}
|
|
144 |
channelArray[totalChannels] = ski;
|
|
145 |
ski.setIndex(totalChannels);
|
|
146 |
pollWrapper.addEntry(ski.channel);
|
|
147 |
totalChannels++;
|
|
148 |
keys.add(ski);
|
|
149 |
}
|
|
150 |
|
|
151 |
protected void implDereg(SelectionKeyImpl ski) throws IOException {
|
|
152 |
// Algorithm: Copy the sc from the end of the list and put it into
|
|
153 |
// the location of the sc to be removed (since order doesn't
|
|
154 |
// matter). Decrement the sc count. Update the index of the sc
|
|
155 |
// that is moved.
|
|
156 |
int i = ski.getIndex();
|
|
157 |
assert (i >= 0);
|
|
158 |
if (i != totalChannels - 1) {
|
|
159 |
// Copy end one over it
|
|
160 |
SelectionKeyImpl endChannel = channelArray[totalChannels-1];
|
|
161 |
channelArray[i] = endChannel;
|
|
162 |
endChannel.setIndex(i);
|
|
163 |
pollWrapper.release(i);
|
|
164 |
PollArrayWrapper.replaceEntry(pollWrapper, totalChannels - 1,
|
|
165 |
pollWrapper, i);
|
|
166 |
} else {
|
|
167 |
pollWrapper.release(i);
|
|
168 |
}
|
|
169 |
// Destroy the last one
|
|
170 |
channelArray[totalChannels-1] = null;
|
|
171 |
totalChannels--;
|
|
172 |
pollWrapper.totalChannels--;
|
|
173 |
ski.setIndex(-1);
|
|
174 |
// Remove the key from keys and selectedKeys
|
|
175 |
keys.remove(ski);
|
|
176 |
selectedKeys.remove(ski);
|
|
177 |
deregister((AbstractSelectionKey)ski);
|
|
178 |
SelectableChannel selch = ski.channel();
|
|
179 |
if (!selch.isOpen() && !selch.isRegistered())
|
|
180 |
((SelChImpl)selch).kill();
|
|
181 |
}
|
|
182 |
|
|
183 |
static {
|
|
184 |
Util.load();
|
|
185 |
}
|
|
186 |
|
|
187 |
}
|