src/java.base/macosx/native/libnio/ch/KQueue.c
changeset 49290 07779973cbe2
parent 47216 71c04702a3d5
equal deleted inserted replaced
49289:148e29df1644 49290:07779973cbe2
     1 /*
     1 /*
     2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    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
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    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_KQueue.h"
       
    33 
       
    34 #include <strings.h>
    26 #include <strings.h>
    35 #include <sys/types.h>
    27 #include <sys/types.h>
    36 #include <sys/event.h>
    28 #include <sys/event.h>
    37 #include <sys/time.h>
    29 #include <sys/time.h>
    38 
    30 
       
    31 #include "jni.h"
       
    32 #include "jni_util.h"
       
    33 #include "jlong.h"
       
    34 #include "nio.h"
       
    35 #include "nio_util.h"
       
    36 
       
    37 #include "sun_nio_ch_KQueue.h"
       
    38 
    39 JNIEXPORT jint JNICALL
    39 JNIEXPORT jint JNICALL
    40 Java_sun_nio_ch_KQueue_keventSize(JNIEnv* env, jclass this)
    40 Java_sun_nio_ch_KQueue_keventSize(JNIEnv* env, jclass clazz)
    41 {
    41 {
    42     return sizeof(struct kevent);
    42     return sizeof(struct kevent);
    43 }
    43 }
    44 
    44 
    45 JNIEXPORT jint JNICALL
    45 JNIEXPORT jint JNICALL
    46 Java_sun_nio_ch_KQueue_identOffset(JNIEnv* env, jclass this)
    46 Java_sun_nio_ch_KQueue_identOffset(JNIEnv* env, jclass clazz)
    47 {
    47 {
    48     return offsetof(struct kevent, ident);
    48     return offsetof(struct kevent, ident);
    49 }
    49 }
    50 
    50 
    51 JNIEXPORT jint JNICALL
    51 JNIEXPORT jint JNICALL
    52 Java_sun_nio_ch_KQueue_filterOffset(JNIEnv* env, jclass this)
    52 Java_sun_nio_ch_KQueue_filterOffset(JNIEnv* env, jclass clazz)
    53 {
    53 {
    54     return offsetof(struct kevent, filter);
    54     return offsetof(struct kevent, filter);
    55 }
    55 }
    56 
    56 
    57 JNIEXPORT jint JNICALL
    57 JNIEXPORT jint JNICALL
    58 Java_sun_nio_ch_KQueue_flagsOffset(JNIEnv* env, jclass this)
    58 Java_sun_nio_ch_KQueue_flagsOffset(JNIEnv* env, jclass clazz)
    59 {
    59 {
    60     return offsetof(struct kevent, flags);
    60     return offsetof(struct kevent, flags);
    61 }
    61 }
    62 
    62 
    63 JNIEXPORT jint JNICALL
    63 JNIEXPORT jint JNICALL
    64 Java_sun_nio_ch_KQueue_kqueue(JNIEnv *env, jclass c) {
    64 Java_sun_nio_ch_KQueue_create(JNIEnv *env, jclass clazz) {
    65     int kqfd = kqueue();
    65     int kqfd = kqueue();
    66     if (kqfd < 0) {
    66     if (kqfd < 0) {
    67         JNU_ThrowIOExceptionWithLastError(env, "kqueue failed");
    67         JNU_ThrowIOExceptionWithLastError(env, "kqueue failed");
       
    68         return IOS_THROWN;
    68     }
    69     }
    69     return kqfd;
    70     return kqfd;
    70 }
    71 }
    71 
    72 
    72 JNIEXPORT jint JNICALL
    73 JNIEXPORT jint JNICALL
    73 Java_sun_nio_ch_KQueue_keventRegister(JNIEnv *env, jclass c, jint kqfd,
    74 Java_sun_nio_ch_KQueue_register(JNIEnv *env, jclass clazz, jint kqfd,
    74                                       jint fd, jint filter, jint flags)
    75                                 jint fd, jint filter, jint flags)
    75 
    76 
    76 {
    77 {
    77     struct kevent changes[1];
    78     struct kevent changes[1];
    78     struct timespec timeout = {0, 0};
       
    79     int res;
    79     int res;
    80 
    80 
    81     EV_SET(&changes[0], fd, filter, flags, 0, 0, 0);
    81     EV_SET(&changes[0], fd, filter, flags, 0, 0, 0);
    82     RESTARTABLE(kevent(kqfd, &changes[0], 1, NULL, 0, &timeout), res);
    82     RESTARTABLE(kevent(kqfd, &changes[0], 1, NULL, 0, NULL), res);
    83     return (res == -1) ? errno : 0;
    83     return (res == -1) ? errno : 0;
    84 }
    84 }
    85 
    85 
    86 JNIEXPORT jint JNICALL
    86 JNIEXPORT jint JNICALL
    87 Java_sun_nio_ch_KQueue_keventPoll(JNIEnv *env, jclass c,
    87 Java_sun_nio_ch_KQueue_poll(JNIEnv *env, jclass clazz, jint kqfd, jlong address,
    88                                   jint kqfd, jlong address, jint nevents)
    88                             jint nevents, jlong timeout)
    89 {
    89 {
    90     struct kevent *events = jlong_to_ptr(address);
    90     struct kevent *events = jlong_to_ptr(address);
    91     int res;
    91     int res;
       
    92     struct timespec ts;
       
    93     struct timespec *tsp;
    92 
    94 
    93     RESTARTABLE(kevent(kqfd, NULL, 0, events, nevents, NULL), res);
    95     if (timeout >= 0) {
       
    96         ts.tv_sec = timeout / 1000;
       
    97         ts.tv_nsec = (timeout % 1000) * 1000000;
       
    98         tsp = &ts;
       
    99     } else {
       
   100         tsp = NULL;
       
   101     }
       
   102 
       
   103     res = kevent(kqfd, NULL, 0, events, nevents, tsp);
    94     if (res < 0) {
   104     if (res < 0) {
    95         JNU_ThrowIOExceptionWithLastError(env, "kqueue failed");
   105         if (errno == EINTR) {
       
   106             return IOS_INTERRUPTED;
       
   107         } else {
       
   108             JNU_ThrowIOExceptionWithLastError(env, "kqueue failed");
       
   109             return IOS_THROWN;
       
   110         }
    96     }
   111     }
    97     return res;
   112     return res;
    98 }
   113 }