1 /*
2 * junixsocket
3 *
4 * Copyright 2009-2024 Christian Kohlschütter
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.newsclub.net.unix;
19
20 import java.io.Closeable;
21 import java.io.FileDescriptor;
22 import java.io.IOException;
23 import java.net.SocketTimeoutException;
24 import java.nio.channels.SelectionKey;
25
26 /**
27 * Helper class to support polling "virtually blocked" file descriptors for virtual threads.
28 *
29 * @author Christian Kohlschütter
30 */
31 interface VirtualThreadPoller {
32 /**
33 * Returns the default instance best suited for the current system.
34 */
35 VirtualThreadPoller INSTANCE = new VirtualThreadPollerNaive();
36
37 /**
38 * Parks the current thread until the given file descriptor is ready, with respect to the given
39 * readiness mode.
40 *
41 * @param fd The file descriptor to check.
42 * @param mode The mode (bitmask of {@link SelectionKey#OP_READ}, {@link SelectionKey#OP_WRITE},
43 * {@link SelectionKey#OP_ACCEPT}, {@link SelectionKey#OP_CONNECT})
44 * @param now The refence time (in millis) for the timeout
45 * @param timeout (in seconds), or 0 for infinite
46 * @param closeOnInterrupt Callback to call upon interrupt (usually to close the resource working
47 * on the file descriptor)
48 * @throws SocketTimeoutException on timeout
49 * @throws SocketClosedByInterruptException on interrupt.
50 * @throws IOException on other error
51 */
52 void parkThreadUntilReady(FileDescriptor fd, /* SelectionKey.OP_ */ int mode, long now,
53 AFSupplier<Integer> timeout, Closeable closeOnInterrupt) throws IOException;
54 }