1 /*
   2  * Copyright (c) 2018, 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 import java.io.IOException;
  29 
  30 import jdk.internal.misc.Unsafe;
  31 import static sun.nio.ch.SolarisEventPort.*;
  32 
  33 /**
  34  * Poller implementation based on the Solaris ports facility.
  35  */
  36 
  37 class PortPoller extends Poller {
  38 
  39     private static final int addressSize = Unsafe.getUnsafe().addressSize();
  40 
  41     private static final int OPEN_MAX = IOUtil.fdLimit();
  42     private static final int POLL_MAX = Math.min(OPEN_MAX-1, 512);
  43 
  44     private final int pfd;
  45     private final int event;
  46     private final long pollArrayAddress;
  47     private final AllocatedNativeObject pollArray;
  48 
  49     PortPoller(int event) throws IOException {
  50         this.pfd = port_create();
  51         this.event = event;
  52 
  53         int allocationSize = POLL_MAX * SIZEOF_PORT_EVENT;
  54         this.pollArray = new AllocatedNativeObject(allocationSize, true);
  55         this.pollArrayAddress = pollArray.address();
  56     }
  57 
  58     @Override
  59     protected void implRegister(int fdVal) {
  60         try {
  61             port_associate(pfd, PORT_SOURCE_FD, (long)fdVal, event);
  62         } catch (IOException ioe) {
  63             throw new InternalError("port_associate failed", ioe);
  64         }
  65     }
  66 
  67     @Override
  68     protected boolean implDeregister(int fdVal) {
  69         try {
  70             return port_dissociate(pfd, PORT_SOURCE_FD, (long)fdVal);
  71         } catch (IOException ioe) {
  72             throw new InternalError("port_dissociate failed", ioe);
  73         }
  74     }
  75 
  76     @Override
  77     public void run() {
  78         try {
  79             for (;;) {
  80                 int updated = port_getn(pfd, pollArrayAddress, POLL_MAX, -1L);
  81                 for (int i=0; i<updated; i++) {
  82                     if (getSource(i) == PORT_SOURCE_FD) {
  83                         int fdVal = getDescriptor(i);
  84                         polled(fdVal);
  85                     }
  86                 }
  87             }
  88         } catch (Throwable t) {
  89             t.printStackTrace();
  90         }
  91     }
  92 
  93     private short getSource(int i) {
  94         int offset = SIZEOF_PORT_EVENT * i + OFFSETOF_SOURCE;
  95         return pollArray.getShort(offset);
  96     }
  97 
  98     private int getDescriptor(int i) {
  99         int offset = SIZEOF_PORT_EVENT * i + OFFSETOF_OBJECT;
 100         if (addressSize == 4) {
 101             return pollArray.getInt(offset);
 102         } else {
 103             return (int) pollArray.getLong(offset);
 104         }
 105     }
 106 }