jdk/src/solaris/native/sun/nio/ch/EPollArrayWrapper.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1142 e01e390f6551
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "sun_nio_ch_EPollArrayWrapper.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <dlfcn.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <sys/resource.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <sys/time.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#ifdef  __cplusplus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
extern "C" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/* epoll_wait(2) man page */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
typedef union epoll_data {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    void *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    __uint32_t u32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    __uint64_t u64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
} epoll_data_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
struct epoll_event {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    __uint32_t events;  /* Epoll events */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    epoll_data_t data;  /* User data variable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
} __attribute__ ((__packed__));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#ifdef  __cplusplus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
#define RESTARTABLE(_cmd, _result) do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
  do { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    _result = _cmd; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
  } while((_result == -1) && (errno == EINTR)); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
} while(0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * epoll event notification is new in 2.6 kernel. As the offical build
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * platform for the JDK is on a 2.4-based distribution then we must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * obtain the addresses of the epoll functions dynamically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
typedef int (*epoll_create_t)(int size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
typedef int (*epoll_ctl_t)   (int epfd, int op, int fd, struct epoll_event *event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
typedef int (*epoll_wait_t)  (int epfd, struct epoll_event *events, int maxevents, int timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
static epoll_create_t epoll_create_func;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
static epoll_ctl_t    epoll_ctl_func;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
static epoll_wait_t   epoll_wait_func;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
iepoll(int epfd, struct epoll_event *events, int numfds, jlong timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    jlong start, now;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    int remaining = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    struct timeval t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    int diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    gettimeofday(&t, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    start = t.tv_sec * 1000 + t.tv_usec / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        int res = (*epoll_wait_func)(epfd, events, numfds, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (res < 0 && errno == EINTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (remaining >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                gettimeofday(&t, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                now = t.tv_sec * 1000 + t.tv_usec / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                diff = now - start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                remaining -= diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                if (diff < 0 || remaining <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                start = now;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
Java_sun_nio_ch_EPollArrayWrapper_init(JNIEnv *env, jclass this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    epoll_create_func = (epoll_create_t) dlsym(RTLD_DEFAULT, "epoll_create");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    epoll_ctl_func    = (epoll_ctl_t)    dlsym(RTLD_DEFAULT, "epoll_ctl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    epoll_wait_func   = (epoll_wait_t)   dlsym(RTLD_DEFAULT, "epoll_wait");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    if ((epoll_create_func == NULL) || (epoll_ctl_func == NULL) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        (epoll_wait_func == NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        JNU_ThrowInternalError(env, "unable to get address of epoll functions, pre-2.6 kernel?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
Java_sun_nio_ch_EPollArrayWrapper_epollCreate(JNIEnv *env, jobject this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * epoll_create expects a size as a hint to the kernel about how to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * dimension internal structures. We can't predict the size in advance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    int epfd = (*epoll_create_func)(256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    if (epfd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
       JNU_ThrowIOExceptionWithLastError(env, "epoll_create failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    return epfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
Java_sun_nio_ch_EPollArrayWrapper_fdLimit(JNIEnv *env, jclass this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    struct rlimit rlp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        JNU_ThrowIOExceptionWithLastError(env, "getrlimit failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    return (jint)rlp.rlim_max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
Java_sun_nio_ch_EPollArrayWrapper_epollCtl(JNIEnv *env, jobject this, jint epfd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                           jint opcode, jint fd, jint events)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    struct epoll_event event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    int res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    event.events = events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    event.data.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    RESTARTABLE((*epoll_ctl_func)(epfd, (int)opcode, (int)fd, &event), res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * A channel may be registered with several Selectors. When each Selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * is polled a EPOLL_CTL_DEL op will be inserted into its pending update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * list to remove the file descriptor from epoll. The "last" Selector will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * close the file descriptor which automatically unregisters it from each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * epoll descriptor. To avoid costly synchronization between Selectors we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * allow pending updates to be processed, ignoring errors. The errors are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * harmless as the last update for the file descriptor is guaranteed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * be EPOLL_CTL_DEL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    if (res < 0 && errno != EBADF && errno != ENOENT && errno != EPERM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        JNU_ThrowIOExceptionWithLastError(env, "epoll_ctl failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
Java_sun_nio_ch_EPollArrayWrapper_epollWait(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                            jlong address, jint numfds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                            jlong timeout, jint epfd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    struct epoll_event *events = jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    int res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    if (timeout <= 0) {           /* Indefinite or no wait */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        RESTARTABLE((*epoll_wait_func)(epfd, events, numfds, timeout), res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    } else {                      /* Bounded wait; bounded restarts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        res = iepoll(epfd, events, numfds, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    if (res < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        JNU_ThrowIOExceptionWithLastError(env, "epoll_wait failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
Java_sun_nio_ch_EPollArrayWrapper_interrupt(JNIEnv *env, jobject this, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    int fakebuf[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    fakebuf[0] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    if (write(fd, fakebuf, 1) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        JNU_ThrowIOExceptionWithLastError(env,"write to interrupt fd failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
}