Path: blob/master/src/java.base/macosx/native/libnio/ch/KQueue.c
41133 views
/*1* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <strings.h>26#include <sys/types.h>27#include <sys/event.h>28#include <sys/time.h>2930#include "jni.h"31#include "jni_util.h"32#include "jlong.h"33#include "nio.h"34#include "nio_util.h"3536#include "sun_nio_ch_KQueue.h"3738JNIEXPORT jint JNICALL39Java_sun_nio_ch_KQueue_keventSize(JNIEnv* env, jclass clazz)40{41return sizeof(struct kevent);42}4344JNIEXPORT jint JNICALL45Java_sun_nio_ch_KQueue_identOffset(JNIEnv* env, jclass clazz)46{47return offsetof(struct kevent, ident);48}4950JNIEXPORT jint JNICALL51Java_sun_nio_ch_KQueue_filterOffset(JNIEnv* env, jclass clazz)52{53return offsetof(struct kevent, filter);54}5556JNIEXPORT jint JNICALL57Java_sun_nio_ch_KQueue_flagsOffset(JNIEnv* env, jclass clazz)58{59return offsetof(struct kevent, flags);60}6162JNIEXPORT jint JNICALL63Java_sun_nio_ch_KQueue_create(JNIEnv *env, jclass clazz) {64int kqfd = kqueue();65if (kqfd < 0) {66JNU_ThrowIOExceptionWithLastError(env, "kqueue failed");67return IOS_THROWN;68}69return kqfd;70}7172JNIEXPORT jint JNICALL73Java_sun_nio_ch_KQueue_register(JNIEnv *env, jclass clazz, jint kqfd,74jint fd, jint filter, jint flags)7576{77struct kevent changes[1];78int res;7980EV_SET(&changes[0], fd, filter, flags, 0, 0, 0);81RESTARTABLE(kevent(kqfd, &changes[0], 1, NULL, 0, NULL), res);82return (res == -1) ? errno : 0;83}8485JNIEXPORT jint JNICALL86Java_sun_nio_ch_KQueue_poll(JNIEnv *env, jclass clazz, jint kqfd, jlong address,87jint nevents, jlong timeout)88{89struct kevent *events = jlong_to_ptr(address);90int res;91struct timespec ts;92struct timespec *tsp;9394if (timeout >= 0) {95ts.tv_sec = timeout / 1000;96ts.tv_nsec = (timeout % 1000) * 1000000;97tsp = &ts;98} else {99tsp = NULL;100}101102res = kevent(kqfd, NULL, 0, events, nevents, tsp);103if (res < 0) {104if (errno == EINTR) {105return IOS_INTERRUPTED;106} else {107JNU_ThrowIOExceptionWithLastError(env, "kqueue failed");108return IOS_THROWN;109}110}111return res;112}113114115