src/java.base/macosx/native/libnet/bsd_close.c
author mbaesken
Wed, 20 Nov 2019 14:16:29 +0100
changeset 59197 bb3472aa26c8
parent 53741 38b6110d5db2
permissions -rw-r--r--
8234501: remove obsolete NET_ReadV Reviewed-by: alanb, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     1
/*
53741
38b6110d5db2 8218882: NET_Writev is declared, NET_WriteV is defined
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     4
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    10
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    15
 * accompanied this code).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    16
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    20
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    23
 * questions.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    24
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    25
37609
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    26
#include <assert.h>
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    27
#include <limits.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    28
#include <stdio.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    29
#include <stdlib.h>
23043
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
    30
#include <sys/param.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    31
#include <signal.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    32
#include <pthread.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    33
#include <sys/types.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    34
#include <sys/socket.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    35
#include <sys/select.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    36
#include <sys/time.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    37
#include <sys/resource.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    38
#include <sys/uio.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    39
#include <unistd.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    40
#include <errno.h>
46862
cb4b080576fa 8180003: Remove sys/ prefix from poll.h and signal.h includes
mikael
parents: 44921
diff changeset
    41
#include <poll.h>
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
    42
#include "jvm.h"
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
    43
#include "net_util.h"
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    44
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    45
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    46
 * Stack allocated by thread when doing blocking operation
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    47
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    48
typedef struct threadEntry {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    49
    pthread_t thr;                      /* this thread */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    50
    struct threadEntry *next;           /* next thread */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    51
    int intr;                           /* interrupted */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    52
} threadEntry_t;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    53
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    54
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    55
 * Heap allocated during initialized - one entry per fd
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    56
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    57
typedef struct {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    58
    pthread_mutex_t lock;               /* fd lock */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    59
    threadEntry_t *threads;             /* threads blocked on fd */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    60
} fdEntry_t;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    61
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    62
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    63
 * Signal to unblock thread
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    64
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    65
static int sigWakeup = SIGIO;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    66
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    67
/*
37609
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    68
 * fdTable holds one entry per file descriptor, up to a certain
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    69
 * maximum.
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    70
 * Theoretically, the number of possible file descriptors can get
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    71
 * large, though usually it does not. Entries for small value file
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    72
 * descriptors are kept in a simple table, which covers most scenarios.
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    73
 * Entries for large value file descriptors are kept in an overflow
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    74
 * table, which is organized as a sparse two dimensional array whose
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    75
 * slabs are allocated on demand. This covers all corner cases while
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    76
 * keeping memory consumption reasonable.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    77
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    78
37609
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    79
/* Base table for low value file descriptors */
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    80
static fdEntry_t* fdTable = NULL;
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    81
/* Maximum size of base table (in number of entries). */
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    82
static const int fdTableMaxSize = 0x1000; /* 4K */
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    83
/* Actual size of base table (in number of entries) */
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    84
static int fdTableLen = 0;
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    85
/* Max. theoretical number of file descriptors on system. */
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    86
static int fdLimit = 0;
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    87
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    88
/* Overflow table, should base table not be large enough. Organized as
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    89
 *   an array of n slabs, each holding 64k entries.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    90
 */
37609
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    91
static fdEntry_t** fdOverflowTable = NULL;
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    92
/* Number of slabs in the overflow table */
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    93
static int fdOverflowTableLen = 0;
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    94
/* Number of entries in one slab */
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    95
static const int fdOverflowTableSlabSize = 0x10000; /* 64k */
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
    96
pthread_mutex_t fdOverflowTableLock = PTHREAD_MUTEX_INITIALIZER;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    97
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    98
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    99
 * Null signal handler
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   100
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   101
static void sig_wakeup(int sig) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   102
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   103
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   104
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   105
 * Initialization routine (executed when library is loaded)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   106
 * Allocate fd tables and sets up signal handler.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   107
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   108
static void __attribute((constructor)) init() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   109
    struct rlimit nbr_files;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   110
    sigset_t sigset;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   111
    struct sigaction sa;
37609
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   112
    int i = 0;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   113
37609
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   114
    /* Determine the maximum number of possible file descriptors. */
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   115
    if (-1 == getrlimit(RLIMIT_NOFILE, &nbr_files)) {
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   116
        fprintf(stderr, "library initialization failed - "
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   117
                "unable to get max # of allocated fds\n");
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   118
        abort();
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   119
    }
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   120
    if (nbr_files.rlim_max != RLIM_INFINITY) {
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   121
        fdLimit = nbr_files.rlim_max;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   122
    } else {
37609
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   123
        /* We just do not know. */
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   124
        fdLimit = INT_MAX;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   125
    }
37609
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   126
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   127
    /* Allocate table for low value file descriptors. */
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   128
    fdTableLen = fdLimit < fdTableMaxSize ? fdLimit : fdTableMaxSize;
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   129
    fdTable = (fdEntry_t*) calloc(fdTableLen, sizeof(fdEntry_t));
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   130
    if (fdTable == NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   131
        fprintf(stderr, "library initialization failed - "
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   132
                "unable to allocate file descriptor table - out of memory");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   133
        abort();
37609
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   134
    } else {
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   135
        for (i = 0; i < fdTableLen; i ++) {
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   136
            pthread_mutex_init(&fdTable[i].lock, NULL);
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   137
        }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   138
    }
37609
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   139
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   140
    /* Allocate overflow table, if needed */
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   141
    if (fdLimit > fdTableMaxSize) {
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   142
        fdOverflowTableLen = ((fdLimit - fdTableMaxSize) / fdOverflowTableSlabSize) + 1;
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   143
        fdOverflowTable = (fdEntry_t**) calloc(fdOverflowTableLen, sizeof(fdEntry_t*));
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   144
        if (fdOverflowTable == NULL) {
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   145
            fprintf(stderr, "library initialization failed - "
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   146
                    "unable to allocate file descriptor overflow table - out of memory");
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   147
            abort();
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   148
        }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   149
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   150
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   151
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   152
     * Setup the signal handler
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   153
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   154
    sa.sa_handler = sig_wakeup;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   155
    sa.sa_flags   = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   156
    sigemptyset(&sa.sa_mask);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   157
    sigaction(sigWakeup, &sa, NULL);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   158
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   159
    sigemptyset(&sigset);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   160
    sigaddset(&sigset, sigWakeup);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   161
    sigprocmask(SIG_UNBLOCK, &sigset, NULL);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   162
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   163
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   164
/*
37609
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   165
 * Return the fd table for this fd.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   166
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   167
static inline fdEntry_t *getFdEntry(int fd)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   168
{
37609
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   169
    fdEntry_t* result = NULL;
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   170
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   171
    if (fd < 0) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   172
        return NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   173
    }
37609
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   174
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   175
    /* This should not happen. If it does, our assumption about
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   176
     * max. fd value was wrong. */
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   177
    assert(fd < fdLimit);
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   178
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   179
    if (fd < fdTableMaxSize) {
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   180
        /* fd is in base table. */
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   181
        assert(fd < fdTableLen);
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   182
        result = &fdTable[fd];
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   183
    } else {
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   184
        /* fd is in overflow table. */
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   185
        const int indexInOverflowTable = fd - fdTableMaxSize;
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   186
        const int rootindex = indexInOverflowTable / fdOverflowTableSlabSize;
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   187
        const int slabindex = indexInOverflowTable % fdOverflowTableSlabSize;
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   188
        fdEntry_t* slab = NULL;
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   189
        assert(rootindex < fdOverflowTableLen);
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   190
        assert(slabindex < fdOverflowTableSlabSize);
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   191
        pthread_mutex_lock(&fdOverflowTableLock);
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   192
        /* Allocate new slab in overflow table if needed */
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   193
        if (fdOverflowTable[rootindex] == NULL) {
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   194
            fdEntry_t* const newSlab =
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   195
                (fdEntry_t*)calloc(fdOverflowTableSlabSize, sizeof(fdEntry_t));
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   196
            if (newSlab == NULL) {
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   197
                fprintf(stderr, "Unable to allocate file descriptor overflow"
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   198
                        " table slab - out of memory");
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   199
                pthread_mutex_unlock(&fdOverflowTableLock);
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   200
                abort();
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   201
            } else {
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   202
                int i;
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   203
                for (i = 0; i < fdOverflowTableSlabSize; i ++) {
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   204
                    pthread_mutex_init(&newSlab[i].lock, NULL);
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   205
                }
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   206
                fdOverflowTable[rootindex] = newSlab;
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   207
            }
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   208
        }
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   209
        pthread_mutex_unlock(&fdOverflowTableLock);
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   210
        slab = fdOverflowTable[rootindex];
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   211
        result = &slab[slabindex];
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   212
    }
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   213
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   214
    return result;
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   215
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   216
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   217
37609
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   218
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   219
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   220
 * Start a blocking operation :-
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   221
 *    Insert thread onto thread list for the fd.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   222
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   223
static inline void startOp(fdEntry_t *fdEntry, threadEntry_t *self)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   224
{
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   225
    self->thr = pthread_self();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   226
    self->intr = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   227
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   228
    pthread_mutex_lock(&(fdEntry->lock));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   229
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   230
        self->next = fdEntry->threads;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   231
        fdEntry->threads = self;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   232
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   233
    pthread_mutex_unlock(&(fdEntry->lock));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   234
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   235
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   236
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   237
 * End a blocking operation :-
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   238
 *     Remove thread from thread list for the fd
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   239
 *     If fd has been interrupted then set errno to EBADF
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   240
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   241
static inline void endOp
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   242
    (fdEntry_t *fdEntry, threadEntry_t *self)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   243
{
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   244
    int orig_errno = errno;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   245
    pthread_mutex_lock(&(fdEntry->lock));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   246
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   247
        threadEntry_t *curr, *prev=NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   248
        curr = fdEntry->threads;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   249
        while (curr != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   250
            if (curr == self) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   251
                if (curr->intr) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   252
                    orig_errno = EBADF;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   253
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   254
                if (prev == NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   255
                    fdEntry->threads = curr->next;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   256
                } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   257
                    prev->next = curr->next;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   258
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   259
                break;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   260
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   261
            prev = curr;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   262
            curr = curr->next;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   263
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   264
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   265
    pthread_mutex_unlock(&(fdEntry->lock));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   266
    errno = orig_errno;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   267
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   268
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   269
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   270
 * Close or dup2 a file descriptor ensuring that all threads blocked on
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   271
 * the file descriptor are notified via a wakeup signal.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   272
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   273
 *      fd1 < 0    => close(fd2)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   274
 *      fd1 >= 0   => dup2(fd1, fd2)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   275
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   276
 * Returns -1 with errno set if operation fails.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   277
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   278
static int closefd(int fd1, int fd2) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   279
    int rv, orig_errno;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   280
    fdEntry_t *fdEntry = getFdEntry(fd2);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   281
    if (fdEntry == NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   282
        errno = EBADF;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   283
        return -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   284
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   285
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   286
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   287
     * Lock the fd to hold-off additional I/O on this fd.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   288
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   289
    pthread_mutex_lock(&(fdEntry->lock));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   290
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   291
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   292
        /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   293
         * Send a wakeup signal to all threads blocked on this
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   294
         * file descriptor.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   295
         */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   296
        threadEntry_t *curr = fdEntry->threads;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   297
        while (curr != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   298
            curr->intr = 1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   299
            pthread_kill( curr->thr, sigWakeup );
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   300
            curr = curr->next;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   301
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   302
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   303
        /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   304
         * And close/dup the file descriptor
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   305
         * (restart if interrupted by signal)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   306
         */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   307
        do {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   308
            if (fd1 < 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   309
                rv = close(fd2);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   310
            } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   311
                rv = dup2(fd1, fd2);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   312
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   313
        } while (rv == -1 && errno == EINTR);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   314
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   315
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   316
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   317
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   318
     * Unlock without destroying errno
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   319
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   320
    orig_errno = errno;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   321
    pthread_mutex_unlock(&(fdEntry->lock));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   322
    errno = orig_errno;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   323
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   324
    return rv;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   325
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   326
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   327
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   328
 * Wrapper for dup2 - same semantics as dup2 system call except
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   329
 * that any threads blocked in an I/O system call on fd2 will be
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   330
 * preempted and return -1/EBADF;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   331
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   332
int NET_Dup2(int fd, int fd2) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   333
    if (fd < 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   334
        errno = EBADF;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   335
        return -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   336
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   337
    return closefd(fd, fd2);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   338
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   339
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   340
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   341
 * Wrapper for close - same semantics as close system call
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   342
 * except that any threads blocked in an I/O on fd will be
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   343
 * preempted and the I/O system call will return -1/EBADF.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   344
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   345
int NET_SocketClose(int fd) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   346
    return closefd(-1, fd);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   347
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   348
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   349
/************** Basic I/O operations here ***************/
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   350
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   351
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   352
 * Macro to perform a blocking IO operation. Restarts
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   353
 * automatically if interrupted by signal (other than
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   354
 * our wakeup signal)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   355
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   356
#define BLOCKING_IO_RETURN_INT(FD, FUNC) {      \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   357
    int ret;                                    \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   358
    threadEntry_t self;                         \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   359
    fdEntry_t *fdEntry = getFdEntry(FD);        \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   360
    if (fdEntry == NULL) {                      \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   361
        errno = EBADF;                          \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   362
        return -1;                              \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   363
    }                                           \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   364
    do {                                        \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   365
        startOp(fdEntry, &self);                \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   366
        ret = FUNC;                             \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   367
        endOp(fdEntry, &self);                  \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   368
    } while (ret == -1 && errno == EINTR);      \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   369
    return ret;                                 \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   370
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   371
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   372
int NET_Read(int s, void* buf, size_t len) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   373
    BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) );
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   374
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   375
40937
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 37609
diff changeset
   376
int NET_NonBlockingRead(int s, void* buf, size_t len) {
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 37609
diff changeset
   377
    BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, MSG_DONTWAIT));
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 37609
diff changeset
   378
}
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 37609
diff changeset
   379
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   380
int NET_RecvFrom(int s, void *buf, int len, unsigned int flags,
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 14342
diff changeset
   381
       struct sockaddr *from, socklen_t *fromlen) {
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 14342
diff changeset
   382
    BLOCKING_IO_RETURN_INT( s, recvfrom(s, buf, len, flags, from, fromlen) );
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   383
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   384
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   385
int NET_Send(int s, void *msg, int len, unsigned int flags) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   386
    BLOCKING_IO_RETURN_INT( s, send(s, msg, len, flags) );
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   387
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   388
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   389
int NET_SendTo(int s, const void *msg, int len,  unsigned  int
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   390
       flags, const struct sockaddr *to, int tolen) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   391
    BLOCKING_IO_RETURN_INT( s, sendto(s, msg, len, flags, to, tolen) );
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   392
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   393
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 14342
diff changeset
   394
int NET_Accept(int s, struct sockaddr *addr, socklen_t *addrlen) {
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 14342
diff changeset
   395
    BLOCKING_IO_RETURN_INT( s, accept(s, addr, addrlen) );
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   396
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   397
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   398
int NET_Connect(int s, struct sockaddr *addr, int addrlen) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   399
    BLOCKING_IO_RETURN_INT( s, connect(s, addr, addrlen) );
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   400
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   401
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   402
int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   403
    BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout) );
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   404
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   405
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   406
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   407
 * Wrapper for select(s, timeout). We are using select() on Mac OS due to Bug 7131399.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   408
 * Auto restarts with adjusted timeout if interrupted by
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   409
 * signal other than our wakeup signal.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   410
 */
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   411
int NET_Timeout(JNIEnv *env, int s, long timeout, jlong nanoTimeStamp) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   412
    struct timeval t, *tp = &t;
23043
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   413
    fd_set fds;
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   414
    fd_set* fdsp = NULL;
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   415
    int allocated = 0;
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   416
    threadEntry_t self;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   417
    fdEntry_t *fdEntry = getFdEntry(s);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   418
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   419
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   420
     * Check that fd hasn't been closed.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   421
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   422
    if (fdEntry == NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   423
        errno = EBADF;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   424
        return -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   425
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   426
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   427
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   428
     * Pick up current time as may need to adjust timeout
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   429
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   430
    if (timeout > 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   431
        /* Timed */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   432
        t.tv_sec = timeout / 1000;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   433
        t.tv_usec = (timeout % 1000) * 1000;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   434
    } else if (timeout < 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   435
        /* Blocking */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   436
        tp = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   437
    } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   438
        /* Poll */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   439
        t.tv_sec = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   440
        t.tv_usec = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   441
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   442
23043
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   443
    if (s < FD_SETSIZE) {
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   444
        fdsp = &fds;
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   445
        FD_ZERO(fdsp);
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   446
    } else {
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   447
        int length = (howmany(s+1, NFDBITS)) * sizeof(int);
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   448
        fdsp = (fd_set *) calloc(1, length);
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   449
        if (fdsp == NULL) {
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   450
            return -1;   // errno will be set to ENOMEM
23043
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   451
        }
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   452
        allocated = 1;
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   453
    }
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   454
    FD_SET(s, fdsp);
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   455
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   456
    jlong prevNanoTime = nanoTimeStamp;
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   457
    jlong nanoTimeout = (jlong) timeout * NET_NSEC_PER_MSEC;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   458
    for(;;) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   459
        int rv;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   460
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   461
        /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   462
         * call select on the fd. If interrupted by our wakeup signal
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   463
         * errno will be set to EBADF.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   464
         */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   465
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   466
        startOp(fdEntry, &self);
23043
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   467
        rv = select(s+1, fdsp, 0, 0, tp);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   468
        endOp(fdEntry, &self);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   469
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   470
        /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   471
         * If interrupted then adjust timeout. If timeout
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   472
         * has expired return 0 (indicating timeout expired).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   473
         */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   474
        if (rv < 0 && errno == EINTR) {
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   475
            jlong newNanoTime = JVM_NanoTime(env, 0);
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   476
            nanoTimeout -= newNanoTime - prevNanoTime;
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   477
            if (nanoTimeout < NET_NSEC_PER_MSEC) {
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   478
                if (allocated != 0)
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   479
                    free(fdsp);
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   480
                return 0;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   481
            }
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   482
            prevNanoTime = newNanoTime;
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   483
            t.tv_sec = nanoTimeout / NET_NSEC_PER_SEC;
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   484
            t.tv_usec = (nanoTimeout % NET_NSEC_PER_SEC) / NET_NSEC_PER_USEC;
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   485
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   486
        } else {
23043
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   487
            if (allocated != 0)
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   488
                free(fdsp);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   489
            return rv;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   490
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   491
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   492
}