Home | History | Annotate | Download | only in native
      1 /*
      2  * Copyright (c) 2008, 2011, 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 
     26 #include "jni.h"
     27 #include "jni_util.h"
     28 #include "jvm.h"
     29 #include "jlong.h"
     30 #include "nio_util.h"
     31 
     32 #include "sun_nio_ch_EPoll.h"
     33 
     34 #include <dlfcn.h>
     35 #include <unistd.h>
     36 #include <sys/types.h>
     37 #include <sys/epoll.h>
     38 
     39 JNIEXPORT jint JNICALL
     40 Java_sun_nio_ch_EPoll_eventSize(JNIEnv* env, jclass this)
     41 {
     42     return sizeof(struct epoll_event);
     43 }
     44 
     45 JNIEXPORT jint JNICALL
     46 Java_sun_nio_ch_EPoll_eventsOffset(JNIEnv* env, jclass this)
     47 {
     48     return offsetof(struct epoll_event, events);
     49 }
     50 
     51 JNIEXPORT jint JNICALL
     52 Java_sun_nio_ch_EPoll_dataOffset(JNIEnv* env, jclass this)
     53 {
     54     return offsetof(struct epoll_event, data);
     55 }
     56 
     57 JNIEXPORT jint JNICALL
     58 Java_sun_nio_ch_EPoll_epollCreate(JNIEnv *env, jclass c) {
     59     /*
     60      * epoll_create expects a size as a hint to the kernel about how to
     61      * dimension internal structures. We can't predict the size in advance.
     62      */
     63     int epfd = epoll_create(256);
     64     if (epfd < 0) {
     65        JNU_ThrowIOExceptionWithLastError(env, "epoll_create failed");
     66     }
     67     return epfd;
     68 }
     69 
     70 JNIEXPORT jint JNICALL
     71 Java_sun_nio_ch_EPoll_epollCtl(JNIEnv *env, jclass c, jint epfd,
     72                                    jint opcode, jint fd, jint events)
     73 {
     74     struct epoll_event event;
     75     int res;
     76 
     77     event.events = events;
     78     event.data.fd = fd;
     79 
     80     RESTARTABLE(epoll_ctl(epfd, (int)opcode, (int)fd, &event), res);
     81 
     82     return (res == 0) ? 0 : errno;
     83 }
     84 
     85 JNIEXPORT jint JNICALL
     86 Java_sun_nio_ch_EPoll_epollWait(JNIEnv *env, jclass c,
     87                                     jint epfd, jlong address, jint numfds)
     88 {
     89     struct epoll_event *events = jlong_to_ptr(address);
     90     int res;
     91 
     92     RESTARTABLE(epoll_wait(epfd, events, numfds, -1), res);
     93     if (res < 0) {
     94         JNU_ThrowIOExceptionWithLastError(env, "epoll_wait failed");
     95     }
     96     return res;
     97 }
     98 
     99 JNIEXPORT void JNICALL
    100 Java_sun_nio_ch_EPoll_close0(JNIEnv *env, jclass c, jint epfd) {
    101     int res;
    102     RESTARTABLE(close(epfd), res);
    103 }
    104