jdk/src/solaris/demo/jni/Poller/Poller.c
author ohair
Tue, 22 Jun 2010 17:07:17 -0700
changeset 5966 4cc8b71dfb19
parent 5506 202f599c92aa
child 7668 d4a77089c587
permissions -rw-r--r--
6939022: Source code adjustments for parfait compilation Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 2000, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 **********************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Poller.c :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * JNI code for use with Poller.java, principally to take advantage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * of poll() or /dev/poll multiplexing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * One will need Solaris 8 or Solaris 7 with adequate patches to take
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * advantage of the /dev/poll performance enhancements, though any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * version of Solaris 7 will automatically use the kernel poll()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * caching.  And poll() will function in 2.5.1 and 2.6 as well, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * will not perform well for large numbers of file descriptors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Several assumptions have been made to simplify this code :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *  1> At most MAX_HANDLES (32) separate pollable entities are currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *     supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *  2> Global synchronization from Java is assumed for all init, create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *     and destroy routines.  Per Object (handle passed in) synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *     is required for all AddFd, RemoveFd, IsMember, and Wait routines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *  3> It is currently up to the user to handle waking up an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *     existing nativeWait() call to do an addfd or removefd on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *     that set...could implement that here with an extra pipe, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *     with a pair of loopback sockets in Poller.java or user code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     In most cases interruption is not necessary for deletions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *     so long as deletions are queued up outside the Poller class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *     and then executed the next time waitMultiple() returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *  4> /dev/poll performance could be slightly improved by coalescing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *     adds/removes so that a write() is only done before the ioctl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *     (DP_POLL), but this complicates exception handling and sees
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *     only modest performance gains so wasn't done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *  5> /dev/poll does not report errors on attempts to remove non-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *     extant fds, but a future bug fix to the /dev/poll device driver
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *     should solve this problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *  6> Could add simpler code for pre-Solaris 7 releases which will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *     perform slightly better on those OSs.  But again there
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *     are only modest gains to be had from these new code paths,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *     so they've been ommitted here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Compile "cc -G -o <dest_dir>/libpoller.so -I ${JAVA_HOME}/include " \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * -I ${JAVA_HOME}/include/solaris Poller.c" and place the <dest_dir>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * in your LD_LIBRARY_PATH
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 **********************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
#include <unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
#include <poll.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
#include <malloc.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * Remove "_NOT"s to turn on features
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * Append "_NOT" to turn off features.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * Use of /dev/poll requires both the include file and kernel driver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
#define DEBUG_NOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
#define DEVPOLL_NOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
#ifdef DEVPOLL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
#include <sys/devpoll.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
#include "Poller.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
#define MAX_HANDLES 32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
#define DBGMSG(x) printf x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
#define ASSERT(x) {if (!(x)) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                   printf("assertion(%s) failed at line : %d\n",#x,__LINE__);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
#define CHECK_HANDLE(x) check_handle(x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
#define DBGMSG(x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
#define ASSERT(x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
#define CHECK_HANDLE(x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * Globals ...protect all with a global synchronization object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
static int Current_handle = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
static int Use_devpoll = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
static int Max_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * Per Poller object data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * Must be synchronized on a per Poller object basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
typedef struct ioevent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
  int inuse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
  int devpollfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
  int last_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
  int total_free;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
  int left_events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
  int max_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
  pollfd_t *pfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
} ioevent_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
static ioevent_t IOE_handles[MAX_HANDLES];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * Exceptions to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * Note : assuming all illegal argument and NULL pointer checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *        have already been done by the Java calling methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
static jint throwOutOfMemoryError(JNIEnv *env, const char * cause)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
  (*env)->ThrowNew(env, (*env)->FindClass(env,"java/lang/OutOfMemoryError"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                   cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
  return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
static jint throwInterruptedIOException(JNIEnv *env, const char * cause)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
  (*env)->ThrowNew(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                   (*env)->FindClass(env,"java/io/InterruptedIOException"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                   cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
  return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
static jint throwIllegalStateException(JNIEnv *env, const char * cause)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
  (*env)->ThrowNew(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                   (*env)->FindClass(env,"java/lang/IllegalStateException"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                   cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
  return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
5966
4cc8b71dfb19 6939022: Source code adjustments for parfait compilation
ohair
parents: 5506
diff changeset
   163
#define MEMORY_EXCEPTION(str) throwOutOfMemoryError(env, "Poller:" str)
4cc8b71dfb19 6939022: Source code adjustments for parfait compilation
ohair
parents: 5506
diff changeset
   164
#define STATE_EXCEPTION(str)  throwIllegalStateException(env, "Poller:" str)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
#define INTERRUPT_EXCEPTION(str) throwInterruptedIOException(env, \
5966
4cc8b71dfb19 6939022: Source code adjustments for parfait compilation
ohair
parents: 5506
diff changeset
   166
                                                             "Poller:" str)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
jint addfd(JNIEnv *, ioevent_t *, jint, jshort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
jint removefd(JNIEnv *, ioevent_t *, jint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * Class Poller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * Method: nativeInit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * Signature: ()I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * Only to be called once, right after this library is loaded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * so no need to deal with reentrancy here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * Could do as a pragma ini, but that isn't as portable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
JNIEXPORT jint JNICALL Java_Poller_nativeInit(JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
  int testdevpollfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
  int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
#ifdef DEVPOLL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
  /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
   * See if we can use this much faster method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
   * Note : must have fix for BUGID # 4223353 or OS can crash!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
  testdevpollfd = open("/dev/poll",O_RDWR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
  if (testdevpollfd >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * If Solaris 7, we need a patch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Until we know what string to search for, we'll play it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * safe and disable this for Solaris 7.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    if (!strcmp(name.release,"5.7"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        Use_devpoll = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        Use_devpoll = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
  DBGMSG(("Use_devpoll=%d\n" ,Use_devpoll));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
  close(testdevpollfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
  /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
   * For now, we optimize for Solaris 7 if /dev/poll isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
   * available, as it is only a small % hit for Solaris < 7.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
   * if ( (Use_devpoll == 0) && !strcmp(name.release,"5.6") )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
   *      Use_sol7opt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
  Current_handle = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
  for (i = 0; i < MAX_HANDLES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    IOE_handles[i].devpollfd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    IOE_handles[i].pfd = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
  /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
   * this tells me the max number of open filedescriptors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
  Max_index = sysconf(_SC_OPEN_MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
  if (Max_index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    Max_index = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
  DBGMSG(("got sysconf(_SC_OPEN_MAX)=%d file desc\n",Max_index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
  return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
JNIEXPORT jint JNICALL Java_Poller_getNumCPUs(JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
  return sysconf(_SC_NPROCESSORS_ONLN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * Class:     Poller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * Method:    nativeCreatePoller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 * Signature: (I)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 * Note : in the case where /dev/poll doesn't exist,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *        using more than one poll array could hurt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 *        Solaris 7 performance due to kernel caching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
JNIEXPORT jint JNICALL Java_Poller_nativeCreatePoller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
  (JNIEnv *env, jobject obj, jint maximum_fds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
  int handle, retval, i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
  ioevent_t *ioeh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
  if (maximum_fds == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    maximum_fds = Max_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
  handle = Current_handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
  if (Current_handle >= MAX_HANDLES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    for (i = 0; i < MAX_HANDLES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
      if (IOE_handles[i].inuse == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        handle = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    if (handle >= MAX_HANDLES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
      return MEMORY_EXCEPTION("CreatePoller - MAX_HANDLES exceeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    Current_handle++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
  ioeh = &IOE_handles[handle];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
  ioeh->inuse      = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
  ioeh->last_index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
  ioeh->total_free = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
  ioeh->left_events = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
  ioeh->max_index = maximum_fds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
  retval = handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
  if (Use_devpoll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    ioeh->devpollfd = open("/dev/poll",O_RDWR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    DBGMSG(("Opened /dev/poll, set devpollfd = %d\n",ioeh->devpollfd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    if (ioeh->devpollfd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
      Current_handle--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
      return MEMORY_EXCEPTION("CreatePoller - can\'t open /dev/poll");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
  ioeh->pfd = malloc(maximum_fds * sizeof(pollfd_t));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
  if (ioeh->pfd == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    Current_handle--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    return MEMORY_EXCEPTION("CreatePoller - malloc failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
  return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 * Class:     Poller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 * Method:    nativeDestroyPoller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 * Signature: (I)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
JNIEXPORT void JNICALL Java_Poller_nativeDestroyPoller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
  (JNIEnv *env, jobject obj, jint handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
  ioevent_t *ioeh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
  if (handle < 0 || handle > MAX_HANDLES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
      STATE_EXCEPTION("DestroyPoller - handle out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
  ioeh = &IOE_handles[handle];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
  ioeh->inuse = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
  if (Use_devpoll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    close(ioeh->devpollfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
  free(ioeh->pfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
static void check_handle(ioevent_t *ioeh)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
  int i,used,unused;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
  used=unused=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
  for (i = 0; i < ioeh->last_index; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
      if (ioeh->pfd[i].fd == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        unused++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
      else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        used++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
  if (unused != ioeh->total_free)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    printf("WARNING : found %d free, claimed %d.  Used : %d\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
           unused, ioeh->total_free, used);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
 * Class:     Poller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
 * Method:    nativeAddFd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
 * Signature: (IIS)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
 * Currently doesn't check to make sure we aren't adding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
 * an fd already added (no problem for /dev/poll...just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
 * an array waster for poll()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
JNIEXPORT jint JNICALL Java_Poller_nativeAddFd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
  (JNIEnv *env, jobject obj, jint handle, jint fd, jshort events)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
  int retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
  ioevent_t *ioeh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
  if (handle < 0 || handle > MAX_HANDLES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    return STATE_EXCEPTION("AddFd - handle out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
  ioeh = &IOE_handles[handle];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
  CHECK_HANDLE(ioeh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
  #ifdef DEVPOLL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
  if (Use_devpoll)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
      int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
      pollfd_t pollelt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
       * use /dev/poll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
      pollelt.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
      pollelt.events = events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
      if ((i = write(ioeh->devpollfd, &pollelt, sizeof(pollfd_t))) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
          sizeof(pollfd_t)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        DBGMSG(("write to devpollfd=%d showed %d bytes out of %d\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                ioeh->devpollfd,i,sizeof(pollfd_t)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return STATE_EXCEPTION("AddFd - /dev/poll add failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        retval = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
  else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
  #endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    { /* no /dev/poll available */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
      retval = addfd(env, ioeh, fd, events);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
  return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
 * Addfd to pollfd array...optimized for Solaris 7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
jint addfd(JNIEnv *env, ioevent_t *ioeh, jint fd, jshort events)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
  int idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
  if (ioeh->total_free)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
       * Traversing from end because that's where we pad.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
      ioeh->total_free--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
      for (idx = ioeh->last_index - 1; idx >= 0; idx--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (ioeh->pfd[idx].fd == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
  else if (ioeh->last_index >= ioeh->max_index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
      return MEMORY_EXCEPTION("AddFd - too many fds");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
  else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
      int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
      int new_total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
       * For Solaris 7, want to add some growth space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
       * and fill extras with fd=-1.  This allows for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
       * kernel poll() implementation to perform optimally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
      new_total = ioeh->last_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
      new_total += (new_total/10) + 1; /* bump size by 10% */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
      if (new_total > ioeh->max_index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        new_total = ioeh->max_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
      for (i = ioeh->last_index; i <= new_total; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
          ioeh->pfd[i].fd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
      idx = ioeh->last_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
      ioeh->total_free = new_total - ioeh->last_index - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
      DBGMSG(("Just grew from %d to %d in size\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
              ioeh->last_index, new_total));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
      ioeh->last_index = new_total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
  ASSERT((idx >= 0) && (idx <= ioeh->max_index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
  ASSERT(ioeh->pfd[idx].fd == -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
  ioeh->pfd[idx].fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
  ioeh->pfd[idx].events = events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
  ioeh->pfd[idx].revents = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
  CHECK_HANDLE(ioeh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
  return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
 * Class:     Poller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
 * Method:    nativeRemoveFd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
 * Signature: (II)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
JNIEXPORT jint JNICALL Java_Poller_nativeRemoveFd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
  (JNIEnv *env, jobject obj, jint handle, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
  ioevent_t *ioeh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
  if (handle < 0 || handle > MAX_HANDLES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    return STATE_EXCEPTION("RemoveFd - handle out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
  ioeh = &IOE_handles[handle];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
  #ifdef DEVPOLL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
  if (Use_devpoll)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
       * use /dev/poll - currently no need for locking here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
      pollfd_t pollelt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
      pollelt.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
      pollelt.events = POLLREMOVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
      if (write(ioeh->devpollfd, &pollelt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                sizeof(pollfd_t) ) != sizeof(pollfd_t))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
          return STATE_EXCEPTION("RemoveFd - /dev/poll failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
  else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
  #endif DEVPOLL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
      return removefd(env, ioeh,fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
 * remove from pollfd array...optimize for Solaris 7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
jint removefd(JNIEnv *env, ioevent_t *ioeh, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
  int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
  int found = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    { /* !Use_devpoll */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
      for (i = 0; i < ioeh->last_index; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
          if (ioeh->pfd[i].fd == fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
              ioeh->pfd[i].fd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
              found = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
      if (!found)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
          return STATE_EXCEPTION("RemoveFd - no such fd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
      ioeh->left_events = 0; /* Have to go back to the kernel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
      ioeh->total_free++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
       * Shrinking pool if > 33% empty. Just don't do this often!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
      if ( (ioeh->last_index > 100) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
           (ioeh->total_free > (ioeh->last_index / 3)) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
          int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
          /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
           * we'll just bite the bullet here, since we're > 33% empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
           * walk through and eliminate -1 fd values, shrink total
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
           * size to still have ~ 10 fd==-1 values at end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
           * Start at end (since we pad here) and, when we find fd != -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
           * swap with an earlier fd == -1 until we have all -1 values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
           * at the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
          CHECK_HANDLE(ioeh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
          for (i = ioeh->last_index - 1, j = 0; i > j; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
              if (ioeh->pfd[i].fd != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                  while ( (j < i) && (ioeh->pfd[j].fd != -1) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                  DBGMSG( ("i=%d,j=%d,ioeh->pfd[j].fd=%d\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                           i, j, ioeh->pfd[j].fd) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                  if (j < i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                      {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                        ASSERT(ioeh->pfd[j].fd == -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        ioeh->pfd[j].fd = ioeh->pfd[i].fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                        ioeh->pfd[j].events = ioeh->pfd[i].events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                        ioeh->pfd[i].fd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
          DBGMSG(("Just shrunk from %d to %d in size\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                  ioeh->last_index, j+11));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
          ioeh->last_index = j + 11; /* last_index always 1 greater */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
          ioeh->total_free = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
          CHECK_HANDLE(ioeh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    } /* !Use_devpoll */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
  return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
 * Class:     Poller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
 * Method:    nativeIsMember
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
 * Signature: (II)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
JNIEXPORT jint JNICALL Java_Poller_nativeIsMember
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
  (JNIEnv *env, jobject obj, jint handle, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
  int found = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
  int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
  ioevent_t *ioeh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
  if (handle < 0 || handle > MAX_HANDLES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    return STATE_EXCEPTION("IsMember - handle out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
  ioeh = &IOE_handles[handle];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
  #ifdef DEVPOLL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
  if (Use_devpoll)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
      pollfd_t pfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
       * DEVPOLL ioctl DP_ISPOLLED call to determine if fd is polled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
      pfd.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
      pfd.events = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
      pfd.revents = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
      found = ioctl(ioeh->devpollfd, DP_ISPOLLED, &pfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
      if (found == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
          return STATE_EXCEPTION("IsMember - /dev/poll failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
  else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
  #endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
      for (i = 0; i < ioeh->last_index; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
          if (fd == ioeh->pfd[i].fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
              found = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
  return found;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
 * Class:     Poller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
 * Method:    nativeWait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
 * Signature: (II[I[SJ)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
JNIEXPORT jint JNICALL Java_Poller_nativeWait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
  (JNIEnv *env, jobject obj, jint handle, jint maxEvents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
   jintArray jfds, jshortArray jrevents, jlong timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
  int useEvents, count, idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
  short *reventp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
  jint  *fdp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
  int   retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
  ioevent_t *ioeh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
  jboolean isCopy1,isCopy2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
  if (handle < 0 || handle > MAX_HANDLES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    return STATE_EXCEPTION("nativeWait - handle out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
  ioeh = &IOE_handles[handle];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
  if (maxEvents == 0) /* just doing a kernel delay! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
      useEvents = poll(NULL,0L,timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
      return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
  #ifdef DEVPOLL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
  if (Use_devpoll)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
      struct dvpoll dopoll;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
       * DEVPOLL ioctl DP_POLL call, reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
      dopoll.dp_timeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
      dopoll.dp_nfds=maxEvents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
      dopoll.dp_fds=ioeh->pfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
      useEvents = ioctl(ioeh->devpollfd, DP_POLL, &dopoll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
      while ((useEvents == -1) && (errno == EAGAIN))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            useEvents = ioctl(ioeh->devpollfd, DP_POLL, &dopoll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
      if (useEvents == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
          if (errno == EINTR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            return INTERRUPT_EXCEPTION("nativeWait - /dev/poll failure EINTR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
          else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            return STATE_EXCEPTION("nativeWait - /dev/poll failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
      reventp =(*env)->GetShortArrayElements(env,jrevents,&isCopy1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
      fdp =(*env)->GetIntArrayElements(env,jfds,&isCopy2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
      for (idx = 0,count = 0; idx < useEvents; idx++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
          if (ioeh->pfd[idx].revents)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
              fdp[count] = ioeh->pfd[idx].fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
              reventp[count] = ioeh->pfd[idx].revents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
              count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
      if (count < useEvents)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        return STATE_EXCEPTION("Wait - Corrupted internals");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
      if (isCopy1 == JNI_TRUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        (*env)->ReleaseShortArrayElements(env,jrevents,reventp,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
      if (isCopy2 == JNI_TRUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        (*env)->ReleaseIntArrayElements(env,jfds,fdp,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
  else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
  #endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    { /* !Use_devpoll */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /* no leftovers=>go to kernel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
      if (ioeh->left_events == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
          useEvents = poll(ioeh->pfd,ioeh->last_index, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
          while ((useEvents == -1) && (errno == EAGAIN))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            useEvents = poll(ioeh->pfd,ioeh->last_index, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
          if (useEvents == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
              if (errno == EINTR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                return INTERRUPT_EXCEPTION("Wait - poll() failure EINTR-" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                                           "IO interrupted.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
              else if (errno == EINVAL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                return STATE_EXCEPTION("Wait - poll() failure EINVAL-" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                                       "invalid args (is fdlim cur < max?)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
              else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                return STATE_EXCEPTION("Wait - poll() failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
          ioeh->left_events = useEvents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
          DBGMSG(("waitnative : poll returns : %d\n",useEvents));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
      else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        {  /* left over from last call */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
          useEvents = ioeh->left_events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
      if (useEvents > maxEvents)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
          useEvents = maxEvents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
      ioeh->left_events -= useEvents; /* left to process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
      DBGMSG(("waitnative : left %d, use %d, max %d\n",ioeh->left_events,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
              useEvents,maxEvents));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
      if (useEvents > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
          reventp =(*env)->GetShortArrayElements(env,jrevents,&isCopy1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
          fdp =(*env)->GetIntArrayElements(env,jfds,&isCopy2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
          for (idx = 0,count = 0; (idx < ioeh->last_index) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                 (count < useEvents); idx++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
              if (ioeh->pfd[idx].revents)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                  fdp[count] = ioeh->pfd[idx].fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                  reventp[count] = ioeh->pfd[idx].revents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                  /* in case of leftover for next walk */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                  ioeh->pfd[idx].revents = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                  count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
          if (count < useEvents)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
              ioeh->left_events = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
              return STATE_EXCEPTION("Wait - Corrupted internals");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
          if (isCopy1 == JNI_TRUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            (*env)->ReleaseShortArrayElements(env,jrevents,reventp,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
          if (isCopy2 == JNI_TRUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            (*env)->ReleaseIntArrayElements(env,jfds,fdp,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    } /* !Use_devpoll */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
  return useEvents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
}