< prev index next >

src/java.base/linux/classes/sun/nio/ch/EPollPort.java

Print this page


   1 /*
   2  * Copyright (c) 2008, 2013, 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


 165                 wakeup();
 166             }
 167         }
 168     }
 169 
 170     // invoke by clients to register a file descriptor
 171     @Override
 172     void startPoll(int fd, int events) {
 173         // update events (or add to epoll on first usage)
 174         int err = epollCtl(epfd, EPOLL_CTL_MOD, fd, (events | EPOLLONESHOT));
 175         if (err == ENOENT)
 176             err = epollCtl(epfd, EPOLL_CTL_ADD, fd, (events | EPOLLONESHOT));
 177         if (err != 0)
 178             throw new AssertionError();     // should not happen
 179     }
 180 
 181     /*
 182      * Task to process events from epoll and dispatch to the channel's
 183      * onEvent handler.
 184      *
 185      * Events are retreived from epoll in batch and offered to a BlockingQueue
 186      * where they are consumed by handler threads. A special "NEED_TO_POLL"
 187      * event is used to signal one consumer to re-poll when all events have
 188      * been consumed.
 189      */
 190     private class EventHandlerTask implements Runnable {
 191         private Event poll() throws IOException {
 192             try {
 193                 for (;;) {
 194                     int n = epollWait(epfd, address, MAX_EPOLL_EVENTS);
 195                     /*
 196                      * 'n' events have been read. Here we map them to their
 197                      * corresponding channel in batch and queue n-1 so that
 198                      * they can be handled by other handler threads. The last
 199                      * event is handled by this thread (and so is not queued).
 200                      */
 201                     fdToChannelLock.readLock().lock();
 202                     try {
 203                         while (n-- > 0) {
 204                             long eventAddress = getEvent(address, n);
 205                             int fd = getDescriptor(eventAddress);


   1 /*
   2  * Copyright (c) 2008, 2018, 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


 165                 wakeup();
 166             }
 167         }
 168     }
 169 
 170     // invoke by clients to register a file descriptor
 171     @Override
 172     void startPoll(int fd, int events) {
 173         // update events (or add to epoll on first usage)
 174         int err = epollCtl(epfd, EPOLL_CTL_MOD, fd, (events | EPOLLONESHOT));
 175         if (err == ENOENT)
 176             err = epollCtl(epfd, EPOLL_CTL_ADD, fd, (events | EPOLLONESHOT));
 177         if (err != 0)
 178             throw new AssertionError();     // should not happen
 179     }
 180 
 181     /*
 182      * Task to process events from epoll and dispatch to the channel's
 183      * onEvent handler.
 184      *
 185      * Events are retrieved from epoll in batch and offered to a BlockingQueue
 186      * where they are consumed by handler threads. A special "NEED_TO_POLL"
 187      * event is used to signal one consumer to re-poll when all events have
 188      * been consumed.
 189      */
 190     private class EventHandlerTask implements Runnable {
 191         private Event poll() throws IOException {
 192             try {
 193                 for (;;) {
 194                     int n = epollWait(epfd, address, MAX_EPOLL_EVENTS);
 195                     /*
 196                      * 'n' events have been read. Here we map them to their
 197                      * corresponding channel in batch and queue n-1 so that
 198                      * they can be handled by other handler threads. The last
 199                      * event is handled by this thread (and so is not queued).
 200                      */
 201                     fdToChannelLock.readLock().lock();
 202                     try {
 203                         while (n-- > 0) {
 204                             long eventAddress = getEvent(address, n);
 205                             int fd = getDescriptor(eventAddress);


< prev index next >