1 /*
   2  * Copyright 2001-2009 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package sun.nio.ch;
  27 
  28 import sun.misc.*;
  29 import java.io.IOException;
  30 import java.util.LinkedList;
  31 
  32 
  33 /**
  34  * Manipulates a native array of pollfd structs on Solaris:
  35  *
  36  * typedef struct pollfd {
  37  *    int fd;
  38  *    short events;
  39  *    short revents;
  40  * } pollfd_t;
  41  *
  42  * @author Mike McCloskey
  43  * @since 1.4
  44  */
  45 
  46 class DevPollArrayWrapper {
  47 
  48     // Event masks
  49     static final short POLLIN       = 0x0001;
  50     static final short POLLPRI      = 0x0002;
  51     static final short POLLOUT      = 0x0004;
  52     static final short POLLRDNORM   = 0x0040;
  53     static final short POLLWRNORM   = POLLOUT;
  54     static final short POLLRDBAND   = 0x0080;
  55     static final short POLLWRBAND   = 0x0100;
  56     static final short POLLNORM     = POLLRDNORM;
  57     static final short POLLERR      = 0x0008;
  58     static final short POLLHUP      = 0x0010;
  59     static final short POLLNVAL     = 0x0020;
  60     static final short POLLREMOVE   = 0x0800;
  61     static final short POLLCONN     = POLLOUT;
  62 
  63     // Miscellaneous constants
  64     static final short SIZE_POLLFD   = 8;
  65     static final short FD_OFFSET     = 0;
  66     static final short EVENT_OFFSET  = 4;
  67     static final short REVENT_OFFSET = 6;
  68 
  69     // Maximum number of open file descriptors
  70     static final int   OPEN_MAX      = fdLimit();
  71 
  72     // Number of pollfd structures to create.
  73     // DP_POLL ioctl allows up to OPEN_MAX-1
  74     static final int   NUM_POLLFDS   = Math.min(OPEN_MAX-1, 8192);
  75 
  76     // Base address of the native pollArray
  77     private long pollArrayAddress;
  78 
  79     // Array of pollfd structs used for driver updates
  80     private AllocatedNativeObject updatePollArray;
  81 
  82     // Maximum number of POLL_FD structs to update at once
  83     private int MAX_UPDATE_SIZE = Math.min(OPEN_MAX, 10000);
  84 
  85     DevPollArrayWrapper() {
  86         int allocationSize = NUM_POLLFDS * SIZE_POLLFD;
  87         pollArray = new AllocatedNativeObject(allocationSize, true);
  88         pollArrayAddress = pollArray.address();
  89         allocationSize = MAX_UPDATE_SIZE * SIZE_POLLFD;
  90         updatePollArray = new AllocatedNativeObject(allocationSize, true);
  91         wfd = init();
  92 
  93         for (int i=0; i<NUM_POLLFDS; i++) {
  94             putDescriptor(i, 0);
  95             putEventOps(i, 0);
  96             putReventOps(i, 0);
  97         }
  98     }
  99 
 100     // Machinery for remembering fd registration changes
 101     // A hashmap could be used but the number of changes pending
 102     // is expected to be small
 103     private static class Updator {
 104         int fd;
 105         int mask;
 106         Updator(int fd, int mask) {
 107             this.fd = fd;
 108             this.mask = mask;
 109         }
 110     }
 111     private LinkedList<Updator> updateList = new LinkedList<Updator>();
 112 
 113     // The pollfd array for results from devpoll driver
 114     private AllocatedNativeObject pollArray;
 115 
 116     // The fd of the devpoll driver
 117     int wfd;
 118 
 119     // The fd of the interrupt line going out
 120     int outgoingInterruptFD;
 121 
 122     // The fd of the interrupt line coming in
 123     int incomingInterruptFD;
 124 
 125     // The index of the interrupt FD
 126     int interruptedIndex;
 127 
 128     // Number of updated pollfd entries
 129     int updated;
 130 
 131     void initInterrupt(int fd0, int fd1) {
 132         outgoingInterruptFD = fd1;
 133         incomingInterruptFD = fd0;
 134         register(wfd, fd0, POLLIN);
 135     }
 136 
 137     void putEventOps(int i, int event) {
 138         int offset = SIZE_POLLFD * i + EVENT_OFFSET;
 139         pollArray.putShort(offset, (short)event);
 140     }
 141 
 142     void putReventOps(int i, int revent) {
 143         int offset = SIZE_POLLFD * i + REVENT_OFFSET;
 144         pollArray.putShort(offset, (short)revent);
 145     }
 146 
 147     void putDescriptor(int i, int fd) {
 148         int offset = SIZE_POLLFD * i + FD_OFFSET;
 149         pollArray.putInt(offset, fd);
 150     }
 151 
 152     int getEventOps(int i) {
 153         int offset = SIZE_POLLFD * i + EVENT_OFFSET;
 154         return pollArray.getShort(offset);
 155     }
 156 
 157     int getReventOps(int i) {
 158         int offset = SIZE_POLLFD * i + REVENT_OFFSET;
 159         return pollArray.getShort(offset);
 160     }
 161 
 162     int getDescriptor(int i) {
 163         int offset = SIZE_POLLFD * i + FD_OFFSET;
 164         return pollArray.getInt(offset);
 165     }
 166 
 167     void setInterest(int fd, int mask) {
 168         synchronized (updateList) {
 169             updateList.add(new Updator(fd, mask));
 170         }
 171     }
 172 
 173     void release(int fd) {
 174         synchronized (updateList) {
 175             updateList.add(new Updator(fd, POLLREMOVE));
 176         }
 177     }
 178 
 179     void closeDevPollFD() throws IOException {
 180         FileDispatcherImpl.closeIntFD(wfd);
 181         pollArray.free();
 182         updatePollArray.free();
 183     }
 184 
 185     int poll(long timeout) throws IOException {
 186         updateRegistrations();
 187         updated = poll0(pollArrayAddress, NUM_POLLFDS, timeout, wfd);
 188         for (int i=0; i<updated; i++) {
 189             if (getDescriptor(i) == incomingInterruptFD) {
 190                 interruptedIndex = i;
 191                 interrupted = true;
 192                 break;
 193             }
 194         }
 195         return updated;
 196     }
 197 
 198     void updateRegistrations() throws IOException {
 199         // Populate pollfd array with updated masks
 200         synchronized (updateList) {
 201             while (updateList.size() > 0) {
 202                 // We have to insert a dummy node in between each
 203                 // real update to use POLLREMOVE on the fd first because
 204                 // otherwise the changes are simply OR'd together
 205                 int index = 0;
 206                 Updator u = null;
 207                 while ((u = updateList.poll()) != null) {
 208                     // First add pollfd struct to clear out this fd
 209                     putPollFD(updatePollArray, index, u.fd, POLLREMOVE);
 210                     index++;
 211                     // Now add pollfd to update this fd, if necessary
 212                     if (u.mask != POLLREMOVE) {
 213                         putPollFD(updatePollArray, index, u.fd, (short)u.mask);
 214                         index++;
 215                     }
 216 
 217                     // Check against the max update size; these are
 218                     // all we will process. Valid index ranges from 0 to
 219                     // (MAX_UPDATE_SIZE - 1) and we can use up to 2 per loop
 220                     if (index >  MAX_UPDATE_SIZE - 2)
 221                         break;
 222                 }
 223                 // Register the changes with /dev/poll
 224                 registerMultiple(wfd, updatePollArray.address(), index);
 225              }
 226         }
 227     }
 228 
 229     private void putPollFD(AllocatedNativeObject array, int index, int fd,
 230                            short event)
 231     {
 232         int structIndex = SIZE_POLLFD * index;
 233         array.putInt(structIndex + FD_OFFSET, fd);
 234         array.putShort(structIndex + EVENT_OFFSET, event);
 235         array.putShort(structIndex + REVENT_OFFSET, (short)0);
 236     }
 237 
 238     boolean interrupted = false;
 239 
 240     public void interrupt() {
 241         interrupt(outgoingInterruptFD);
 242     }
 243 
 244     public int interruptedIndex() {
 245         return interruptedIndex;
 246     }
 247 
 248     boolean interrupted() {
 249         return interrupted;
 250     }
 251 
 252     void clearInterrupted() {
 253         interrupted = false;
 254     }
 255 
 256     private native int init();
 257     private native void register(int wfd, int fd, int mask);
 258     private native void registerMultiple(int wfd, long address, int len)
 259         throws IOException;
 260     private native int poll0(long pollAddress, int numfds, long timeout,
 261                              int wfd);
 262     private static native void interrupt(int fd);
 263     private static native int fdLimit();
 264 
 265 }