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