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 import java.lang.ref.Cleaner.Cleanable;
29 import jdk.internal.ref.CleanerFactory;
30 import static sun.nio.ch.EPoll.*;
31
32 /**
33 * Poller implementation based on the epoll facility.
34 */
35
36 class EPollPoller extends Poller {
37 private static final int ENOENT = 2;
38
39 private final int epfd;
40 private final int event;
41 private final int maxEvents;
42 private final long address;
43 private final EventFD eventfd; // wakeup event, used for shutdown
44
45 // close action, and cleaner if this is subpoller
46 private final Runnable closer;
47 private final Cleanable cleaner;
48
49 EPollPoller(Poller.Mode mode, boolean subPoller, boolean read) throws IOException {
50 boolean wakeable = (mode == Mode.POLLER_PER_CARRIER) && subPoller;
51 int maxEvents = (subPoller) ? 16 : 64;
52
53 int epfd = EPoll.create();
54 long address = 0L;
55 EventFD eventfd = null;
56 try {
57 address = EPoll.allocatePollArray(maxEvents);
58
59 // register one end of the pipe with epoll to allow for wakeup
60 if (wakeable) {
61 eventfd = new EventFD();
62 IOUtil.configureBlocking(eventfd.efd(), false);
63 EPoll.ctl(epfd, EPOLL_CTL_ADD, eventfd.efd(), EPOLLIN);
64 }
65 } catch (Throwable e) {
66 FileDispatcherImpl.closeIntFD(epfd);
67 if (address != 0L) EPoll.freePollArray(address);
68 if (eventfd != null) eventfd.close();
69 throw e;
70 }
71
72 this.epfd = epfd;
73 this.event = (read) ? EPOLLIN : EPOLLOUT;
74 this.maxEvents = maxEvents;
75 this.address = address;
76 this.eventfd = eventfd;
77
78 // create action to close epoll instance, register cleaner when wakeable
79 this.closer = closer(epfd, address, eventfd);
80 if (wakeable) {
81 this.cleaner = CleanerFactory.cleaner().register(this, closer);
82 } else {
83 this.cleaner = null;
84 }
85 }
86
87 /**
88 * Returns an action to close the epoll instance and release other resources.
89 */
90 private static Runnable closer(int epfd, long address, EventFD eventfd) {
91 return () -> {
92 try {
93 FileDispatcherImpl.closeIntFD(epfd);
94 EPoll.freePollArray(address);
95 if (eventfd != null) eventfd.close();
96 } catch (IOException _) { }
97 };
98 }
99
100 @Override
101 void close() {
102 if (cleaner != null) {
103 cleaner.clean();
104 } else {
105 closer.run();
106 }
107 }
108
109 @Override
110 int fdVal() {
111 return epfd;
112 }
113
114 @Override
115 void implStartPoll(int fdVal) throws IOException {
116 // re-enable if already registered but disabled (previously polled)
117 int err = EPoll.ctl(epfd, EPOLL_CTL_MOD, fdVal, (event | EPOLLONESHOT));
118 if (err == ENOENT)
119 err = EPoll.ctl(epfd, EPOLL_CTL_ADD, fdVal, (event | EPOLLONESHOT));
120 if (err != 0)
121 throw new IOException("epoll_ctl failed: " + err);
122 }
123
124 @Override
125 void implStopPoll(int fdVal, boolean polled) {
126 // event is disabled if already polled
127 if (!polled) {
128 EPoll.ctl(epfd, EPOLL_CTL_DEL, fdVal, 0);
129 }
130 }
131
132 @Override
133 void wakeupPoller() throws IOException {
134 if (eventfd == null) {
135 throw new UnsupportedOperationException();
136 }
137 eventfd.set();
138 }
139
140 @Override
141 int poll(int timeout) throws IOException {
142 int n = EPoll.wait(epfd, address, maxEvents, timeout);
143 int polled = 0;
144 int i = 0;
145 while (i < n) {
146 long eventAddress = EPoll.getEvent(address, i);
147 int fd = EPoll.getDescriptor(eventAddress);
148 if (eventfd == null || fd != eventfd.efd()) {
149 polled(fd);
150 polled++;
151 }
152 i++;
153 }
154 return polled;
155 }
156 }
157