Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/nio/ch/PollArrayWrapper.c
32288 views
/*1* Copyright (c) 2001, 2005, 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 "jni.h"26#include "jni_util.h"27#include "jvm.h"28#include "jlong.h"29#include "sun_nio_ch_PollArrayWrapper.h"30#include <poll.h>31#include <unistd.h>32#include <sys/time.h>3334#define RESTARTABLE(_cmd, _result) do { \35do { \36_result = _cmd; \37} while((_result == -1) && (errno == EINTR)); \38} while(0)3940static int41ipoll(struct pollfd fds[], unsigned int nfds, int timeout)42{43jlong start, now;44int remaining = timeout;45struct timeval t;46int diff;4748gettimeofday(&t, NULL);49start = t.tv_sec * 1000 + t.tv_usec / 1000;5051for (;;) {52int res = poll(fds, nfds, remaining);53if (res < 0 && errno == EINTR) {54if (remaining >= 0) {55gettimeofday(&t, NULL);56now = t.tv_sec * 1000 + t.tv_usec / 1000;57diff = now - start;58remaining -= diff;59if (diff < 0 || remaining <= 0) {60return 0;61}62start = now;63}64} else {65return res;66}67}68}6970JNIEXPORT jint JNICALL71Java_sun_nio_ch_PollArrayWrapper_poll0(JNIEnv *env, jobject this,72jlong address, jint numfds,73jlong timeout)74{75struct pollfd *a;76int err = 0;7778a = (struct pollfd *) jlong_to_ptr(address);7980if (timeout <= 0) { /* Indefinite or no wait */81RESTARTABLE (poll(a, numfds, timeout), err);82} else { /* Bounded wait; bounded restarts */83err = ipoll(a, numfds, timeout);84}8586if (err < 0) {87JNU_ThrowIOExceptionWithLastError(env, "Poll failed");88}89return (jint)err;90}9192JNIEXPORT void JNICALL93Java_sun_nio_ch_PollArrayWrapper_interrupt(JNIEnv *env, jobject this, jint fd)94{95int fakebuf[1];96fakebuf[0] = 1;97if (write(fd, fakebuf, 1) < 0) {98JNU_ThrowIOExceptionWithLastError(env,99"Write to interrupt fd failed");100}101}102103104