Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/Selector/RegisterDuringSelect.java
51568 views
1
/*
2
* Copyright (c) 2018, 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
/* @test
25
* @bug 8201315
26
* @build SelectorUtils
27
* @run main RegisterDuringSelect
28
* @summary Test that channels can be registered, interest ops can changed,
29
* and keys cancelled while a selection operation is in progress.
30
*/
31
32
import java.io.IOException;
33
import java.nio.channels.Pipe;
34
import java.nio.channels.SelectionKey;
35
import java.nio.channels.Selector;
36
37
public class RegisterDuringSelect {
38
interface TestOperation {
39
void accept(Thread t, Selector sel, Pipe.SourceChannel sc) throws Exception;
40
}
41
static class Test {
42
final Selector sel;
43
final Pipe p;
44
final Pipe.SourceChannel sc;
45
46
Test() throws Exception {
47
sel = Selector.open();
48
p = Pipe.open();
49
sc = p.source();
50
sc.configureBlocking(false);
51
}
52
void test(TestOperation op) throws Exception {
53
try {
54
Thread t = new Thread(() -> {
55
try {
56
sel.select();
57
} catch (IOException ex) {
58
throw new RuntimeException(ex);
59
}
60
});
61
t.start();
62
op.accept(t, sel, sc);
63
} finally {
64
sel.close();
65
p.source().close();
66
p.sink().close();
67
}
68
}
69
}
70
/**
71
* Invoke register, interestOps, and cancel concurrently with a thread
72
* doing a selection operation
73
*/
74
public static void main(String args[]) throws Exception {
75
new Test().test((t, sel, sc) -> {
76
System.out.println("register ...");
77
// spin until make sure select is invoked
78
SelectorUtils.spinUntilLocked(t, sel);
79
SelectionKey key = sc.register(sel, SelectionKey.OP_READ);
80
try {
81
if (!sel.keys().contains(key))
82
throw new RuntimeException("key not in key set");
83
} finally {
84
sel.wakeup();
85
t.join();
86
}
87
});
88
new Test().test((t, sel, sc) -> {
89
System.out.println("interestOps ...");
90
SelectionKey key = sc.register(sel, SelectionKey.OP_READ);
91
// spin until make sure select is invoked
92
SelectorUtils.spinUntilLocked(t, sel);
93
key.interestOps(0);
94
try {
95
if (key.interestOps() != 0)
96
throw new RuntimeException("interested ops not cleared");
97
} finally {
98
sel.wakeup();
99
t.join();
100
}
101
});
102
new Test().test((t, sel, sc) -> {
103
System.out.println("cancel ...");
104
SelectionKey key = sc.register(sel, SelectionKey.OP_READ);
105
// spin until make sure select is invoked
106
SelectorUtils.spinUntilLocked(t, sel);
107
key.cancel();
108
sel.wakeup();
109
t.join();
110
if (sel.keys().contains(key))
111
throw new RuntimeException("key not removed from key set");
112
});
113
}
114
}
115
116