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 final Poller.Mode mode;
34
35 PollerProvider(Poller.Mode mode) {
36 this.mode = mode;
37 }
38
39 final Poller.Mode pollerMode() {
40 return mode;
41 }
42
43 /**
44 * Creates a PollerProvider that uses its preferred/default poller mode.
45 */
46 static PollerProvider createProvider() {
47 return new DefaultPollerProvider();
48 }
49
50 /**
51 * Creates a PollerProvider that uses the given poller mode.
52 */
53 static PollerProvider createProvider(Poller.Mode mode) {
54 return new DefaultPollerProvider(mode);
55 }
56
57 /**
58 * Default number of read pollers. The count must be a power of 2.
59 * @implSpec The default implementation returns 1.
60 */
61 int defaultReadPollers() {
62 return 1;
63 }
64
65 /**
66 * Default number of write pollers. The count must be a power of 2.
67 * @implSpec The default implementation returns 1.
68 */
69 int defaultWritePollers() {
70 return 1;
71 }
72
73 /**
74 * Maps a file descriptor to an index from 0 to {@code toIndex}.
75 * @implSpec The default implementation is good for Unix file descriptors.
76 */
77 int fdValToIndex(int fdVal, int toIndex) {
78 return fdVal & (toIndex - 1);
79 }
80
81 /**
82 * Creates a Poller for POLLIN polling.
83 * @param subPoller true to create a sub-poller
84 */
85 abstract Poller readPoller(boolean subPoller) throws IOException;
86
87 /**
88 * Creates a Poller for POLLOUT polling.
89 * @param subPoller true to create a sub-poller
90 */
91 abstract Poller writePoller(boolean subPoller) throws IOException;
92
93 /**
94 * Returns true if read pollers support read ops in addition to POLLIN polling.
95 * @implSpec The default implementation returns false.
96 */
97 boolean supportReadOps() {
98 return false;
99 }
100
101 /**
102 * Returns true if write pollers support write ops in addition to POLLOUT polling.
103 * @implSpec The default implementation returns false.
104 */
105 boolean supportWriteOps() {
106 return false;
107 }
108 }