author | alanb |
Mon, 16 Apr 2012 20:12:20 +0100 | |
changeset 12438 | c1183d4a80d4 |
parent 12047 | 320a714614e9 |
child 13585 | 4e446fa0c1c0 |
permissions | -rw-r--r-- |
12047 | 1 |
/* |
2 |
* Copyright (c) 2011, 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 |
|
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
12047 | 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 |
/* |
|
27 |
* KQueueArrayWrapper.java |
|
28 |
* Implementation of Selector using FreeBSD / Mac OS X kqueues |
|
29 |
* Derived from Sun's DevPollArrayWrapper |
|
30 |
*/ |
|
31 |
||
32 |
package sun.nio.ch; |
|
33 |
||
34 |
import sun.misc.*; |
|
35 |
import java.io.IOException; |
|
36 |
import java.io.FileDescriptor; |
|
37 |
||
38 |
||
39 |
/* |
|
40 |
* struct kevent { // 32-bit 64-bit |
|
41 |
* uintptr_t ident; // 4 8 |
|
42 |
* short filter; // 2 2 |
|
43 |
* u_short flags; // 2 2 |
|
44 |
* u_int fflags; // 4 4 |
|
45 |
* intptr_t data; // 4 8 |
|
46 |
* void *udata; // 4 8 |
|
47 |
* } // Total: 20 32 |
|
48 |
* |
|
49 |
* The implementation works in 32-bit and 64-bit world. We do this by calling a |
|
50 |
* native function that actually sets the sizes and offsets of the fields based |
|
51 |
* on which mode we're in. |
|
52 |
*/ |
|
53 |
||
54 |
class KQueueArrayWrapper { |
|
55 |
// Event masks |
|
56 |
static final short POLLIN = AbstractPollArrayWrapper.POLLIN; |
|
57 |
static final short POLLOUT = AbstractPollArrayWrapper.POLLOUT; |
|
58 |
||
59 |
// kevent filters |
|
60 |
static short EVFILT_READ; |
|
61 |
static short EVFILT_WRITE; |
|
62 |
||
63 |
// kevent struct |
|
64 |
// These fields are now set by initStructSizes in the static initializer. |
|
65 |
static short SIZEOF_KEVENT; |
|
66 |
static short FD_OFFSET; |
|
67 |
static short FILTER_OFFSET; |
|
68 |
||
12438
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
69 |
// kevent array size |
c1183d4a80d4
7143744: (se) Stabilize KQueue SelectorProvider and make default on MacOSX
alanb
parents:
12047
diff
changeset
|
70 |
static final int NUM_KEVENTS = 128; |
12047 | 71 |
|
72 |
// Are we in a 64-bit VM? |
|
73 |
static boolean is64bit = false; |
|
74 |
||
75 |
// The kevent array (used for outcoming events only) |
|
76 |
private AllocatedNativeObject keventArray = null; |
|
77 |
private long keventArrayAddress; |
|
78 |
||
79 |
// The kqueue fd |
|
80 |
private int kq = -1; |
|
81 |
||
82 |
// The fd of the interrupt line going out |
|
83 |
private int outgoingInterruptFD; |
|
84 |
||
85 |
// The fd of the interrupt line coming in |
|
86 |
private int incomingInterruptFD; |
|
87 |
||
88 |
static { |
|
89 |
initStructSizes(); |
|
90 |
String datamodel = java.security.AccessController.doPrivileged( |
|
91 |
new sun.security.action.GetPropertyAction("sun.arch.data.model") |
|
92 |
); |
|
93 |
is64bit = datamodel.equals("64"); |
|
94 |
} |
|
95 |
||
96 |
KQueueArrayWrapper() { |
|
97 |
int allocationSize = SIZEOF_KEVENT * NUM_KEVENTS; |
|
98 |
keventArray = new AllocatedNativeObject(allocationSize, true); |
|
99 |
keventArrayAddress = keventArray.address(); |
|
100 |
kq = init(); |
|
101 |
} |
|
102 |
||
103 |
void initInterrupt(int fd0, int fd1) { |
|
104 |
outgoingInterruptFD = fd1; |
|
105 |
incomingInterruptFD = fd0; |
|
106 |
register0(kq, fd0, 1, 0); |
|
107 |
} |
|
108 |
||
109 |
int getReventOps(int index) { |
|
110 |
int result = 0; |
|
111 |
int offset = SIZEOF_KEVENT*index + FILTER_OFFSET; |
|
112 |
short filter = keventArray.getShort(offset); |
|
113 |
||
114 |
// This is all that's necessary based on inspection of usage: |
|
115 |
// SinkChannelImpl, SourceChannelImpl, DatagramChannelImpl, |
|
116 |
// ServerSocketChannelImpl, SocketChannelImpl |
|
117 |
if (filter == EVFILT_READ) { |
|
118 |
result |= POLLIN; |
|
119 |
} else if (filter == EVFILT_WRITE) { |
|
120 |
result |= POLLOUT; |
|
121 |
} |
|
122 |
||
123 |
return result; |
|
124 |
} |
|
125 |
||
126 |
int getDescriptor(int index) { |
|
127 |
int offset = SIZEOF_KEVENT*index + FD_OFFSET; |
|
128 |
/* The ident field is 8 bytes in 64-bit world, however the API wants us |
|
129 |
* to return an int. Hence read the 8 bytes but return as an int. |
|
130 |
*/ |
|
131 |
if (is64bit) { |
|
132 |
long fd = keventArray.getLong(offset); |
|
133 |
assert fd <= Integer.MAX_VALUE; |
|
134 |
return (int) fd; |
|
135 |
} else { |
|
136 |
return keventArray.getInt(offset); |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
void setInterest(int fd, int events) { |
|
141 |
register0(kq, fd, events & POLLIN, events & POLLOUT); |
|
142 |
} |
|
143 |
||
144 |
void release(int fd) { |
|
145 |
register0(kq, fd, 0, 0); |
|
146 |
} |
|
147 |
||
148 |
void close() throws IOException { |
|
149 |
if (keventArray != null) { |
|
150 |
keventArray.free(); |
|
151 |
keventArray = null; |
|
152 |
} |
|
153 |
if (kq >= 0) { |
|
154 |
FileDispatcherImpl.closeIntFD(kq); |
|
155 |
kq = -1; |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
int poll(long timeout) { |
|
160 |
int updated = kevent0(kq, keventArrayAddress, NUM_KEVENTS, timeout); |
|
161 |
return updated; |
|
162 |
} |
|
163 |
||
164 |
void interrupt() { |
|
165 |
interrupt(outgoingInterruptFD); |
|
166 |
} |
|
167 |
||
168 |
private native int init(); |
|
169 |
private static native void initStructSizes(); |
|
170 |
||
171 |
private native void register0(int kq, int fd, int read, int write); |
|
172 |
private native int kevent0(int kq, long keventAddress, int keventCount, |
|
173 |
long timeout); |
|
174 |
private static native void interrupt(int fd); |
|
175 |
} |
|
176 |