1 /*
2 * Copyright (c) 2005, 2024, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 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 package sun.nio.ch;
27
28 import java.io.IOException;
29 import java.nio.channels.SelectionKey;
30 import java.nio.channels.Selector;
31 import java.nio.channels.spi.SelectorProvider;
32 import java.util.ArrayDeque;
33 import java.util.Deque;
34 import java.util.HashMap;
35 import java.util.Map;
36 import java.util.concurrent.TimeUnit;
37 import java.util.function.Consumer;
38 import jdk.internal.misc.Blocker;
39
40 import static sun.nio.ch.EPoll.EPOLLIN;
41 import static sun.nio.ch.EPoll.EPOLL_CTL_ADD;
42 import static sun.nio.ch.EPoll.EPOLL_CTL_DEL;
43 import static sun.nio.ch.EPoll.EPOLL_CTL_MOD;
44
45
46 /**
47 * Linux epoll based Selector implementation
48 */
49
50 class EPollSelectorImpl extends SelectorImpl {
51
52 // maximum number of events to poll in one call to epoll_wait
53 private static final int NUM_EPOLLEVENTS = Math.min(IOUtil.fdLimit(), 1024);
54
55 // epoll file descriptor
56 private final int epfd;
57
58 // address of poll array when polling with epoll_wait
88 }
89
90 // register the eventfd object for wakeups
91 EPoll.ctl(epfd, EPOLL_CTL_ADD, eventfd.efd(), EPOLLIN);
92 }
93
94 @Override
95 protected int doSelect(Consumer<SelectionKey> action, long timeout)
96 throws IOException
97 {
98 assert Thread.holdsLock(this);
99
100 // epoll_wait timeout is int
101 int to = (int) Math.min(timeout, Integer.MAX_VALUE);
102 boolean blocking = (to != 0);
103 boolean timedPoll = (to > 0);
104
105 int numEntries;
106 processUpdateQueue();
107 processDeregisterQueue();
108 try {
109 begin(blocking);
110
111 do {
112 long startTime = timedPoll ? System.nanoTime() : 0;
113 boolean attempted = Blocker.begin(blocking);
114 try {
115 numEntries = EPoll.wait(epfd, pollArrayAddress, NUM_EPOLLEVENTS, to);
116 } finally {
117 Blocker.end(attempted);
118 }
119 if (numEntries == IOStatus.INTERRUPTED && timedPoll) {
120 // timed poll interrupted so need to adjust timeout
121 long adjust = System.nanoTime() - startTime;
122 to -= (int) TimeUnit.NANOSECONDS.toMillis(adjust);
123 if (to <= 0) {
124 // timeout expired so no retry
125 numEntries = 0;
126 }
127 }
128 } while (numEntries == IOStatus.INTERRUPTED);
129 assert IOStatus.check(numEntries);
130
131 } finally {
132 end(blocking);
133 }
134 processDeregisterQueue();
135 return processEvents(numEntries, action);
136 }
137
138 /**
139 * Process changes to the interest ops.
140 */
141 private void processUpdateQueue() {
142 assert Thread.holdsLock(this);
143
144 synchronized (updateLock) {
145 SelectionKeyImpl ski;
146 while ((ski = updateKeys.pollFirst()) != null) {
147 if (ski.isValid()) {
148 int fd = ski.getFDVal();
149 // add to fdToKey if needed
150 SelectionKeyImpl previous = fdToKey.putIfAbsent(fd, ski);
151 assert (previous == null) || (previous == ski);
152
153 int newEvents = ski.translateInterestOps();
154 int registeredEvents = ski.registeredEvents();
155 if (newEvents != registeredEvents) {
156 if (newEvents == 0) {
157 // remove from epoll
|
1 /*
2 * Copyright (c) 2005, 2022, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 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 package sun.nio.ch;
27
28 import java.io.IOException;
29 import java.nio.channels.SelectionKey;
30 import java.nio.channels.Selector;
31 import java.nio.channels.spi.SelectorProvider;
32 import java.util.ArrayDeque;
33 import java.util.Deque;
34 import java.util.HashMap;
35 import java.util.Map;
36 import java.util.concurrent.TimeUnit;
37 import java.util.function.Consumer;
38
39 import static sun.nio.ch.EPoll.EPOLLIN;
40 import static sun.nio.ch.EPoll.EPOLL_CTL_ADD;
41 import static sun.nio.ch.EPoll.EPOLL_CTL_DEL;
42 import static sun.nio.ch.EPoll.EPOLL_CTL_MOD;
43
44
45 /**
46 * Linux epoll based Selector implementation
47 */
48
49 class EPollSelectorImpl extends SelectorImpl {
50
51 // maximum number of events to poll in one call to epoll_wait
52 private static final int NUM_EPOLLEVENTS = Math.min(IOUtil.fdLimit(), 1024);
53
54 // epoll file descriptor
55 private final int epfd;
56
57 // address of poll array when polling with epoll_wait
87 }
88
89 // register the eventfd object for wakeups
90 EPoll.ctl(epfd, EPOLL_CTL_ADD, eventfd.efd(), EPOLLIN);
91 }
92
93 @Override
94 protected int doSelect(Consumer<SelectionKey> action, long timeout)
95 throws IOException
96 {
97 assert Thread.holdsLock(this);
98
99 // epoll_wait timeout is int
100 int to = (int) Math.min(timeout, Integer.MAX_VALUE);
101 boolean blocking = (to != 0);
102 boolean timedPoll = (to > 0);
103
104 int numEntries;
105 processUpdateQueue();
106 processDeregisterQueue();
107
108 if (Thread.currentThread().isVirtual()) {
109 numEntries = (timedPoll)
110 ? timedPoll(TimeUnit.MILLISECONDS.toNanos(to))
111 : untimedPoll(blocking);
112 } else {
113 try {
114 begin(blocking);
115 do {
116 long startTime = timedPoll ? System.nanoTime() : 0;
117 numEntries = EPoll.wait(epfd, pollArrayAddress, NUM_EPOLLEVENTS, to);
118 if (numEntries == IOStatus.INTERRUPTED && timedPoll) {
119 // timed poll interrupted so need to adjust timeout
120 long adjust = System.nanoTime() - startTime;
121 to -= (int) TimeUnit.NANOSECONDS.toMillis(adjust);
122 if (to <= 0) {
123 // timeout expired so no retry
124 numEntries = 0;
125 }
126 }
127 } while (numEntries == IOStatus.INTERRUPTED);
128 } finally {
129 end(blocking);
130 }
131 }
132 assert IOStatus.check(numEntries);
133
134 processDeregisterQueue();
135 return processEvents(numEntries, action);
136 }
137
138 /**
139 * If blocking, parks the current virtual thread until a file descriptor is polled
140 * or the thread is interrupted.
141 */
142 private int untimedPoll(boolean block) throws IOException {
143 int numEntries = EPoll.wait(epfd, pollArrayAddress, NUM_EPOLLEVENTS, 0);
144 if (block) {
145 while (numEntries == 0 && !Thread.currentThread().isInterrupted()) {
146 Poller.pollSelector(epfd, 0);
147 numEntries = EPoll.wait(epfd, pollArrayAddress, NUM_EPOLLEVENTS, 0);
148 }
149 }
150 return numEntries;
151 }
152
153 /**
154 * Parks the current virtual thread until a file descriptor is polled, or the thread
155 * is interrupted, for up to the specified waiting time.
156 */
157 private int timedPoll(long nanos) throws IOException {
158 long startNanos = System.nanoTime();
159 int numEntries = EPoll.wait(epfd, pollArrayAddress, NUM_EPOLLEVENTS, 0);
160 while (numEntries == 0 && !Thread.currentThread().isInterrupted()) {
161 long remainingNanos = nanos - (System.nanoTime() - startNanos);
162 if (remainingNanos <= 0) {
163 // timeout
164 break;
165 }
166 Poller.pollSelector(epfd, remainingNanos);
167 numEntries = EPoll.wait(epfd, pollArrayAddress, NUM_EPOLLEVENTS, 0);
168 }
169 return numEntries;
170 }
171
172 /**
173 * Process changes to the interest ops.
174 */
175 private void processUpdateQueue() {
176 assert Thread.holdsLock(this);
177
178 synchronized (updateLock) {
179 SelectionKeyImpl ski;
180 while ((ski = updateKeys.pollFirst()) != null) {
181 if (ski.isValid()) {
182 int fd = ski.getFDVal();
183 // add to fdToKey if needed
184 SelectionKeyImpl previous = fdToKey.putIfAbsent(fd, ski);
185 assert (previous == null) || (previous == ski);
186
187 int newEvents = ski.translateInterestOps();
188 int registeredEvents = ski.registeredEvents();
189 if (newEvents != registeredEvents) {
190 if (newEvents == 0) {
191 // remove from epoll
|