Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/Selector/RacyDeregister.java
51568 views
1
/*
2
* Copyright (c) 2013, 2021, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* Portions Copyright (c) 2012 IBM Corporation
26
*/
27
28
import java.net.InetAddress;
29
import java.net.InetSocketAddress;
30
import java.nio.channels.SelectionKey;
31
import java.nio.channels.Selector;
32
import java.nio.channels.ServerSocketChannel;
33
import java.nio.channels.SocketChannel;
34
35
/*
36
* @test
37
* @bug 6429204 8203766 8205641
38
* @summary SelectionKey.interestOps does not update interest set on Windows.
39
* @author Frank Ding
40
* @run main/timeout=1200 RacyDeregister
41
*/
42
43
/* @test
44
* @requires (os.family == "windows")
45
* @run main/othervm/timeout=1200 -Djava.nio.channels.spi.SelectorProvider=sun.nio.ch.WindowsSelectorProvider RacyDeregister
46
*/
47
48
public class RacyDeregister {
49
50
// 90% of 1200 second timeout as milliseconds
51
static final int TIMEOUT_THRESHOLD_MILLIS = 1200*900;
52
53
// Time at start of main().
54
static long t0;
55
56
static boolean notified;
57
static final Object selectorLock = new Object();
58
static final Object notifyLock = new Object();
59
/**
60
* null: not terminated
61
* true: passed
62
* false: failed
63
*/
64
static volatile Boolean succTermination = null;
65
66
public static void main(String[] args) throws Exception {
67
t0 = System.currentTimeMillis();
68
69
InetAddress addr = InetAddress.getByName(null);
70
ServerSocketChannel sc = ServerSocketChannel.open();
71
sc.socket().bind(new InetSocketAddress(addr, 0));
72
73
SocketChannel.open(new InetSocketAddress(addr,
74
sc.socket().getLocalPort()));
75
76
SocketChannel accepted = sc.accept();
77
accepted.configureBlocking(false);
78
79
SocketChannel.open(new InetSocketAddress(addr,
80
sc.socket().getLocalPort()));
81
SocketChannel accepted2 = sc.accept();
82
accepted2.configureBlocking(false);
83
84
final Selector sel = Selector.open();
85
SelectionKey key2 = accepted2.register(sel, SelectionKey.OP_READ);
86
final SelectionKey[] key = new SelectionKey[]{
87
accepted.register(sel, SelectionKey.OP_READ)};
88
89
// thread that will be changing key[0].interestOps to OP_READ | OP_WRITE
90
new Thread() {
91
92
public void run() {
93
try {
94
for (int k = 0; k < 15; k++) {
95
System.out.format("outer loop %3d at %7d ms%n", k,
96
System.currentTimeMillis() - t0);
97
System.out.flush();
98
for (int i = 0; i < 10000; i++) {
99
synchronized (notifyLock) {
100
synchronized (selectorLock) {
101
sel.wakeup();
102
key[0].interestOps(SelectionKey.OP_READ
103
| SelectionKey.OP_WRITE);
104
}
105
notified = false;
106
long beginTime = System.currentTimeMillis();
107
while (true) {
108
notifyLock.wait(5000);
109
if (notified) {
110
break;
111
}
112
long endTime = System.currentTimeMillis();
113
if (endTime - beginTime > 5000) {
114
for (int j = 0; j < 60; j++) {
115
Thread.sleep(1000);
116
if (notified) {
117
long t =
118
System.currentTimeMillis();
119
System.err.printf
120
("Notified after %d ms%n",
121
t - beginTime);
122
System.err.flush();
123
break;
124
}
125
}
126
succTermination = false;
127
// wake up main thread doing select()
128
sel.wakeup();
129
return;
130
}
131
}
132
}
133
}
134
long t = System.currentTimeMillis();
135
if (t - t0 > TIMEOUT_THRESHOLD_MILLIS) {
136
System.err.format
137
("Timeout after %d outer loop iterations%n", k);
138
System.err.flush();
139
succTermination = false;
140
// wake up main thread doing select()
141
sel.wakeup();
142
return;
143
}
144
}
145
succTermination = true;
146
// wake up main thread doing select()
147
sel.wakeup();
148
} catch (Exception e) {
149
System.out.println(e);
150
System.out.flush();
151
succTermination = true;
152
// wake up main thread doing select()
153
sel.wakeup();
154
}
155
}
156
}.start();
157
158
// main thread will be doing registering/deregistering with the sel
159
while (true) {
160
sel.select();
161
if (Boolean.TRUE.equals(succTermination)) {
162
System.out.println("Test passed");
163
System.out.flush();
164
sel.close();
165
sc.close();
166
break;
167
} else if (Boolean.FALSE.equals(succTermination)) {
168
System.err.println("Failed to pass the test");
169
System.err.flush();
170
sel.close();
171
sc.close();
172
throw new RuntimeException("Failed to pass the test");
173
}
174
synchronized (selectorLock) {
175
}
176
if (sel.selectedKeys().contains(key[0]) && key[0].isWritable()) {
177
synchronized (notifyLock) {
178
notified = true;
179
notifyLock.notify();
180
key[0].cancel();
181
sel.selectNow();
182
key2 = accepted2.register(sel, SelectionKey.OP_READ);
183
key[0] = accepted.register(sel, SelectionKey.OP_READ);
184
}
185
}
186
key2.cancel();
187
sel.selectedKeys().clear();
188
}
189
}
190
}
191
192