src/java.base/unix/native/libnio/ch/IOUtil.c
author igerasim
Fri, 25 May 2018 12:44:34 -0700
changeset 50275 69204b98dc3d
parent 49430 e376090dc07e
child 54154 1caf2daef7cf
permissions -rw-r--r--
8203369: Check for both EAGAIN and EWOULDBLOCK error codes Reviewed-by: alanb
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) 2000, 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: 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 <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <string.h>
12872
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 7668
diff changeset
    28
#include <sys/resource.h>
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 7668
diff changeset
    29
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "sun_nio_ch_IOUtil.h"
12872
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 7668
diff changeset
    35
#include "java_lang_Integer.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "nio.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "nio_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
static jfieldID fd_fdID;        /* for jint 'fd' in java.io.FileDescriptor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
{
22631
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 22604
diff changeset
    45
    CHECK_NULL(clazz = (*env)->FindClass(env, "java/io/FileDescriptor"));
ac85b05a53f4 8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents: 22604
diff changeset
    46
    CHECK_NULL(fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
Java_sun_nio_ch_IOUtil_randomBytes(JNIEnv *env, jclass clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                  jbyteArray randArray)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
Java_sun_nio_ch_IOUtil_fdVal(JNIEnv *env, jclass clazz, jobject fdo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    return (*env)->GetIntField(env, fdo, fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
Java_sun_nio_ch_IOUtil_setfdVal(JNIEnv *env, jclass clazz, jobject fdo, jint val)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    (*env)->SetIntField(env, fdo, fd_fdID, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
configureBlocking(int fd, jboolean blocking)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    int flags = fcntl(fd, F_GETFL);
6520
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
    73
    int newflags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
6520
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
    75
    return (flags == newflags) ? 0 : fcntl(fd, F_SETFL, newflags);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                         jobject fdo, jboolean blocking)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    if (configureBlocking(fdval(env, fdo), blocking) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
6520
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
    86
JNIEXPORT jlong JNICALL
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
    87
Java_sun_nio_ch_IOUtil_makePipe(JNIEnv *env, jobject this, jboolean blocking)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    int fd[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    if (pipe(fd) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        JNU_ThrowIOExceptionWithLastError(env, "Pipe failed");
6520
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
    93
        return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
6520
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
    95
    if (blocking == JNI_FALSE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if ((configureBlocking(fd[0], JNI_FALSE) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            || (configureBlocking(fd[1], JNI_FALSE) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");
6520
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
    99
            close(fd[0]);
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   100
            close(fd[1]);
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   101
            return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
6520
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   104
    return ((jlong) fd[0] << 32) | (jlong) fd[1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 47216
diff changeset
   107
JNIEXPORT jint JNICALL
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 47216
diff changeset
   108
Java_sun_nio_ch_IOUtil_write1(JNIEnv *env, jclass cl, jint fd, jbyte b)
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 47216
diff changeset
   109
{
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 47216
diff changeset
   110
    char c = (char)b;
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 47216
diff changeset
   111
    return convertReturnVal(env, write(fd, &c, 1), JNI_FALSE);
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 47216
diff changeset
   112
}
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 47216
diff changeset
   113
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
{
49290
07779973cbe2 8199791: (se) More Selector cleanup
alanb
parents: 47216
diff changeset
   117
    char buf[16];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    int tn = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        int n = read(fd, buf, sizeof(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        tn += n;
50275
69204b98dc3d 8203369: Check for both EAGAIN and EWOULDBLOCK error codes
igerasim
parents: 49430
diff changeset
   123
        if ((n < 0) && (errno != EAGAIN && errno != EWOULDBLOCK))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            JNU_ThrowIOExceptionWithLastError(env, "Drain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (n == (int)sizeof(buf))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return (tn > 0) ? JNI_TRUE : JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
12872
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 7668
diff changeset
   131
JNIEXPORT jint JNICALL
49430
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   132
Java_sun_nio_ch_IOUtil_drain1(JNIEnv *env, jclass cl, jint fd)
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   133
{
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   134
    int res;
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   135
    char buf[1];
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   136
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   137
    res = read(fd, buf, 1);
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   138
    if (res < 0) {
50275
69204b98dc3d 8203369: Check for both EAGAIN and EWOULDBLOCK error codes
igerasim
parents: 49430
diff changeset
   139
        if (errno == EAGAIN || errno == EWOULDBLOCK) {
49430
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   140
            res = 0;
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   141
        } else if (errno == EINTR) {
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   142
            return IOS_INTERRUPTED;
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   143
        } else {
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   144
            JNU_ThrowIOExceptionWithLastError(env, "read");
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   145
            return IOS_THROWN;
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   146
        }
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   147
    }
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   148
    return res;
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   149
}
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   150
e376090dc07e 8200256: java/nio/channels/AsynchronousChannelGroup/Basic.java fails intermittently
alanb
parents: 49290
diff changeset
   151
JNIEXPORT jint JNICALL
12872
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 7668
diff changeset
   152
Java_sun_nio_ch_IOUtil_fdLimit(JNIEnv *env, jclass this)
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 7668
diff changeset
   153
{
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 7668
diff changeset
   154
    struct rlimit rlp;
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 7668
diff changeset
   155
    if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) {
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 7668
diff changeset
   156
        JNU_ThrowIOExceptionWithLastError(env, "getrlimit failed");
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 7668
diff changeset
   157
        return -1;
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 7668
diff changeset
   158
    }
32232
8d58fc5a0349 8074821: Resolve disabled warnings for libnio
bpb
parents: 26207
diff changeset
   159
    if (rlp.rlim_max == RLIM_INFINITY ||
8d58fc5a0349 8074821: Resolve disabled warnings for libnio
bpb
parents: 26207
diff changeset
   160
        rlp.rlim_max > (rlim_t)java_lang_Integer_MAX_VALUE) {
12872
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 7668
diff changeset
   161
        return java_lang_Integer_MAX_VALUE;
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 7668
diff changeset
   162
    } else {
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 7668
diff changeset
   163
        return (jint)rlp.rlim_max;
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 7668
diff changeset
   164
    }
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 7668
diff changeset
   165
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
13024
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   167
JNIEXPORT jint JNICALL
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   168
Java_sun_nio_ch_IOUtil_iovMax(JNIEnv *env, jclass this)
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   169
{
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   170
    jlong iov_max = sysconf(_SC_IOV_MAX);
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   171
    if (iov_max == -1)
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   172
        iov_max = 16;
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   173
    return (jint)iov_max;
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   174
}
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 12872
diff changeset
   175
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
/* Declared in nio_util.h for use elsewhere in NIO */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
convertReturnVal(JNIEnv *env, jint n, jboolean reading)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    if (n > 0) /* Number of bytes written */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return n;
6520
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   183
    else if (n == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (reading) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            return IOS_EOF; /* EOF is -1 in javaland */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
50275
69204b98dc3d 8203369: Check for both EAGAIN and EWOULDBLOCK error codes
igerasim
parents: 49430
diff changeset
   190
    else if (errno == EAGAIN || errno == EWOULDBLOCK)
6520
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   191
        return IOS_UNAVAILABLE;
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   192
    else if (errno == EINTR)
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   193
        return IOS_INTERRUPTED;
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   194
    else {
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   195
        const char *msg = reading ? "Read failed" : "Write failed";
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   196
        JNU_ThrowIOExceptionWithLastError(env, msg);
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   197
        return IOS_THROWN;
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   198
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
/* Declared in nio_util.h for use elsewhere in NIO */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    if (n > 0) /* Number of bytes written */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return n;
6520
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   208
    else if (n == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (reading) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            return IOS_EOF; /* EOF is -1 in javaland */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
50275
69204b98dc3d 8203369: Check for both EAGAIN and EWOULDBLOCK error codes
igerasim
parents: 49430
diff changeset
   215
    else if (errno == EAGAIN || errno == EWOULDBLOCK)
6520
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   216
        return IOS_UNAVAILABLE;
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   217
    else if (errno == EINTR)
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   218
        return IOS_INTERRUPTED;
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   219
    else {
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   220
        const char *msg = reading ? "Read failed" : "Write failed";
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   221
        JNU_ThrowIOExceptionWithLastError(env, msg);
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   222
        return IOS_THROWN;
0e7cf575332e 6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents: 5506
diff changeset
   223
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
fdval(JNIEnv *env, jobject fdo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    return (*env)->GetIntField(env, fdo, fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
}