1 /*
   2  * Copyright (c) 2004, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 8195160
  26  * @summary Check blocking of select and close
  27  * @requires (os.family == "linux")
  28  * @library .. /test/lib
  29  * @build RsocketTest
  30  * @run main/othervm -Djava.net.preferIPv4Stack=true SelectAndClose
  31  */
  32 
  33 import java.io.IOException;
  34 import java.lang.management.ManagementFactory;
  35 import java.lang.management.MonitorInfo;
  36 import java.lang.management.ThreadInfo;
  37 import java.nio.channels.Selector;
  38 import jdk.net.Sockets;
  39 
  40 public class SelectAndClose {
  41     static Selector selector;
  42     static volatile boolean awakened = false;
  43 
  44     private static boolean mightHoldLock(Thread t, Object lock) {
  45         long tid = t.getId();
  46         int hash = System.identityHashCode(lock);
  47         ThreadInfo ti = ManagementFactory.getThreadMXBean().
  48             getThreadInfo(new long[]{ tid} , true, false, 100)[0];
  49         if (ti != null) {
  50             for (MonitorInfo mi : ti.getLockedMonitors()) {
  51                 if (mi.getIdentityHashCode() == hash)
  52                     return true;
  53             }
  54         }
  55         return false;
  56     }
  57 
  58     public static void main(String[] args) throws Exception {
  59         if (!RsocketTest.isRsocketAvailable())
  60             return;
  61 
  62         selector = Sockets.openRdmaSelector();
  63 
  64         // Create and start a selector in a separate thread.
  65         Thread selectThread = new Thread(new Runnable() {
  66                 public void run() {
  67                     try {
  68                         selector.select();
  69                         awakened = true;
  70                     } catch (IOException e) {
  71                         e.printStackTrace();
  72                         throw new RuntimeException("Select did not awaken!");
  73                     }
  74                 }
  75             });
  76         selectThread.start();
  77 
  78         // Spin until the monitor of the selected-key set is likely held
  79         // as selected operations are specified to synchronize on the
  80         // selected-key set.
  81         while (!mightHoldLock(selectThread, selector.selectedKeys())) {
  82             Thread.sleep(50);
  83         }
  84 
  85         // Close the selector.
  86         selector.close();
  87 
  88         if (!awakened)
  89             selector.wakeup();
  90 
  91         // Wait for select() thread to finish.
  92         selectThread.join();
  93 
  94         if (!awakened) {
  95             throw new RuntimeException("Select did not awaken!");
  96         }
  97     }
  98 }