jdk/src/demo/solaris/jni/Poller/Poller.java
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
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10292
diff changeset
     2
 * Copyright (c) 1999, 2011, 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
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
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: 5506
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: 5506
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: 5506
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: 5506
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * This class is provided for access to the underlying poll(2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * or /dev/poll kernel interfaces.  This may be needed for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * multiplexing IO when an application cannot afford to have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * a thread block on each outstanding IO request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * It currently supports the same basic functionality as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * C poll(2) API, although for efficiency we needed to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * passing the entire pollfd array for every call.  See man
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * pages for poll(2) for info on C API and event types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author  Bruce Chapman
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see     java.io.FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see     java.net.Socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see     attached README.txt
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 14342
diff changeset
    61
 * @since   1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class Poller {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
   * Solaris POLL event types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
  public final static short POLLERR  = 0x08;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
  public final static short POLLHUP  = 0x10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
  public final static short POLLNVAL = 0x20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
  public final static short POLLIN   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
  public final static short POLLPRI  = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
  public final static short POLLOUT  = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
  public final static short POLLRDNORM = 0x40;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
  public final static short POLLWRNORM = POLLOUT ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
  public final static short POLLRDBAND = 0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
  public final static short POLLWRBAND = 0x100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
  public final static short POLLNORM   = POLLRDNORM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
  /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
   * This global synchronization object must be used for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
   * creation or destruction of Poller objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
  private final static Object globalSync = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
  /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
   * The handle for a Poller Object...is used in the JNI C code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
   * where all the associated data is kept.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
  private int handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
   * Constructs an instance of a <code>Poller</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
   * Native code uses sysconf(_SC_OPEN_MAX) to determine how
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
   * many fd/skt objects this Poller object can contain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
  public Poller() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    synchronized(globalSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
      this.handle = nativeCreatePoller(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
   * Constructs an instance of a <code>Poller</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
   * @param  maxFd the maximum number of FileDescriptors/Sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
   *         this Poller object can contain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
  public Poller(int maxFd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    synchronized(globalSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
      this.handle = nativeCreatePoller(maxFd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
   * Needed to clean up at the JNI C level when object is GCd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
  protected void finalize() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    synchronized(globalSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
      nativeDestroyPoller(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
      super.finalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
   * Since we can't guarantee WHEN finalize is called, we may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
   * recycle on our own.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
   * @param  maxFd the maximum number of FileDescriptors/Sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
   *         this Poller object can contain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
  public void reset(int maxFd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    synchronized(globalSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
      nativeDestroyPoller(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
      this.handle = nativeCreatePoller(maxFd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
   * Since we can't guarantee WHEN finalize is called, we may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
   * recycle on our own.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
  public void reset() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    synchronized(globalSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
      nativeDestroyPoller(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
      this.handle = nativeCreatePoller(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
   * Add FileDescriptor to the set handled by this Poller object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
   * @param  fdObj the FileDescriptor, Socket, or ServerSocket to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
   * @param  event the bitmask of events we are interested in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
   * @return the OS level fd associated with this IO Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
   *          (which is what waitMultiple() stores in fds[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
  public synchronized int add(Object fdObj, short event) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    return nativeAddFd(handle,findfd(fdObj), event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
   * Remove FileDescriptor from the set handled by this Poller object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
   * Must be called before the fd/skt is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
   * @param fdObj the FileDescriptor, Socket, or ServerSocket to remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
   * @return true if removal succeeded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
  public synchronized boolean remove(Object fdObj) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    return (nativeRemoveFd(handle,findfd(fdObj)) == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
   * Check if fd or socket is already in the set handled by this Poller object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
   * @param fdObj the FileDescriptor or [Server]Socket to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
   * @return true if fd/skt is in the set for this Poller object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
  public synchronized boolean isMember(Object fdObj) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    return (nativeIsMember(handle,findfd(fdObj)) == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
   * Wait on Multiple IO Objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
   * @param maxRet    the maximum number of fds[] and revents[] to return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
   * @param fds[]     (return) an array of ints in which to store fds with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
   *                  available data upon a successful non-timeout return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
   *                  fds.length must be >= maxRet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
   * @param revents[] (return) the actual events available on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
   *                  same-indexed fds[] (i.e. fds[0] has events revents[0])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
   *                  revents.length must be >= maxRet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
   * Note : both above arrays are "dense," i.e. only fds[] with events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
   *        available are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
   * @param timeout   the maximum number of milliseconds to wait for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
   *                  events before timing out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
   * @return          the number of fds with triggered events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
   * Note : convenience methods exist for skipping the timeout parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
   *        or the maxRet parameter (in the case of no maxRet, fds.length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
   *        must equal revents.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
   * obj.waitMultiple(null,null,timeout) can be used for pausing the LWP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
   * (much more reliable and scalable than Thread.sleep() or Object.wait())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
  public synchronized int waitMultiple(int maxRet, int[] fds,short[] revents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                       long timeout) throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
      if ((revents == null) || (fds == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (maxRet > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
          throw new NullPointerException("fds or revents is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
      } else if ( (maxRet < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                  (maxRet > revents.length) || (maxRet > fds.length) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        throw new IllegalArgumentException("maxRet out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
      int ret = nativeWait(handle, maxRet, fds, revents, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
      if (ret < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        throw new InterruptedIOException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
      return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
   * Wait on Multiple IO Objects (no timeout).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
   * A convenience method for waiting indefinitely on IO events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
   * @see Poller#waitMultiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
  public int waitMultiple(int maxRet, int[] fds, short[] revents)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
      return waitMultiple(maxRet, fds, revents,-1L); // already synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
   * Wait on Multiple IO Objects (no maxRet).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
   * A convenience method for waiting on IO events when the fds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
   * and revents arrays are the same length and that specifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
   * maximum number of return events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
   * @see Poller#waitMultiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
  public synchronized int waitMultiple(int[] fds, short[] revents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                       long timeout) throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
      if ((revents == null) && (fds == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return nativeWait(handle,0,null,null,timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
      } else if ((revents == null) || (fds == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        throw new NullPointerException("revents or fds is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
      } else if (fds.length == revents.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return nativeWait(handle, fds.length, fds, revents, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
      throw new IllegalArgumentException("fds.length != revents.length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
   * Wait on Multiple IO Objects (no maxRet/timeout).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
   * A convenience method for waiting on IO events when the fds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
   * and revents arrays are the same length and that specifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
   * maximum number of return events, and when waiting indefinitely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
   * for IO events to occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
   * @see Poller#waitMultiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
  public int waitMultiple(int[] fds, short[] revents)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
      if ((revents == null) || (fds == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        throw new NullPointerException("fds or revents is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
      } else if (fds.length == revents.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return waitMultiple(revents.length,fds,revents,-1L); // already sync
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
      throw new IllegalArgumentException("fds.length != revents.length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
  // Utility - get (int) fd from FileDescriptor or [Server]Socket objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
  private int findfd(Object fdObj) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    Class cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    Field f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    Object val, implVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    if ((fdObj instanceof java.net.Socket) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        (fdObj instanceof java.net.ServerSocket)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
      cl = fdObj.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
      f = cl.getDeclaredField("impl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
      f.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
      val = f.get(fdObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
      cl = f.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
      f = cl.getDeclaredField("fd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
      f.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
      implVal = f.get(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
      cl = f.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
      f = cl.getDeclaredField("fd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
      f.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
      return  ((Integer) f.get(implVal)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    } else if ( fdObj instanceof java.io.FileDescriptor ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
      cl = fdObj.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
      f = cl.getDeclaredField("fd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
      f.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
      return  ((Integer) f.get(fdObj)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
      throw new IllegalArgumentException("Illegal Object type.");
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
  // Actual NATIVE calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
  private static native int  nativeInit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
  private native int  nativeCreatePoller(int maxFd) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
  private native void nativeDestroyPoller(int handle) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
  private native int  nativeAddFd(int handle, int fd, short events)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
  private native int  nativeRemoveFd(int handle, int fd) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
  private native int  nativeRemoveIndex(int handle, int index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
  private native int  nativeIsMember(int handle, int fd) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
  private native int  nativeWait(int handle, int maxRet, int[] fds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                                        short[] events, long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
   * Get number of active CPUs in this machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
   * to determine proper level of concurrency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
  public static native int  getNumCPUs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
  static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
      System.loadLibrary("poller");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
      nativeInit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
}