jdk/src/windows/native/sun/nio/ch/WindowsSelectorImpl.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 894 15e617238139
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 2002 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/* Maximum number of sockets per select() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/* This number should be equal to WindowsSelectorImpl.MAX_SELECTABLE_FDS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/* This definition MUST precede the inclusion of winsock2.h */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#define FD_SETSIZE 1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include "sun_nio_ch_WindowsSelectorImpl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include "winsock2.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    jint fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    jshort events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
} pollfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
static int POLLIN   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
static int POLLCONN = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
static int POLLOUT  = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#define WAKEUP_SOCKET_BUF_SIZE 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
Java_sun_nio_ch_WindowsSelectorImpl_00024SubSelector_poll0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                                   jlong pollAddress, jint numfds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                                   jintArray returnReadFds, jintArray returnWriteFds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                   jintArray returnExceptFds, jlong timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    DWORD result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    pollfd *fds = (pollfd *) pollAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    FD_SET readfds, writefds, exceptfds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    struct timeval timevalue, *tv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static struct timeval zerotime = {0, 0};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    int read_count = 0, write_count = 0, except_count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
#ifdef _WIN64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    int resultbuf[FD_SETSIZE + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    if (timeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        tv = &zerotime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    } else if (timeout < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        tv = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        tv = &timevalue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        tv->tv_sec =  (long)(timeout / 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        tv->tv_usec = (long)((timeout % 1000) * 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /* Set FD_SET structures required for select */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    for (i = 0; i < numfds; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (fds[i].events & POLLIN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
           readfds.fd_array[read_count] = fds[i].fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
           read_count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (fds[i].events & (POLLOUT | POLLCONN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
           writefds.fd_array[write_count] = fds[i].fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
           write_count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        exceptfds.fd_array[except_count] = fds[i].fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        except_count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    readfds.fd_count = read_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    writefds.fd_count = write_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    exceptfds.fd_count = except_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /* Call select */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    if ((result = select(0 , &readfds, &writefds, &exceptfds, tv))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                                             == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        /* Bad error - this should not happen frequently */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        /* Iterate over sockets and call select() on each separately */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        FD_SET errreadfds, errwritefds, errexceptfds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        readfds.fd_count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        writefds.fd_count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        exceptfds.fd_count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        for (i = 0; i < numfds; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            /* prepare select structures for the i-th socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            errreadfds.fd_count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            errwritefds.fd_count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (fds[i].events & POLLIN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
               errreadfds.fd_array[0] = fds[i].fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
               errreadfds.fd_count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (fds[i].events & (POLLOUT | POLLCONN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                errwritefds.fd_array[0] = fds[i].fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                errwritefds.fd_count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            errexceptfds.fd_array[0] = fds[i].fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            errexceptfds.fd_count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            /* call select on the i-th socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (select(0, &errreadfds, &errwritefds, &errexceptfds, &zerotime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                                             == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                /* This socket causes an error. Add it to exceptfds set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                exceptfds.fd_array[exceptfds.fd_count] = fds[i].fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                exceptfds.fd_count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                /* This socket does not cause an error. Process result */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                if (errreadfds.fd_count == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    readfds.fd_array[readfds.fd_count] = fds[i].fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    readfds.fd_count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                if (errwritefds.fd_count == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    writefds.fd_array[writefds.fd_count] = fds[i].fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    writefds.fd_count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (errexceptfds.fd_count == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    exceptfds.fd_array[exceptfds.fd_count] = fds[i].fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    exceptfds.fd_count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /* Return selected sockets. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /* Each Java array consists of sockets count followed by sockets list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
#ifdef _WIN64
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    resultbuf[0] = readfds.fd_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    for (i = 0; i < (int)readfds.fd_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        resultbuf[i + 1] = (int)readfds.fd_array[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    (*env)->SetIntArrayRegion(env, returnReadFds, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                              readfds.fd_count + 1, resultbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    resultbuf[0] = writefds.fd_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    for (i = 0; i < (int)writefds.fd_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        resultbuf[i + 1] = (int)writefds.fd_array[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    (*env)->SetIntArrayRegion(env, returnWriteFds, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                              writefds.fd_count + 1, resultbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    resultbuf[0] = exceptfds.fd_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    for (i = 0; i < (int)exceptfds.fd_count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        resultbuf[i + 1] = (int)exceptfds.fd_array[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    (*env)->SetIntArrayRegion(env, returnExceptFds, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                              exceptfds.fd_count + 1, resultbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    (*env)->SetIntArrayRegion(env, returnReadFds, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                              readfds.fd_count + 1, (jint *)&readfds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    (*env)->SetIntArrayRegion(env, returnWriteFds, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                              writefds.fd_count + 1, (jint *)&writefds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    (*env)->SetIntArrayRegion(env, returnExceptFds, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                              exceptfds.fd_count + 1, (jint *)&exceptfds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
Java_sun_nio_ch_WindowsSelectorImpl_setWakeupSocket0(JNIEnv *env, jclass this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                                jint scoutFd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /* Write one byte into the pipe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    send(scoutFd, (char*)&POLLIN, 1, 0);
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_WindowsSelectorImpl_resetWakeupSocket0(JNIEnv *env, jclass this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                                jint scinFd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    char bytes[WAKEUP_SOCKET_BUF_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    long bytesToRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /* Drain socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /* Find out how many bytes available for read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    ioctlsocket (scinFd, FIONREAD, &bytesToRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    if (bytesToRead == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /* Prepare corresponding buffer if needed, and then read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    if (bytesToRead > WAKEUP_SOCKET_BUF_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        char* buf = (char*)malloc(bytesToRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        recv(scinFd, buf, bytesToRead, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        free(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        recv(scinFd, bytes, WAKEUP_SOCKET_BUF_SIZE, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
}