< prev index next >

src/java.base/macosx/classes/sun/nio/ch/KQueuePoller.java

Print this page

  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 static sun.nio.ch.KQueue.*;
 29 
 30 /**
 31  * Poller implementation based on the kqueue facility.
 32  */
 33 class KQueuePoller extends Poller {
 34     private final int kqfd;
 35     private final int filter;
 36     private final int maxEvents;
 37     private final long address;
 38 
 39     KQueuePoller(boolean subPoller, boolean read) throws IOException {
 40         this.kqfd = KQueue.create();

































 41         this.filter = (read) ? EVFILT_READ : EVFILT_WRITE;
 42         this.maxEvents = (subPoller) ? 64 : 512;
 43         this.address = KQueue.allocatePollArray(maxEvents);

































 44     }
 45 
 46     @Override
 47     int fdVal() {
 48         return kqfd;
 49     }
 50 
 51     @Override
 52     void implRegister(int fdVal) throws IOException {
 53         int err = KQueue.register(kqfd, fdVal, filter, (EV_ADD|EV_ONESHOT));
 54         if (err != 0)
 55             throw new IOException("kevent failed: " + err);
 56     }
 57 
 58     @Override
 59     void implDeregister(int fdVal, boolean polled) {
 60         // event was deleted if already polled
 61         if (!polled) {
 62             KQueue.register(kqfd, fdVal, filter, EV_DELETE);
 63         }
 64     }
 65 








 66     @Override
 67     int poll(int timeout) throws IOException {
 68         int n = KQueue.poll(kqfd, address, maxEvents, timeout);

 69         int i = 0;
 70         while (i < n) {
 71             long keventAddress = KQueue.getEvent(address, i);
 72             int fdVal = KQueue.getDescriptor(keventAddress);
 73             polled(fdVal);



 74             i++;
 75         }
 76         return n;
 77     }
 78 }

  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.KQueue.*;
 31 
 32 /**
 33  * Poller implementation based on the kqueue facility.
 34  */
 35 class KQueuePoller extends Poller {
 36     private final int kqfd;
 37     private final int filter;
 38     private final int maxEvents;
 39     private final long address;
 40 
 41     // file descriptors used for wakeup during shutdown
 42     private final int fd0;
 43     private final int fd1;
 44 
 45     // close action, and cleaner if this is subpoller
 46     private final Runnable closer;
 47     private final Cleanable cleaner;
 48 
 49     KQueuePoller(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 kqfd = KQueue.create();
 54         long address = 0L;
 55         int fd0 = -1;
 56         int fd1 = -1;
 57         try {
 58             address = KQueue.allocatePollArray(maxEvents);
 59 
 60             // register one end of the pipe with kqueue to allow for wakeup
 61             if (wakeable) {
 62                 long fds = IOUtil.makePipe(false);
 63                 fd0 = (int) (fds >>> 32);
 64                 fd1 = (int) fds;
 65                 KQueue.register(kqfd, fd0, EVFILT_READ, EV_ADD);
 66             }
 67         } catch (Throwable e) {
 68             FileDispatcherImpl.closeIntFD(kqfd);
 69             if (address != 0L) KQueue.freePollArray(address);
 70             if (fd0 >= 0) FileDispatcherImpl.closeIntFD(fd0);
 71             if (fd1 >= 0) FileDispatcherImpl.closeIntFD(fd1);
 72             throw e;
 73         }
 74 
 75         this.kqfd = kqfd;
 76         this.filter = (read) ? EVFILT_READ : EVFILT_WRITE;
 77         this.maxEvents = maxEvents;
 78         this.address = address;
 79         this.fd0 = fd0;
 80         this.fd1 = fd1;
 81 
 82         // create action to close kqueue, register cleaner when wakeable
 83         this.closer = closer(kqfd, address, fd0, fd1);
 84         if (wakeable) {
 85             this.cleaner = CleanerFactory.cleaner().register(this, closer);
 86         } else {
 87             this.cleaner = null;
 88         }
 89     }
 90 
 91     /**
 92      * Returns an action to close the kqueue and release other resources.
 93      */
 94     private static Runnable closer(int kqfd, long address, int fd0, int fd1) {
 95         return () -> {
 96             try {
 97                 FileDispatcherImpl.closeIntFD(kqfd);
 98                 KQueue.freePollArray(address);
 99                 if (fd0 >= 0) FileDispatcherImpl.closeIntFD(fd0);
100                 if (fd1 >= 0) FileDispatcherImpl.closeIntFD(fd1);
101             } catch (IOException _) { }
102         };
103     }
104 
105     @Override
106     void close() {
107         if (cleaner != null) {
108             cleaner.clean();
109         } else {
110             closer.run();
111         }
112     }
113 
114     @Override
115     int fdVal() {
116         return kqfd;
117     }
118 
119     @Override
120     void implStartPoll(int fdVal) throws IOException {
121         int err = KQueue.register(kqfd, fdVal, filter, (EV_ADD|EV_ONESHOT));
122         if (err != 0)
123             throw new IOException("kevent failed: " + err);
124     }
125 
126     @Override
127     void implStopPoll(int fdVal, boolean polled) {
128         // event was deleted if already polled
129         if (!polled) {
130             KQueue.register(kqfd, fdVal, filter, EV_DELETE);
131         }
132     }
133 
134     @Override
135     void wakeupPoller() throws IOException {
136         if (fd1 < 0) {
137             throw new UnsupportedOperationException();
138         }
139         IOUtil.write1(fd1, (byte)0);
140     }
141 
142     @Override
143     int poll(int timeout) throws IOException {
144         int n = KQueue.poll(kqfd, address, maxEvents, timeout);
145         int polled = 0;
146         int i = 0;
147         while (i < n) {
148             long keventAddress = KQueue.getEvent(address, i);
149             int fdVal = KQueue.getDescriptor(keventAddress);
150             if (fdVal != fd0) {
151                 polled(fdVal);
152                 polled++;
153             }
154             i++;
155         }
156         return polled;
157     }
158 }
< prev index next >