src/java.base/unix/native/libnio/ch/PollArrayWrapper.c
author alanb
Fri, 23 Mar 2018 14:18:18 +0000
changeset 49290 07779973cbe2
parent 47216 71c04702a3d5
permissions -rw-r--r--
8199791: (se) More Selector cleanup Reviewed-by: redestad, bpb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "sun_nio_ch_PollArrayWrapper.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <poll.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <sys/time.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#define RESTARTABLE(_cmd, _result) do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
  do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    _result = _cmd; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  } while((_result == -1) && (errno == EINTR)); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
} while(0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
ipoll(struct pollfd fds[], unsigned int nfds, int timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    jlong start, now;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    int remaining = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    struct timeval t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    int diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    gettimeofday(&t, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    start = t.tv_sec * 1000 + t.tv_usec / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        int res = poll(fds, nfds, remaining);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (res < 0 && errno == EINTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            if (remaining >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                gettimeofday(&t, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                now = t.tv_sec * 1000 + t.tv_usec / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                diff = now - start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                remaining -= diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                if (diff < 0 || remaining <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                start = now;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
Java_sun_nio_ch_PollArrayWrapper_poll0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                       jlong address, jint numfds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                       jlong timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    struct pollfd *a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    int err = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    a = (struct pollfd *) jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    if (timeout <= 0) {           /* Indefinite or no wait */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        RESTARTABLE (poll(a, numfds, timeout), err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    } else {                     /* Bounded wait; bounded restarts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        err = ipoll(a, numfds, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        JNU_ThrowIOExceptionWithLastError(env, "Poll failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    return (jint)err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
Java_sun_nio_ch_PollArrayWrapper_interrupt(JNIEnv *env, jobject this, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    int fakebuf[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    fakebuf[0] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    if (write(fd, fakebuf, 1) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         JNU_ThrowIOExceptionWithLastError(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                          "Write to interrupt fd failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
}