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.
   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 /*
  25  * @test
  26  * @bug 8195160
  27  * @summary Test that RDMA channels can be registered, interest ops can changed,
  28  *          and keys cancelled while a selection operation is in progress.
  29  * @requires (os.family == "linux")
  30  * @library .. /test/lib
  31  * @build RsocketTest
  32  * @run main/othervm -Djava.net.preferIPv4Stack=true BasicAccept
  33  */
  34 import java.io.IOException;
  35 import java.nio.channels.Pipe;
  36 import java.nio.channels.SelectionKey;
  37 import java.nio.channels.Selector;
  38 import java.util.concurrent.Callable;
  39 import java.util.concurrent.ExecutorService;
  40 import java.util.concurrent.Executors;
  41 import java.util.concurrent.Future;
  42 import java.util.concurrent.Phaser;
  43 import jdk.net.Sockets;
  44 
  45 public class RegisterDuringSelect {
  46 
  47     static Callable<Void> selectLoop(Selector sel, Phaser barrier) {
  48         return new Callable<Void>() {
  49             @Override
  50             public Void call() throws IOException {
  51                 for (;;) {
  52                     sel.select();
  53                     if (sel.isOpen()) {
  54                         barrier.arriveAndAwaitAdvance();
  55                     } else {
  56                         // closed
  57                         return null;
  58                     }
  59                 }
  60             }
  61         };
  62     }
  63     /**
  64      * Invoke register, interestOps, and cancel concurrently with a thread
  65      * doing a selection operation
  66      */
  67     public static void main(String args[]) throws Exception {
  68         if (!RsocketTest.isRsocketAvailable())
  69             return;
  70 
  71         Future<Void> result;
  72 
  73         ExecutorService pool = Executors.newFixedThreadPool(1);
  74         try (Selector sel = Sockets.openRdmaSelector()) {
  75             Phaser barrier = new Phaser(2);
  76 
  77             // submit task to do the select loop
  78             result = pool.submit(selectLoop(sel, barrier));
  79 
  80             Pipe p = Pipe.open();
  81             try {
  82                 Pipe.SourceChannel sc = p.source();
  83                 sc.configureBlocking(false);
  84 
  85                 System.out.println("register ...");
  86                 SelectionKey key = sc.register(sel, SelectionKey.OP_READ);
  87                 if (!sel.keys().contains(key))
  88                     throw new RuntimeException("key not in key set");
  89                 sel.wakeup();
  90                 barrier.arriveAndAwaitAdvance();
  91 
  92                 System.out.println("interestOps ...");
  93                 key.interestOps(0);
  94                 sel.wakeup();
  95                 barrier.arriveAndAwaitAdvance();
  96 
  97                 System.out.println("cancel ...");
  98                 key.cancel();
  99                 sel.wakeup();
 100                 barrier.arriveAndAwaitAdvance();
 101                 if (sel.keys().contains(key))
 102                     throw new RuntimeException("key not removed from key set");
 103 
 104             } finally {
 105                 p.source().close();
 106                 p.sink().close();
 107             }
 108 
 109         } finally {
 110             pool.shutdown();
 111         }
 112 
 113         // ensure selectLoop completes without exception
 114         result.get();
 115 
 116     }
 117 }