View Javadoc
1   /*
2    * junixsocket
3    *
4    * Copyright 2009-2026 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  @SuppressWarnings("PMD.ImplicitFunctionalInterface")
32  interface VirtualThreadPoller {
33    /**
34     * Returns the default instance best suited for the current system.
35     */
36    VirtualThreadPoller INSTANCE = new VirtualThreadPollerNaive();
37  
38    /**
39     * Parks the current thread until the given file descriptor is ready, with respect to the given
40     * readiness mode.
41     *
42     * @param fd The file descriptor to check.
43     * @param mode The mode (bitmask of {@link SelectionKey#OP_READ}, {@link SelectionKey#OP_WRITE},
44     *          {@link SelectionKey#OP_ACCEPT}, {@link SelectionKey#OP_CONNECT})
45     * @param now The refence time (in millis) for the timeout
46     * @param timeout (in seconds), or 0 for infinite
47     * @param closeOnInterrupt Callback to call upon interrupt (usually to close the resource working
48     *          on the file descriptor)
49     * @throws SocketTimeoutException on timeout
50     * @throws SocketClosedByInterruptException on interrupt.
51     * @throws IOException on other error
52     */
53    void parkThreadUntilReady(FileDescriptor fd, /* SelectionKey.OP_ */ int mode, long now,
54        AFSupplier<Integer> timeout, Closeable closeOnInterrupt) throws IOException;
55  }