1 /*
   2  * Copyright (c) 2017, 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 package sun.nio.ch;
  26 
  27 import java.io.IOError;
  28 import java.io.IOException;
  29 import java.util.Map;
  30 import java.util.concurrent.ConcurrentHashMap;
  31 import java.util.concurrent.locks.LockSupport;
  32 
  33 import jdk.internal.misc.InnocuousThread;
  34 import jdk.internal.access.JavaLangAccess;
  35 import jdk.internal.access.SharedSecrets;
  36 
  37 abstract class Poller implements Runnable {
  38     private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
  39     private static final Poller READ_POLLER;
  40     private static final Poller WRITE_POLLER;
  41 
  42     static {
  43         try {
  44             READ_POLLER = startPollerThread("Read-Poller", PollerProvider.readPoller());
  45             WRITE_POLLER = startPollerThread("Write-Poller", PollerProvider.writePoller());
  46         } catch (IOException ioe) {
  47             throw new IOError(ioe);
  48         }
  49     }
  50 
  51     private static Poller startPollerThread(String name, Poller poller) {
  52         try {
  53             Thread t = JLA.executeOnCarrierThread(() ->
  54                     InnocuousThread.newSystemThread(name, poller));
  55             t.setDaemon(true);
  56             t.start();
  57             return poller;
  58         } catch (Exception e) {
  59             throw new InternalError(e);
  60         }
  61     }
  62 
  63     /**
  64      * Registers a strand to be unparked when a file descriptor is ready for I/O.
  65      *
  66      * @throws IllegalArgumentException if the event is not POLLIN or POLLOUT
  67      * @throws IllegalStateException if another strand is already registered
  68      *         to be unparked when the file descriptor is ready for this event
  69      */
  70     static void register(Object strand, int fdVal, int event) {
  71         if (event == Net.POLLIN) {
  72             READ_POLLER.register(strand, fdVal);
  73         } else if (event == Net.POLLOUT) {
  74             WRITE_POLLER.register(strand, fdVal);
  75         } else {
  76             throw new IllegalArgumentException("Unknown event " + event);
  77         }
  78     }
  79 
  80     /**
  81      * Deregister a strand so that it will not be unparked when a file descriptor
  82      * is ready for I/O. This method is a no-op if the strand is not registered.
  83      *
  84      * @throws IllegalArgumentException if the event is not POLLIN or POLLOUT
  85      */
  86     static void deregister(Object strand, int fdVal, int event) {
  87         if (event == Net.POLLIN) {
  88             READ_POLLER.deregister(strand, fdVal);
  89         } else if (event == Net.POLLOUT) {
  90             WRITE_POLLER.deregister(strand, fdVal);
  91         } else {
  92             throw new IllegalArgumentException("Unknown event " + event);
  93         }
  94     }
  95 
  96     /**
  97      * Stops polling the file descriptor for the given event and unpark any
  98      * strand registered to be unparked when the file descriptor is ready for I/O.
  99      */
 100     static void stopPoll(int fdVal, int event) {
 101         if (event == Net.POLLIN) {
 102             READ_POLLER.wakeup(fdVal);
 103         } else if (event == Net.POLLOUT) {
 104             WRITE_POLLER.wakeup(fdVal);
 105         } else {
 106             throw new IllegalArgumentException();
 107         }
 108     }
 109 
 110     /**
 111      * Stops polling the file descriptor and unpark any strands that are registered
 112      * to be unparked when the file descriptor is ready for I/O.
 113      */
 114     static void stopPoll(int fdVal) {
 115         stopPoll(fdVal, Net.POLLIN);
 116         stopPoll(fdVal, Net.POLLOUT);
 117     }
 118 
 119     private final Map<Integer, Object> map = new ConcurrentHashMap<>();
 120 
 121     protected Poller() { }
 122 
 123     private void register(Object strand, int fdVal) {
 124         Object previous = map.putIfAbsent(fdVal, strand);
 125         if (previous != null && previous != strand) {
 126             throw new IllegalStateException();
 127         }
 128         implRegister(fdVal);
 129     }
 130     private void deregister(Object strand, int fdVal) {
 131         if (map.remove(fdVal, strand)) {
 132             implDeregister(fdVal);
 133         }
 134     }
 135 
 136     private void wakeup(int fdVal) {
 137         Object strand = map.remove(fdVal);
 138         if (strand != null) {
 139             implDeregister(fdVal);
 140             LockSupport.unpark(strand);
 141         }
 142     }
 143 
 144     /**
 145      * Called by the polling facility when the file descriptor is polled
 146      */
 147     final protected void polled(int fdVal) {
 148         Object strand = map.remove(fdVal);
 149         if (strand != null) {
 150             LockSupport.unpark(strand);
 151         }
 152     }
 153 
 154     /**
 155      * Register the file descriptor
 156      */
 157     abstract protected void implRegister(int fdVal);
 158 
 159     /**
 160      * Deregister (or disarm) the file descriptor
 161      */
 162     abstract protected boolean implDeregister(int fdVal);
 163 }