src/java.base/solaris/native/libnio/ch/DevPollArrayWrapper.c
author alanb
Fri, 23 Mar 2018 14:18:18 +0000
changeset 49290 07779973cbe2
parent 47216 71c04702a3d5
child 49526 cad4c844902a
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
/*
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2018, 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: 2434
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: 2434
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: 2434
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2434
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2434
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_DevPollArrayWrapper.h"
46862
cb4b080576fa 8180003: Remove sys/ prefix from poll.h and signal.h includes
mikael
parents: 37789
diff changeset
    31
#include <poll.h>
2
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
#ifdef  __cplusplus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
extern "C" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
typedef uint32_t        caddr32_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/* /dev/poll ioctl */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#define         DPIOC   (0xD0 << 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#define DP_POLL         (DPIOC | 1)     /* poll on fds in cached in /dev/poll */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#define DP_ISPOLLED     (DPIOC | 2)     /* is this fd cached in /dev/poll */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#define DEVPOLLSIZE     1000            /* /dev/poll table size increment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#define POLLREMOVE      0x0800          /* Removes fd from monitored set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * /dev/poll DP_POLL ioctl format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
typedef struct dvpoll {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        pollfd_t        *dp_fds;        /* pollfd array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        nfds_t          dp_nfds;        /* num of pollfd's in dp_fds[] */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        int             dp_timeout;     /* time out in millisec */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
} dvpoll_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
typedef struct dvpoll32 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        caddr32_t       dp_fds;         /* pollfd array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        uint32_t        dp_nfds;        /* num of pollfd's in dp_fds[] */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        int32_t         dp_timeout;     /* time out in millisec */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
} dvpoll32_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
#ifdef  __cplusplus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
#define RESTARTABLE(_cmd, _result) do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
  do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    _result = _cmd; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
  } while((_result == -1) && (errno == EINTR)); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
} while(0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
idevpoll(jint wfd, int dpctl, struct dvpoll a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    jlong start, now;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    int remaining = a.dp_timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    struct timeval t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    int diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    gettimeofday(&t, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    start = t.tv_sec * 1000 + t.tv_usec / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        /*  poll(7d) ioctl does not return remaining count */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        int res = ioctl(wfd, dpctl, &a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (res < 0 && errno == EINTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if (remaining >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                gettimeofday(&t, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                now = t.tv_sec * 1000 + t.tv_usec / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                diff = now - start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                remaining -= diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                if (diff < 0 || remaining <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                start = now;
37789
d18410086785 8153192: (se) Selector.select(long) uses wrong timeout after EINTR (lnx)
bpb
parents: 25859
diff changeset
    97
                a.dp_timeout = remaining;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
Java_sun_nio_ch_DevPollArrayWrapper_init(JNIEnv *env, jobject this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    int wfd = open("/dev/poll", O_RDWR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    if (wfd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
       JNU_ThrowIOExceptionWithLastError(env, "Error opening driver");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
       return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    return wfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
Java_sun_nio_ch_DevPollArrayWrapper_register(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                             jint wfd, jint fd, jint mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
{
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   120
    struct pollfd a[1];
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   121
    int n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    a[0].fd = fd;
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   124
    a[0].events = mask;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    a[0].revents = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   127
    n = write(wfd, &a[0], sizeof(a));
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   128
    if (n != sizeof(a)) {
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   129
        if (n < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            JNU_ThrowIOExceptionWithLastError(env, "Error writing pollfds");
12844
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   131
        } else {
883981db17b3 7169050: (se) Selector.select slow on Solaris due to insertion of POLLREMOVE and 0 events
alanb
parents: 5506
diff changeset
   132
            JNU_ThrowIOException(env, "Unexpected number of bytes written");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
Java_sun_nio_ch_DevPollArrayWrapper_registerMultiple(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                                     jint wfd, jlong address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                                     jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    unsigned char *pollBytes = (unsigned char *)jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    unsigned char *pollEnd = pollBytes + sizeof(struct pollfd) * len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    while (pollBytes < pollEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int bytesWritten = write(wfd, pollBytes, (int)(pollEnd - pollBytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (bytesWritten < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            JNU_ThrowIOExceptionWithLastError(env, "Error writing pollfds");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        pollBytes += bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
Java_sun_nio_ch_DevPollArrayWrapper_poll0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                       jlong address, jint numfds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                       jlong timeout, jint wfd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    struct dvpoll a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    void *pfd = (void *) jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    a.dp_fds = pfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    a.dp_nfds = numfds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    a.dp_timeout = (int)timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    if (timeout <= 0) {             /* Indefinite or no wait */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        RESTARTABLE (ioctl(wfd, DP_POLL, &a), result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    } else {                        /* Bounded wait; bounded restarts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        result = idevpoll(wfd, DP_POLL, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    if (result < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        JNU_ThrowIOExceptionWithLastError(env, "Error reading driver");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
}