0N/A/*
2362N/A * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include "jni.h"
0N/A#include "jni_util.h"
0N/A#include "jvm.h"
0N/A#include "jlong.h"
0N/A#include "sun_nio_ch_PollArrayWrapper.h"
0N/A#include <poll.h>
0N/A#include <unistd.h>
0N/A#include <sys/time.h>
0N/A
0N/A#define RESTARTABLE(_cmd, _result) do { \
0N/A do { \
0N/A _result = _cmd; \
0N/A } while((_result == -1) && (errno == EINTR)); \
0N/A} while(0)
0N/A
0N/Astatic int
0N/Aipoll(struct pollfd fds[], unsigned int nfds, int timeout)
0N/A{
0N/A jlong start, now;
0N/A int remaining = timeout;
0N/A struct timeval t;
0N/A int diff;
0N/A
0N/A gettimeofday(&t, NULL);
0N/A start = t.tv_sec * 1000 + t.tv_usec / 1000;
0N/A
0N/A for (;;) {
0N/A int res = poll(fds, nfds, remaining);
0N/A if (res < 0 && errno == EINTR) {
0N/A if (remaining >= 0) {
0N/A gettimeofday(&t, NULL);
0N/A now = t.tv_sec * 1000 + t.tv_usec / 1000;
0N/A diff = now - start;
0N/A remaining -= diff;
0N/A if (diff < 0 || remaining <= 0) {
0N/A return 0;
0N/A }
0N/A start = now;
0N/A }
0N/A } else {
0N/A return res;
0N/A }
0N/A }
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_nio_ch_PollArrayWrapper_poll0(JNIEnv *env, jobject this,
0N/A jlong address, jint numfds,
0N/A jlong timeout)
0N/A{
0N/A struct pollfd *a;
0N/A int err = 0;
0N/A
0N/A a = (struct pollfd *) jlong_to_ptr(address);
0N/A
0N/A if (timeout <= 0) { /* Indefinite or no wait */
0N/A RESTARTABLE (poll(a, numfds, timeout), err);
0N/A } else { /* Bounded wait; bounded restarts */
0N/A err = ipoll(a, numfds, timeout);
0N/A }
0N/A
0N/A if (err < 0) {
0N/A JNU_ThrowIOExceptionWithLastError(env, "Poll failed");
0N/A }
0N/A return (jint)err;
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_nio_ch_PollArrayWrapper_interrupt(JNIEnv *env, jobject this, jint fd)
0N/A{
0N/A int fakebuf[1];
0N/A fakebuf[0] = 1;
0N/A if (write(fd, fakebuf, 1) < 0) {
0N/A JNU_ThrowIOExceptionWithLastError(env,
0N/A "Write to interrupt fd failed");
0N/A }
0N/A}