1 /*
2 * Copyright (c) 2017, 2023, 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 package sun.nio.ch;
26
27 import java.io.IOException;
28
29 /**
30 * Provider class for Poller implementations.
31 */
32 abstract class PollerProvider {
33 private static final PollerProvider INSTANCE = new DefaultPollerProvider();
34
35 PollerProvider() { }
36
37 /**
38 * Returns the system-wide PollerProvider.
39 */
40 static PollerProvider provider() {
41 return INSTANCE;
42 }
43
44 /**
45 * Returns the default poller mode.
46 * @implSpec The default implementation uses system threads.
47 */
48 Poller.Mode defaultPollerMode() {
49 return Poller.Mode.SYSTEM_THREADS;
50 }
51
52 /**
53 * Default number of read pollers for the given mode. The count must be a power of 2.
54 * @implSpec The default implementation returns 1.
55 */
56 int defaultReadPollers(Poller.Mode mode) {
57 return 1;
58 }
59
60 /**
61 * Default number of write pollers for the given mode. The count must be a power of 2.
62 * @implSpec The default implementation returns 1.
63 */
64 int defaultWritePollers(Poller.Mode mode) {
65 return 1;
66 }
67
68 /**
69 * Maps a file descriptor to an index from 0 to {@code toIndex}.
70 * @implSpec The default implementation is good for Unix file descriptors.
71 */
72 int fdValToIndex(int fdVal, int toIndex) {
73 return fdVal & (toIndex - 1);
74 }
75
76 /**
77 * Creates a Poller for read ops.
78 * @param subPoller true to create a sub-poller
79 */
80 abstract Poller readPoller(boolean subPoller) throws IOException;
81
82 /**
83 * Creates a Poller for write ops.
84 * @param subPoller true to create a sub-poller
85 */
86 abstract Poller writePoller(boolean subPoller) throws IOException;
87 }