src/java.base/aix/native/libnet/aix_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:
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
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.
59197
bb3472aa26c8 8234501: remove obsolete NET_ReadV
mbaesken
parents: 53741
diff changeset
     3
 * Copyright (c) 2016, 2019, SAP SE and/or its affiliates. All rights reserved.
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
     4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
     5
 *
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
     6
 * This code is free software; you can redistribute it and/or modify it
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
     7
 * under the terms of the GNU General Public License version 2 only, as
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
     8
 * published by the Free Software Foundation.  Oracle designates this
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
     9
 * particular file as subject to the "Classpath" exception as provided
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    10
 * by Oracle in the LICENSE file that accompanied this code.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    11
 *
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    12
 * This code is distributed in the hope that it will be useful, but WITHOUT
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    14
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    15
 * version 2 for more details (a copy is included in the LICENSE file that
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    16
 * accompanied this code).
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    17
 *
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    18
 * You should have received a copy of the GNU General Public License version
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    19
 * 2 along with this work; if not, write to the Free Software Foundation,
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    20
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    21
 *
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    22
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    23
 * or visit www.oracle.com if you need additional information or have any
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    24
 * questions.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    25
 */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    26
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    27
/*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    28
 * This file contains implementations of NET_... functions. The NET_.. functions are
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    29
 * wrappers for common file- and socket functions plus provisions for non-blocking IO.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    30
 *
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    31
 * (basically, the layers remember all  file descriptors waiting for a particular fd;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    32
 *  all threads waiting on a certain fd can be woken up by sending them a signal; this
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    33
 *  is done e.g. when the fd is closed.)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    34
 *
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    35
 * This was originally copied from the linux_close.c implementation.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    36
 *
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    37
 * Side Note: This coding needs initialization. Under Linux this is done
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    38
 * automatically via __attribute((constructor)), on AIX this is done manually
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    39
 * (see aix_close_init).
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    40
 *
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    41
 */
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22605
diff changeset
    42
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22605
diff changeset
    43
/*
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22605
diff changeset
    44
   AIX needs a workaround for I/O cancellation, see:
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22605
diff changeset
    45
   http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf1/close.htm
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22605
diff changeset
    46
   ...
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22605
diff changeset
    47
   The close subroutine is blocked until all subroutines which use the file
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22605
diff changeset
    48
   descriptor return to usr space. For example, when a thread is calling close
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22605
diff changeset
    49
   and another thread is calling select with the same file descriptor, the
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22605
diff changeset
    50
   close subroutine does not return until the select call returns.
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22605
diff changeset
    51
   ...
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22605
diff changeset
    52
*/
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 22605
diff changeset
    53
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
    54
#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
    55
#include <limits.h>
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    56
#include <stdio.h>
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    57
#include <stdlib.h>
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    58
#include <signal.h>
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    59
#include <pthread.h>
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    60
#include <sys/types.h>
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    61
#include <sys/socket.h>
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    62
#include <sys/time.h>
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    63
#include <sys/resource.h>
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    64
#include <sys/uio.h>
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    65
#include <unistd.h>
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    66
#include <errno.h>
46862
cb4b080576fa 8180003: Remove sys/ prefix from poll.h and signal.h includes
mikael
parents: 44921
diff changeset
    67
#include <poll.h>
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
    68
#include "jvm.h"
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
    69
#include "net_util.h"
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    70
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    71
/*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    72
 * Stack allocated by thread when doing blocking operation
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    73
 */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    74
typedef struct threadEntry {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    75
    pthread_t thr;                      /* this thread */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    76
    struct threadEntry *next;           /* next thread */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    77
    int intr;                           /* interrupted */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    78
} threadEntry_t;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    79
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    80
/*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    81
 * Heap allocated during initialized - one entry per fd
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    82
 */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    83
typedef struct {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    84
    pthread_mutex_t lock;               /* fd lock */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    85
    threadEntry_t *threads;             /* threads blocked on fd */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    86
} fdEntry_t;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    87
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    88
/*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    89
 * Signal to unblock thread
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    90
 */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    91
static int sigWakeup = (SIGRTMAX - 1);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    92
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
    93
/*
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
    94
 * 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
    95
 * 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
    96
 * 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
    97
 * 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
    98
 * 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
    99
 * 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
   100
 * 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
   101
 * 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
   102
 * keeping memory consumption reasonable.
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   103
 */
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
   104
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   105
/* 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
   106
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
   107
/* 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
   108
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
   109
/* 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
   110
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
   111
/* 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
   112
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
   113
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
/* 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
   115
 *   an array of n slabs, each holding 64k 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
   116
 */
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
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
   118
/* 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
   119
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
   120
/* 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
   121
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
   122
pthread_mutex_t fdOverflowTableLock = PTHREAD_MUTEX_INITIALIZER;
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   123
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   124
/*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   125
 * Null signal handler
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   126
 */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   127
static void sig_wakeup(int sig) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   128
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   129
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   130
/*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   131
 * Initialization routine (executed when library is loaded)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   132
 * Allocate fd tables and sets up signal handler.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   133
 *
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   134
 * On AIX we don't have __attribute((constructor)) so we need to initialize
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   135
 * manually (from JNI_OnLoad() in 'src/share/native/java/net/net_util.c')
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   136
 */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   137
void aix_close_init() {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   138
    struct rlimit nbr_files;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   139
    sigset_t sigset;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   140
    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
   141
    int i = 0;
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   142
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
   143
    /* Determine the maximum number of possible file descriptors. */
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   144
    if (-1 == getrlimit(RLIMIT_NOFILE, &nbr_files)) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   145
        fprintf(stderr, "library initialization failed - "
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   146
                "unable to get max # of allocated fds\n");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   147
        abort();
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   148
    }
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
   149
    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
   150
        fdLimit = nbr_files.rlim_max;
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   151
    } 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
   152
        /* 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
   153
        fdLimit = INT_MAX;
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   154
    }
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
   155
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   156
    /* 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
   157
    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
   158
    fdTable = (fdEntry_t*) calloc(fdTableLen, sizeof(fdEntry_t));
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   159
    if (fdTable == NULL) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   160
        fprintf(stderr, "library initialization failed - "
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   161
                "unable to allocate file descriptor table - out of memory");
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   162
        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
   163
    } 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
   164
        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
   165
            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
   166
        }
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   167
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
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
    /* 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
   170
    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
   171
        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
   172
        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
   173
        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
   174
            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
   175
                    "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
   176
            abort();
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   177
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   178
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   179
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   180
    /*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   181
     * Setup the signal handler
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   182
     */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   183
    sa.sa_handler = sig_wakeup;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   184
    sa.sa_flags   = 0;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   185
    sigemptyset(&sa.sa_mask);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   186
    sigaction(sigWakeup, &sa, NULL);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   187
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   188
    sigemptyset(&sigset);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   189
    sigaddset(&sigset, sigWakeup);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   190
    sigprocmask(SIG_UNBLOCK, &sigset, NULL);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   191
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   192
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   193
/*
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
   194
 * Return the fd table for this fd.
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   195
 */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   196
static inline fdEntry_t *getFdEntry(int fd)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   197
{
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
   198
    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
   199
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
    if (fd < 0) {
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   201
        return NULL;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   202
    }
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
   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
    /* 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
   205
     * 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
   206
    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
   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
    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
   209
        /* 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
   210
        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
   211
        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
   212
    } 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
   213
        /* 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
   214
        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
   215
        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
   216
        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
   217
        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
   218
        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
   219
        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
   220
        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
   221
        /* 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
   222
        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
   223
            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
   224
                (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
   225
            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
   226
                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
   227
                        " 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
   228
                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
   229
                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
   230
            } 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
   231
                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
   232
                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
   233
                    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
   234
                }
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   235
                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
   236
            }
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   237
        }
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   238
        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
   239
        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
   240
        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
   241
    }
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   242
332ee9a0ec11 8150460: (linux|bsd|aix)_close.c: file descriptor table may become large or may not work at all
stuefe
parents: 28658
diff changeset
   243
    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
   244
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   245
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   246
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
   247
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   248
/*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   249
 * Start a blocking operation :-
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   250
 *    Insert thread onto thread list for the fd.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   251
 */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   252
static inline void startOp(fdEntry_t *fdEntry, threadEntry_t *self)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   253
{
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   254
    self->thr = pthread_self();
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   255
    self->intr = 0;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   256
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   257
    pthread_mutex_lock(&(fdEntry->lock));
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   258
    {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   259
        self->next = fdEntry->threads;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   260
        fdEntry->threads = self;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   261
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   262
    pthread_mutex_unlock(&(fdEntry->lock));
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   263
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   264
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   265
/*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   266
 * End a blocking operation :-
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   267
 *     Remove thread from thread list for the fd
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   268
 *     If fd has been interrupted then set errno to EBADF
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   269
 */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   270
static inline void endOp
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   271
    (fdEntry_t *fdEntry, threadEntry_t *self)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   272
{
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   273
    int orig_errno = errno;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   274
    pthread_mutex_lock(&(fdEntry->lock));
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   275
    {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   276
        threadEntry_t *curr, *prev=NULL;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   277
        curr = fdEntry->threads;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   278
        while (curr != NULL) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   279
            if (curr == self) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   280
                if (curr->intr) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   281
                    orig_errno = EBADF;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   282
                }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   283
                if (prev == NULL) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   284
                    fdEntry->threads = curr->next;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   285
                } else {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   286
                    prev->next = curr->next;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   287
                }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   288
                break;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   289
            }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   290
            prev = curr;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   291
            curr = curr->next;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   292
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   293
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   294
    pthread_mutex_unlock(&(fdEntry->lock));
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   295
    errno = orig_errno;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   296
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   297
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   298
/*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   299
 * Close or dup2 a file descriptor ensuring that all threads blocked on
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   300
 * the file descriptor are notified via a wakeup signal.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   301
 *
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   302
 *      fd1 < 0    => close(fd2)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   303
 *      fd1 >= 0   => dup2(fd1, fd2)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   304
 *
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   305
 * Returns -1 with errno set if operation fails.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   306
 */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   307
static int closefd(int fd1, int fd2) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   308
    int rv, orig_errno;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   309
    fdEntry_t *fdEntry = getFdEntry(fd2);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   310
    if (fdEntry == NULL) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   311
        errno = EBADF;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   312
        return -1;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   313
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   314
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   315
    /*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   316
     * Lock the fd to hold-off additional I/O on this fd.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   317
     */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   318
    pthread_mutex_lock(&(fdEntry->lock));
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   319
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   320
    {
22605
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   321
        /* On fast machines we see that we enter dup2 before the
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   322
         * accepting thread had a chance to get and process the signal.
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   323
         * So in case we woke a thread up, give it some time to cope.
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   324
         * Also see https://bugs.openjdk.java.net/browse/JDK-8006395 */
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   325
        int num_woken = 0;
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   326
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   327
        /*
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   328
         * Send a wakeup signal to all threads blocked on this
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   329
         * file descriptor.
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   330
         */
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   331
        threadEntry_t *curr = fdEntry->threads;
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   332
        while (curr != NULL) {
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   333
            curr->intr = 1;
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   334
            pthread_kill( curr->thr, sigWakeup );
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   335
            num_woken ++;
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   336
            curr = curr->next;
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   337
        }
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   338
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   339
        if (num_woken > 0) {
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   340
          usleep(num_woken * 50);
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   341
        }
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 22597
diff changeset
   342
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   343
        /*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   344
         * And close/dup the file descriptor
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   345
         * (restart if interrupted by signal)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   346
         */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   347
        do {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   348
            if (fd1 < 0) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   349
                rv = close(fd2);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   350
            } else {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   351
                rv = dup2(fd1, fd2);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   352
            }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   353
        } while (rv == -1 && errno == EINTR);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   354
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   355
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   356
    /*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   357
     * Unlock without destroying errno
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   358
     */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   359
    orig_errno = errno;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   360
    pthread_mutex_unlock(&(fdEntry->lock));
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   361
    errno = orig_errno;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   362
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   363
    return rv;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   364
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   365
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   366
/*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   367
 * Wrapper for dup2 - same semantics as dup2 system call except
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   368
 * that any threads blocked in an I/O system call on fd2 will be
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   369
 * preempted and return -1/EBADF;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   370
 */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   371
int NET_Dup2(int fd, int fd2) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   372
    if (fd < 0) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   373
        errno = EBADF;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   374
        return -1;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   375
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   376
    return closefd(fd, fd2);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   377
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   378
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   379
/*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   380
 * Wrapper for close - same semantics as close system call
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   381
 * except that any threads blocked in an I/O on fd will be
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   382
 * preempted and the I/O system call will return -1/EBADF.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   383
 */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   384
int NET_SocketClose(int fd) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   385
    return closefd(-1, fd);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   386
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   387
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   388
/************** Basic I/O operations here ***************/
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   389
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   390
/*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   391
 * Macro to perform a blocking IO operation. Restarts
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   392
 * automatically if interrupted by signal (other than
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   393
 * our wakeup signal)
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   394
 */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   395
#define BLOCKING_IO_RETURN_INT(FD, FUNC) {      \
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   396
    int ret;                                    \
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   397
    threadEntry_t self;                         \
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   398
    fdEntry_t *fdEntry = getFdEntry(FD);        \
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   399
    if (fdEntry == NULL) {                      \
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   400
        errno = EBADF;                          \
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   401
        return -1;                              \
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   402
    }                                           \
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   403
    do {                                        \
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   404
        startOp(fdEntry, &self);                \
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   405
        ret = FUNC;                             \
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   406
        endOp(fdEntry, &self);                  \
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   407
    } while (ret == -1 && errno == EINTR);      \
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   408
    return ret;                                 \
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   409
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   410
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   411
int NET_Read(int s, void* buf, size_t len) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   412
    BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) );
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   413
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   414
40937
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 37609
diff changeset
   415
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
   416
    BLOCKING_IO_RETURN_INT(s, recv(s, buf, len, MSG_NONBLOCK));
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 37609
diff changeset
   417
}
f72e61ec734b 8075484: SocketInputStream.socketRead0 can hang even with soTimeout set
vtewari
parents: 37609
diff changeset
   418
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   419
int NET_RecvFrom(int s, void *buf, int len, unsigned int flags,
45257
09fdadea5fe8 8180424: Another build issue on AIX after 8034174
stuefe
parents: 40937
diff changeset
   420
       struct sockaddr *from, socklen_t *fromlen) {
09fdadea5fe8 8180424: Another build issue on AIX after 8034174
stuefe
parents: 40937
diff changeset
   421
    BLOCKING_IO_RETURN_INT( s, recvfrom(s, buf, len, flags, from, fromlen) );
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   422
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   423
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   424
int NET_Send(int s, void *msg, int len, unsigned int flags) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   425
    BLOCKING_IO_RETURN_INT( s, send(s, msg, len, flags) );
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   426
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   427
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   428
int NET_SendTo(int s, const void *msg, int len,  unsigned  int
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   429
       flags, const struct sockaddr *to, int tolen) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   430
    BLOCKING_IO_RETURN_INT( s, sendto(s, msg, len, flags, to, tolen) );
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   431
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   432
45257
09fdadea5fe8 8180424: Another build issue on AIX after 8034174
stuefe
parents: 40937
diff changeset
   433
int NET_Accept(int s, struct sockaddr *addr, socklen_t *addrlen) {
09fdadea5fe8 8180424: Another build issue on AIX after 8034174
stuefe
parents: 40937
diff changeset
   434
    BLOCKING_IO_RETURN_INT( s, accept(s, addr, addrlen) );
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   435
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   436
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   437
int NET_Connect(int s, struct sockaddr *addr, int addrlen) {
24858
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   438
    int crc = -1, prc = -1;
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   439
    threadEntry_t self;
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   440
    fdEntry_t* fdEntry = getFdEntry(s);
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   441
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   442
    if (fdEntry == NULL) {
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   443
        errno = EBADF;
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   444
        return -1;
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   445
    }
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   446
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   447
    /* On AIX, when the system call connect() is interrupted, the connection
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   448
     * is not aborted and it will be established asynchronously by the kernel.
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   449
     * Hence, no need to restart connect() when EINTR is received
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   450
     */
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   451
    startOp(fdEntry, &self);
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   452
    crc = connect(s, addr, addrlen);
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   453
    endOp(fdEntry, &self);
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   454
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   455
    if (crc == -1 && errno == EINTR) {
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   456
        struct pollfd s_pollfd;
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   457
        int sockopt_arg = 0;
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   458
        socklen_t len;
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   459
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   460
        s_pollfd.fd = s;
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   461
        s_pollfd.events = POLLOUT | POLLERR;
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   462
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   463
        /* poll the file descriptor */
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   464
        do {
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   465
            startOp(fdEntry, &self);
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   466
            prc = poll(&s_pollfd, 1, -1);
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   467
            endOp(fdEntry, &self);
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   468
        } while (prc == -1  && errno == EINTR);
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   469
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   470
        if (prc < 0)
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   471
            return prc;
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   472
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   473
        len = sizeof(sockopt_arg);
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   474
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   475
        /* Check whether the connection has been established */
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   476
        if (getsockopt(s, SOL_SOCKET, SO_ERROR, &sockopt_arg, &len) == -1)
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   477
            return -1;
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   478
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   479
        if (sockopt_arg != 0 ) {
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   480
            errno = sockopt_arg;
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   481
            return -1;
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   482
        }
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   483
    } else {
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   484
        return crc;
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   485
    }
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   486
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   487
    /* At this point, fd is connected. Set successful return code */
6907286cb170 8043954: Add async connect() support to NET_Connect() for AIX platform
luchsh
parents: 23048
diff changeset
   488
    return 0;
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   489
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   490
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   491
int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   492
    BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout) );
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   493
}
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   494
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   495
/*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   496
 * Wrapper for poll(s, timeout).
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   497
 * Auto restarts with adjusted timeout if interrupted by
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   498
 * signal other than our wakeup signal.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   499
 */
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   500
int NET_Timeout(JNIEnv *env, int s, long timeout, jlong nanoTimeStamp) {
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   501
    jlong prevNanoTime = nanoTimeStamp;
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   502
    jlong nanoTimeout = (jlong) timeout * NET_NSEC_PER_MSEC;
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   503
    fdEntry_t *fdEntry = getFdEntry(s);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   504
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   505
    /*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   506
     * Check that fd hasn't been closed.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   507
     */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   508
    if (fdEntry == NULL) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   509
        errno = EBADF;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   510
        return -1;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   511
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   512
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   513
    for(;;) {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   514
        struct pollfd pfd;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   515
        int rv;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   516
        threadEntry_t self;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   517
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   518
        /*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   519
         * Poll the fd. If interrupted by our wakeup signal
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   520
         * errno will be set to EBADF.
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   521
         */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   522
        pfd.fd = s;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   523
        pfd.events = POLLIN | POLLERR;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   524
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   525
        startOp(fdEntry, &self);
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   526
        rv = poll(&pfd, 1, nanoTimeout / NET_NSEC_PER_MSEC);
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   527
        endOp(fdEntry, &self);
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   528
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   529
        /*
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   530
         * If interrupted then adjust timeout. If timeout
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   531
         * has expired return 0 (indicating timeout expired).
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   532
         */
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   533
        if (rv < 0 && errno == EINTR) {
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   534
            jlong newNanoTime = JVM_NanoTime(env, 0);
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   535
            nanoTimeout -= newNanoTime - prevNanoTime;
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   536
            if (nanoTimeout < NET_NSEC_PER_MSEC) {
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   537
                return 0;
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   538
            }
45061
74b09ee3cd55 8179905: Remove the use of gettimeofday in Networking code
vtewari
parents: 44921
diff changeset
   539
            prevNanoTime = newNanoTime;
22597
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   540
        } else {
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   541
            return rv;
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   542
        }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   543
    }
7515a991bb37 8024854: PPC64: Basic changes and files to build the class library on AIX
simonis
parents:
diff changeset
   544
}