1 /*
   2  * Copyright (c) 2011, 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 /*
  27  * KQueueArrayWrapper.java
  28  * Implementation of Selector using FreeBSD / Mac OS X kqueues
  29  * Derived from Sun's DevPollArrayWrapper
  30  */
  31 
  32 package sun.nio.ch;
  33 
  34 import java.io.IOException;
  35 import java.util.Iterator;
  36 import java.util.LinkedList;
  37 import sun.security.action.GetPropertyAction;
  38 
  39 /*
  40  * struct kevent {           // 32-bit    64-bit
  41  *     uintptr_t ident;      //   4         8
  42  *     short     filter;     //   2         2
  43  *     u_short   flags;      //   2         2
  44  *     u_int     fflags;     //   4         4
  45  *     intptr_t  data;       //   4         8
  46  *     void      *udata;     //   4         8
  47  * }                  // Total:  20        32
  48  *
  49  * The implementation works in 32-bit and 64-bit world. We do this by calling a
  50  * native function that actually sets the sizes and offsets of the fields based
  51  * on which mode we're in.
  52  */
  53 
  54 class KQueueArrayWrapper {
  55     // kevent filters
  56     static short EVFILT_READ;
  57     static short EVFILT_WRITE;
  58 
  59     // kevent struct
  60     // These fields are now set by initStructSizes in the static initializer.
  61     static short SIZEOF_KEVENT;
  62     static short FD_OFFSET;
  63     static short FILTER_OFFSET;
  64 
  65     // kevent array size
  66     static final int NUM_KEVENTS = 128;
  67 
  68     // Are we in a 64-bit VM?
  69     static boolean is64bit = false;
  70 
  71     // The kevent array (used for outcoming events only)
  72     private AllocatedNativeObject keventArray = null;
  73     private long keventArrayAddress;
  74 
  75     // The kqueue fd
  76     private int kq = -1;
  77 
  78     // The fd of the interrupt line going out
  79     private int outgoingInterruptFD;
  80 
  81     // The fd of the interrupt line coming in
  82     private int incomingInterruptFD;
  83 
  84     static {
  85         IOUtil.load();
  86         initStructSizes();
  87         String datamodel = GetPropertyAction.getProperty("sun.arch.data.model");
  88         is64bit = "64".equals(datamodel);
  89     }
  90 
  91     KQueueArrayWrapper() {
  92         int allocationSize = SIZEOF_KEVENT * NUM_KEVENTS;
  93         keventArray = new AllocatedNativeObject(allocationSize, true);
  94         keventArrayAddress = keventArray.address();
  95         kq = init();
  96     }
  97 
  98     // Used to update file description registrations
  99     private static class Update {
 100         SelChImpl channel;
 101         int events;
 102         Update(SelChImpl channel, int events) {
 103             this.channel = channel;
 104             this.events = events;
 105         }
 106     }
 107 
 108     private LinkedList<Update> updateList = new LinkedList<Update>();
 109 
 110     void initInterrupt(int fd0, int fd1) {
 111         outgoingInterruptFD = fd1;
 112         incomingInterruptFD = fd0;
 113         register0(kq, fd0, 1, 0);
 114     }
 115 
 116     int getReventOps(int index) {
 117         int result = 0;
 118         int offset = SIZEOF_KEVENT*index + FILTER_OFFSET;
 119         short filter = keventArray.getShort(offset);
 120 
 121         // This is all that's necessary based on inspection of usage:
 122         //   SinkChannelImpl, SourceChannelImpl, DatagramChannelImpl,
 123         //   ServerSocketChannelImpl, SocketChannelImpl
 124         if (filter == EVFILT_READ) {
 125             result |= Net.POLLIN;
 126         } else if (filter == EVFILT_WRITE) {
 127             result |= Net.POLLOUT;
 128         }
 129 
 130         return result;
 131     }
 132 
 133     int getDescriptor(int index) {
 134         int offset = SIZEOF_KEVENT*index + FD_OFFSET;
 135         /* The ident field is 8 bytes in 64-bit world, however the API wants us
 136          * to return an int. Hence read the 8 bytes but return as an int.
 137          */
 138         if (is64bit) {
 139           long fd = keventArray.getLong(offset);
 140           assert fd <= Integer.MAX_VALUE;
 141           return (int) fd;
 142         } else {
 143           return keventArray.getInt(offset);
 144         }
 145     }
 146 
 147     void setInterest(SelChImpl channel, int events) {
 148         synchronized (updateList) {
 149             // update existing registration
 150             updateList.add(new Update(channel, events));
 151         }
 152     }
 153 
 154     void release(SelChImpl channel) {
 155         synchronized (updateList) {
 156             // flush any pending updates
 157             for (Iterator<Update> it = updateList.iterator(); it.hasNext();) {
 158                 if (it.next().channel == channel) {
 159                     it.remove();
 160                 }
 161             }
 162 
 163             // remove
 164             register0(kq, channel.getFDVal(), 0, 0);
 165         }
 166     }
 167 
 168     void updateRegistrations() {
 169         synchronized (updateList) {
 170             Update u = null;
 171             while ((u = updateList.poll()) != null) {
 172                 SelChImpl ch = u.channel;
 173                 if (!ch.isOpen())
 174                     continue;
 175 
 176                 register0(kq, ch.getFDVal(), u.events & Net.POLLIN, u.events & Net.POLLOUT);
 177             }
 178         }
 179     }
 180 
 181 
 182     void close() throws IOException {
 183         if (keventArray != null) {
 184             keventArray.free();
 185             keventArray = null;
 186         }
 187         if (kq >= 0) {
 188             FileDispatcherImpl.closeIntFD(kq);
 189             kq = -1;
 190         }
 191     }
 192 
 193     int poll(long timeout) {
 194         updateRegistrations();
 195         int updated = kevent0(kq, keventArrayAddress, NUM_KEVENTS, timeout);
 196         return updated;
 197     }
 198 
 199     void interrupt() {
 200         interrupt(outgoingInterruptFD);
 201     }
 202 
 203     private native int init();
 204     private static native void initStructSizes();
 205 
 206     private native void register0(int kq, int fd, int read, int write);
 207     private native int kevent0(int kq, long keventAddress, int keventCount,
 208                                long timeout);
 209     private static native void interrupt(int fd);
 210 }