Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/Selector/RacyDeregister.java
38828 views
1
/*
2
* Copyright (c) 2013, 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
38
* @summary SelectionKey.interestOps does not update interest set on Windows.
39
* @author Frank Ding
40
*/
41
public class RacyDeregister {
42
43
static boolean notified;
44
static final Object selectorLock = new Object();
45
static final Object notifyLock = new Object();
46
/**
47
* null: not terminated
48
* true: passed
49
* false: failed
50
*/
51
static volatile Boolean succTermination = null;
52
53
public static void main(String[] args) throws Exception {
54
InetAddress addr = InetAddress.getByName(null);
55
ServerSocketChannel sc = ServerSocketChannel.open();
56
sc.socket().bind(new InetSocketAddress(addr, 0));
57
58
SocketChannel.open(new InetSocketAddress(addr,
59
sc.socket().getLocalPort()));
60
61
SocketChannel accepted = sc.accept();
62
accepted.configureBlocking(false);
63
64
SocketChannel.open(new InetSocketAddress(addr,
65
sc.socket().getLocalPort()));
66
SocketChannel accepted2 = sc.accept();
67
accepted2.configureBlocking(false);
68
69
final Selector sel = Selector.open();
70
SelectionKey key2 = accepted2.register(sel, SelectionKey.OP_READ);
71
final SelectionKey[] key = new SelectionKey[]{
72
accepted.register(sel, SelectionKey.OP_READ)};
73
74
75
// thread that will be changing key[0].interestOps to OP_READ | OP_WRITE
76
new Thread() {
77
78
public void run() {
79
try {
80
for (int k = 0; k < 15; k++) {
81
for (int i = 0; i < 10000; i++) {
82
synchronized (notifyLock) {
83
synchronized (selectorLock) {
84
sel.wakeup();
85
key[0].interestOps(SelectionKey.OP_READ
86
| SelectionKey.OP_WRITE);
87
}
88
notified = false;
89
long beginTime = System.currentTimeMillis();
90
while (true) {
91
notifyLock.wait(5000);
92
if (notified) {
93
break;
94
}
95
long endTime = System.currentTimeMillis();
96
if (endTime - beginTime > 5000) {
97
succTermination = false;
98
// wake up main thread doing select()
99
sel.wakeup();
100
return;
101
}
102
}
103
}
104
}
105
}
106
succTermination = true;
107
// wake up main thread doing select()
108
sel.wakeup();
109
} catch (Exception e) {
110
System.out.println(e);
111
succTermination = true;
112
// wake up main thread doing select()
113
sel.wakeup();
114
}
115
}
116
}.start();
117
118
// main thread will be doing registering/deregistering with the sel
119
while (true) {
120
sel.select();
121
if (Boolean.TRUE.equals(succTermination)) {
122
System.out.println("Test passed");
123
sel.close();
124
sc.close();
125
break;
126
} else if (Boolean.FALSE.equals(succTermination)) {
127
System.out.println("Failed to pass the test");
128
sel.close();
129
sc.close();
130
throw new RuntimeException("Failed to pass the test");
131
}
132
synchronized (selectorLock) {
133
}
134
if (sel.selectedKeys().contains(key[0]) && key[0].isWritable()) {
135
synchronized (notifyLock) {
136
notified = true;
137
notifyLock.notify();
138
key[0].cancel();
139
sel.selectNow();
140
key2 = accepted2.register(sel, SelectionKey.OP_READ);
141
key[0] = accepted.register(sel, SelectionKey.OP_READ);
142
}
143
}
144
key2.cancel();
145
sel.selectedKeys().clear();
146
}
147
}
148
}
149
150