src/java.base/unix/classes/sun/nio/ch/PollArrayWrapper.java
changeset 49679 7084eec5c723
parent 49678 fa26e7c6efb7
parent 49520 7a64b48586d8
child 49680 2e681d678ec8
child 49708 6709f13dccd3
equal deleted inserted replaced
49678:fa26e7c6efb7 49679:7084eec5c723
     1 /*
       
     2  * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package sun.nio.ch;
       
    27 
       
    28 
       
    29 /**
       
    30  * Manipulates a native array of pollfd structs on Solaris:
       
    31  *
       
    32  * typedef struct pollfd {
       
    33  *    int fd;
       
    34  *    short events;
       
    35  *    short revents;
       
    36  * } pollfd_t;
       
    37  *
       
    38  * @author Mike McCloskey
       
    39  * @since 1.4
       
    40  */
       
    41 
       
    42 public class PollArrayWrapper extends AbstractPollArrayWrapper {
       
    43 
       
    44     // File descriptor to write for interrupt
       
    45     int interruptFD;
       
    46 
       
    47     PollArrayWrapper(int newSize) {
       
    48         newSize = (newSize + 1) * SIZE_POLLFD;
       
    49         pollArray = new AllocatedNativeObject(newSize, false);
       
    50         pollArrayAddress = pollArray.address();
       
    51         totalChannels = 1;
       
    52     }
       
    53 
       
    54     void initInterrupt(int fd0, int fd1) {
       
    55         interruptFD = fd1;
       
    56         putDescriptor(0, fd0);
       
    57         putEventOps(0, Net.POLLIN);
       
    58         putReventOps(0, 0);
       
    59     }
       
    60 
       
    61     void release(int i) {
       
    62         return;
       
    63     }
       
    64 
       
    65     void free() {
       
    66         pollArray.free();
       
    67     }
       
    68 
       
    69     /**
       
    70      * Prepare another pollfd struct for use.
       
    71      */
       
    72     void addEntry(SelChImpl sc) {
       
    73         putDescriptor(totalChannels, IOUtil.fdVal(sc.getFD()));
       
    74         putEventOps(totalChannels, 0);
       
    75         putReventOps(totalChannels, 0);
       
    76         totalChannels++;
       
    77     }
       
    78 
       
    79     /**
       
    80      * Writes the pollfd entry from the source wrapper at the source index
       
    81      * over the entry in the target wrapper at the target index. The source
       
    82      * array remains unchanged unless the source array and the target are
       
    83      * the same array.
       
    84      */
       
    85     static void replaceEntry(PollArrayWrapper source, int sindex,
       
    86                       PollArrayWrapper target, int tindex) {
       
    87         target.putDescriptor(tindex, source.getDescriptor(sindex));
       
    88         target.putEventOps(tindex, source.getEventOps(sindex));
       
    89         target.putReventOps(tindex, source.getReventOps(sindex));
       
    90     }
       
    91 
       
    92     /**
       
    93      * Grows the pollfd array to a size that will accommodate newSize
       
    94      * pollfd entries. This method does no checking of the newSize
       
    95      * to determine if it is in fact bigger than the old size: it
       
    96      * always reallocates an array of the new size.
       
    97      */
       
    98     void grow(int newSize) {
       
    99         // create new array
       
   100         PollArrayWrapper temp = new PollArrayWrapper(newSize);
       
   101 
       
   102         // Copy over existing entries
       
   103         for (int i=0; i<totalChannels; i++)
       
   104             replaceEntry(this, i, temp, i);
       
   105 
       
   106         // Swap new array into pollArray field
       
   107         pollArray.free();
       
   108         pollArray = temp.pollArray;
       
   109         pollArrayAddress = pollArray.address();
       
   110     }
       
   111 
       
   112     int poll(int numfds, int offset, long timeout) {
       
   113         return poll0(pollArrayAddress + (offset * SIZE_POLLFD),
       
   114                      numfds, timeout);
       
   115     }
       
   116 
       
   117     public void interrupt() {
       
   118         interrupt(interruptFD);
       
   119     }
       
   120 
       
   121     private native int poll0(long pollAddress, int numfds, long timeout);
       
   122 
       
   123     private static native void interrupt(int fd);
       
   124 
       
   125     static {
       
   126         IOUtil.load();
       
   127     }
       
   128 }