jdk/src/java.base/macosx/native/libnet/bsd_close.c
author michaelm
Thu, 04 May 2017 18:13:42 +0100
changeset 44921 0672237e13c0
parent 44916 54d70322b32c
child 45061 74b09ee3cd55
child 46862 cb4b080576fa
permissions -rw-r--r--
8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux Reviewed-by: chegar
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
/*
44921
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
     2
 * Copyright (c) 2001, 2016, 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>
23043
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
    41
#include <sys/poll.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    42
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    43
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    44
 * Stack allocated by thread when doing blocking operation
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
typedef struct threadEntry {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    47
    pthread_t thr;                      /* this thread */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    48
    struct threadEntry *next;           /* next thread */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    49
    int intr;                           /* interrupted */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    50
} threadEntry_t;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    51
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    52
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    53
 * Heap allocated during initialized - one entry per fd
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
typedef struct {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    56
    pthread_mutex_t lock;               /* fd lock */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    57
    threadEntry_t *threads;             /* threads blocked on fd */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    58
} fdEntry_t;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    59
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    60
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    61
 * Signal to unblock thread
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
static int sigWakeup = SIGIO;
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
/*
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
    66
 * 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
    67
 * 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
    68
 * 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
    69
 * 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
    70
 * 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
    71
 * 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
    72
 * 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
    73
 * 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
    74
 * keeping memory consumption reasonable.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    75
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    76
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
    77
/* 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
    78
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
    79
/* 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
    80
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
    81
/* 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
    82
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
    83
/* 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
    84
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
    85
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
/* 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
    87
 *   an array of n slabs, each holding 64k entries.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    88
 */
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
    89
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
    90
/* 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
    91
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
    92
/* 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
    93
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
    94
pthread_mutex_t fdOverflowTableLock = PTHREAD_MUTEX_INITIALIZER;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    95
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    96
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    97
 * Null signal handler
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
static void sig_wakeup(int sig) {
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
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
 * Initialization routine (executed when library is loaded)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   104
 * Allocate fd tables and sets up signal handler.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   105
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   106
static void __attribute((constructor)) init() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   107
    struct rlimit nbr_files;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   108
    sigset_t sigset;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   109
    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
   110
    int i = 0;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   111
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
    /* 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
   113
    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
   114
        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
   115
                "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
   116
        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
   117
    }
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
    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
   119
        fdLimit = nbr_files.rlim_max;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   120
    } 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
   121
        /* 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
   122
        fdLimit = INT_MAX;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   123
    }
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
   124
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   125
    /* 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
   126
    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
   127
    fdTable = (fdEntry_t*) calloc(fdTableLen, sizeof(fdEntry_t));
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   128
    if (fdTable == NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   129
        fprintf(stderr, "library initialization failed - "
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   130
                "unable to allocate file descriptor table - out of memory");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   131
        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
   132
    } 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
   133
        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
   134
            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
   135
        }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   136
    }
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
   137
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   138
    /* 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
   139
    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
   140
        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
   141
        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
   142
        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
   143
            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
   144
                    "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
   145
            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
   146
        }
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   147
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   148
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
     * Setup the signal handler
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
    sa.sa_handler = sig_wakeup;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   153
    sa.sa_flags   = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   154
    sigemptyset(&sa.sa_mask);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   155
    sigaction(sigWakeup, &sa, NULL);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   156
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   157
    sigemptyset(&sigset);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   158
    sigaddset(&sigset, sigWakeup);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   159
    sigprocmask(SIG_UNBLOCK, &sigset, NULL);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   160
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   161
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   162
/*
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
   163
 * Return the fd table for this fd.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   164
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   165
static inline fdEntry_t *getFdEntry(int fd)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   166
{
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
   167
    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
   168
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
    if (fd < 0) {
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   170
        return NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   171
    }
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
   172
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   173
    /* 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
   174
     * 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
   175
    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
   176
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
    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
   178
        /* 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
   179
        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
   180
        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
   181
    } 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
   182
        /* 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
   183
        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
   184
        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
   185
        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
   186
        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
   187
        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
   188
        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
   189
        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
   190
        /* 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
   191
        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
   192
            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
   193
                (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
   194
            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
   195
                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
   196
                        " 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
   197
                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
   198
                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
   199
            } 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
   200
                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
   201
                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
   202
                    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
   203
                }
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
                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
   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
        }
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
        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
   208
        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
   209
        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
   210
    }
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
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
    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
   213
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   214
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   215
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
   216
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   217
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   218
 * Start a blocking operation :-
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   219
 *    Insert thread onto thread list for the fd.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   220
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   221
static inline void startOp(fdEntry_t *fdEntry, threadEntry_t *self)
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
    self->thr = pthread_self();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   224
    self->intr = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   225
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   226
    pthread_mutex_lock(&(fdEntry->lock));
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
        self->next = fdEntry->threads;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   229
        fdEntry->threads = self;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   230
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   231
    pthread_mutex_unlock(&(fdEntry->lock));
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
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
 * End a blocking operation :-
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   236
 *     Remove thread from thread list for the fd
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   237
 *     If fd has been interrupted then set errno to EBADF
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   238
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   239
static inline void endOp
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   240
    (fdEntry_t *fdEntry, threadEntry_t *self)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   241
{
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   242
    int orig_errno = errno;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   243
    pthread_mutex_lock(&(fdEntry->lock));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   244
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   245
        threadEntry_t *curr, *prev=NULL;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   246
        curr = fdEntry->threads;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   247
        while (curr != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   248
            if (curr == self) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   249
                if (curr->intr) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   250
                    orig_errno = EBADF;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   251
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   252
                if (prev == NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   253
                    fdEntry->threads = curr->next;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   254
                } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   255
                    prev->next = curr->next;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   256
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   257
                break;
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
            prev = curr;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   260
            curr = curr->next;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   261
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   262
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   263
    pthread_mutex_unlock(&(fdEntry->lock));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   264
    errno = orig_errno;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   265
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   266
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
 * 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
   269
 * the file descriptor are notified via a wakeup signal.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   270
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   271
 *      fd1 < 0    => close(fd2)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   272
 *      fd1 >= 0   => dup2(fd1, fd2)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   273
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   274
 * Returns -1 with errno set if operation fails.
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
static int closefd(int fd1, int fd2) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   277
    int rv, orig_errno;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   278
    fdEntry_t *fdEntry = getFdEntry(fd2);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   279
    if (fdEntry == NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   280
        errno = EBADF;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   281
        return -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   282
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   283
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
     * 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
   286
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   287
    pthread_mutex_lock(&(fdEntry->lock));
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
    {
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
         * Send a wakeup signal to all threads blocked on this
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   292
         * file descriptor.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   293
         */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   294
        threadEntry_t *curr = fdEntry->threads;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   295
        while (curr != NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   296
            curr->intr = 1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   297
            pthread_kill( curr->thr, sigWakeup );
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   298
            curr = curr->next;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   299
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   300
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
         * And close/dup the file descriptor
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   303
         * (restart if interrupted by signal)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   304
         */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   305
        do {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   306
            if (fd1 < 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   307
                rv = close(fd2);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   308
            } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   309
                rv = dup2(fd1, fd2);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   310
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   311
        } while (rv == -1 && errno == EINTR);
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
    }
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
     * Unlock without destroying errno
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
    orig_errno = errno;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   319
    pthread_mutex_unlock(&(fdEntry->lock));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   320
    errno = orig_errno;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   321
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   322
    return rv;
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
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
 * Wrapper for dup2 - same semantics as dup2 system call except
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   327
 * 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
   328
 * preempted and return -1/EBADF;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   329
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   330
int NET_Dup2(int fd, int fd2) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   331
    if (fd < 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   332
        errno = EBADF;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   333
        return -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   334
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   335
    return closefd(fd, fd2);
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
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
 * Wrapper for close - same semantics as close system call
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   340
 * 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
   341
 * preempted and the I/O system call will return -1/EBADF.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   342
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   343
int NET_SocketClose(int fd) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   344
    return closefd(-1, fd);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   345
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   346
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   347
/************** Basic I/O operations here ***************/
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
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   350
 * Macro to perform a blocking IO operation. Restarts
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   351
 * automatically if interrupted by signal (other than
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   352
 * our wakeup signal)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   353
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   354
#define BLOCKING_IO_RETURN_INT(FD, FUNC) {      \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   355
    int ret;                                    \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   356
    threadEntry_t self;                         \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   357
    fdEntry_t *fdEntry = getFdEntry(FD);        \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   358
    if (fdEntry == NULL) {                      \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   359
        errno = EBADF;                          \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   360
        return -1;                              \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   361
    }                                           \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   362
    do {                                        \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   363
        startOp(fdEntry, &self);                \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   364
        ret = FUNC;                             \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   365
        endOp(fdEntry, &self);                  \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   366
    } while (ret == -1 && errno == EINTR);      \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   367
    return ret;                                 \
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   368
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   369
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   370
int NET_Read(int s, void* buf, size_t len) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   371
    BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) );
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   372
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   373
40937
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 37609
diff changeset
   374
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
   375
    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
   376
}
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 37609
diff changeset
   377
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   378
int NET_ReadV(int s, const struct iovec * vector, int count) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   379
    BLOCKING_IO_RETURN_INT( s, readv(s, vector, count) );
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   380
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   381
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   382
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
   383
       struct sockaddr *from, socklen_t *fromlen) {
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 14342
diff changeset
   384
    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
   385
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   386
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   387
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
   388
    BLOCKING_IO_RETURN_INT( s, send(s, msg, len, flags) );
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   389
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   390
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   391
int NET_WriteV(int s, const struct iovec * vector, int count) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   392
    BLOCKING_IO_RETURN_INT( s, writev(s, vector, count) );
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   393
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   394
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   395
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
   396
       flags, const struct sockaddr *to, int tolen) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   397
    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
   398
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   399
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 14342
diff changeset
   400
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
   401
    BLOCKING_IO_RETURN_INT( s, accept(s, addr, addrlen) );
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   402
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   403
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   404
int NET_Connect(int s, struct sockaddr *addr, int addrlen) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   405
    BLOCKING_IO_RETURN_INT( s, connect(s, addr, addrlen) );
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
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   408
int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   409
    BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout) );
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   410
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   411
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   412
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   413
 * 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
   414
 * Auto restarts with adjusted timeout if interrupted by
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   415
 * signal other than our wakeup signal.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   416
 */
44921
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
   417
int NET_Timeout0(int s, long timeout, long currentTime) {
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
   418
    long prevtime = currentTime, newtime;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   419
    struct timeval t, *tp = &t;
23043
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   420
    fd_set fds;
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   421
    fd_set* fdsp = NULL;
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   422
    int allocated = 0;
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   423
    threadEntry_t self;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   424
    fdEntry_t *fdEntry = getFdEntry(s);
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
     * Check that fd hasn't been closed.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   428
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   429
    if (fdEntry == NULL) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   430
        errno = EBADF;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   431
        return -1;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   432
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   433
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   434
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   435
     * Pick up current time as may need to adjust timeout
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   436
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   437
    if (timeout > 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   438
        /* Timed */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   439
        t.tv_sec = timeout / 1000;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   440
        t.tv_usec = (timeout % 1000) * 1000;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   441
    } else if (timeout < 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   442
        /* Blocking */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   443
        tp = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   444
    } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   445
        /* Poll */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   446
        t.tv_sec = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   447
        t.tv_usec = 0;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   448
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   449
23043
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   450
    if (s < FD_SETSIZE) {
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   451
        fdsp = &fds;
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   452
        FD_ZERO(fdsp);
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   453
    } else {
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   454
        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
   455
        fdsp = (fd_set *) calloc(1, length);
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   456
        if (fdsp == NULL) {
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   457
            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
   458
        }
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   459
        allocated = 1;
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   460
    }
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   461
    FD_SET(s, fdsp);
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   462
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   463
    for(;;) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   464
        int rv;
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
        /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   467
         * call select on the fd. If interrupted by our wakeup signal
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   468
         * errno will be set to EBADF.
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
        startOp(fdEntry, &self);
23043
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   472
        rv = select(s+1, fdsp, 0, 0, tp);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   473
        endOp(fdEntry, &self);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   474
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   475
        /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   476
         * If interrupted then adjust timeout. If timeout
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   477
         * has expired return 0 (indicating timeout expired).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   478
         */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   479
        if (rv < 0 && errno == EINTR) {
44921
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
   480
            if (timeout > 0) {
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
   481
                struct timeval now;
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
   482
                gettimeofday(&now, NULL);
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
   483
                newtime = now.tv_sec * 1000  +  now.tv_usec / 1000;
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
   484
                timeout -= newtime - prevtime;
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
   485
                if (timeout <= 0) {
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
   486
                    if (allocated != 0)
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
   487
                        free(fdsp);
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
   488
                    return 0;
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
   489
                }
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
   490
                prevtime = newtime;
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
   491
                t.tv_sec = timeout / 1000;
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
   492
                t.tv_usec = (timeout % 1000) * 1000;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   493
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   494
        } else {
23043
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   495
            if (allocated != 0)
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
   496
                free(fdsp);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   497
            return rv;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   498
        }
44921
0672237e13c0 8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux
michaelm
parents: 44916
diff changeset
   499
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   500
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   501
}