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