jdk/src/solaris/classes/sun/nio/ch/EPoll.java
changeset 2057 3acf8e5e2ca0
child 5506 202f599c92aa
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package sun.nio.ch;
       
    27 
       
    28 import java.io.IOException;
       
    29 import sun.misc.Unsafe;
       
    30 
       
    31 /**
       
    32  * Provides access to the Linux epoll facility.
       
    33  */
       
    34 
       
    35 class EPoll {
       
    36     private EPoll() { }
       
    37 
       
    38     private static final Unsafe unsafe = Unsafe.getUnsafe();
       
    39 
       
    40     /**
       
    41      * typedef union epoll_data {
       
    42      *     void *ptr;
       
    43      *     int fd;
       
    44      *     __uint32_t u32;
       
    45      *     __uint64_t u64;
       
    46      *  } epoll_data_t;
       
    47      *
       
    48      * struct epoll_event {
       
    49      *     __uint32_t events;
       
    50      *     epoll_data_t data;
       
    51      * }
       
    52      */
       
    53     private static final int SIZEOF_EPOLLEVENT   = eventSize();
       
    54     private static final int OFFSETOF_EVENTS     = eventsOffset();
       
    55     private static final int OFFSETOF_FD         = dataOffset();
       
    56 
       
    57     // opcodes
       
    58     static final int EPOLL_CTL_ADD  = 1;
       
    59     static final int EPOLL_CTL_DEL  = 2;
       
    60     static final int EPOLL_CTL_MOD  = 3;
       
    61 
       
    62     // flags
       
    63     static final int EPOLLONESHOT   = (1 << 30);
       
    64 
       
    65     /**
       
    66      * Allocates a poll array to handle up to {@code count} events.
       
    67      */
       
    68     static long allocatePollArray(int count) {
       
    69         return unsafe.allocateMemory(count * SIZEOF_EPOLLEVENT);
       
    70     }
       
    71 
       
    72     /**
       
    73      * Free a poll array
       
    74      */
       
    75     static void freePollArray(long address) {
       
    76         unsafe.freeMemory(address);
       
    77     }
       
    78 
       
    79     /**
       
    80      * Returns event[i];
       
    81      */
       
    82     static long getEvent(long address, int i) {
       
    83         return address + (SIZEOF_EPOLLEVENT*i);
       
    84     }
       
    85 
       
    86     /**
       
    87      * Returns event->data.fd
       
    88      */
       
    89     static int getDescriptor(long eventAddress) {
       
    90         return unsafe.getInt(eventAddress + OFFSETOF_FD);
       
    91     }
       
    92 
       
    93     /**
       
    94      * Returns event->events
       
    95      */
       
    96     static int getEvents(long eventAddress) {
       
    97         return unsafe.getInt(eventAddress + OFFSETOF_EVENTS);
       
    98     }
       
    99 
       
   100     // -- Native methods --
       
   101 
       
   102     private static native void init();
       
   103 
       
   104     private static native int eventSize();
       
   105 
       
   106     private static native int eventsOffset();
       
   107 
       
   108     private static native int dataOffset();
       
   109 
       
   110     static native int epollCreate() throws IOException;
       
   111 
       
   112     static native int epollCtl(int epfd, int opcode, int fd, int events);
       
   113 
       
   114     static native int epollWait(int epfd, long pollAddress, int numfds)
       
   115         throws IOException;
       
   116 
       
   117     static {
       
   118         Util.load();
       
   119         init();
       
   120     }
       
   121 }