1 /*
   2  * Copyright (c) 2019, 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 jdk.internal.misc;
  27 
  28 import java.util.concurrent.ForkJoinPool;
  29 import java.util.concurrent.ForkJoinWorkerThread;
  30 
  31 /**
  32  * Supports the execution of tasks with a ManagedBlocker.
  33  */
  34 
  35 public class Blocker {
  36     private Blocker() { }
  37 
  38     private static final Unsafe U = Unsafe.getUnsafe();
  39 
  40     public interface BlockingRunnable<X extends Throwable> {
  41         void run() throws X;
  42     }
  43 
  44     public interface BlockingCallable<V, X extends Throwable> {
  45         V call() throws X;
  46     }
  47 
  48     /**
  49      * Runs the given task with a ManagedBlocker when invoked in the context of
  50      * a fiber and the carrier thread is a ForkJoinWorkerThread.
  51      */
  52     public static <X extends Throwable> void run(BlockingRunnable<X> task) throws X {
  53         BlockingCallable<Void, X> wrapper = () -> {
  54             task.run();
  55             return null;
  56         };
  57         run(wrapper);
  58     }
  59 
  60     /**
  61      * Runs the given task with a ManagedBlocker when invoked in the context of
  62      * a fiber and the carrier thread is a ForkJoinWorkerThread.
  63      */
  64     public static <V, X extends Throwable> V run(BlockingCallable<V, X> task) throws X {
  65         if (!(Strands.currentCarrierThread() instanceof ForkJoinWorkerThread)
  66                 || Fiber.current().isEmpty()) {
  67             return task.call();
  68         }
  69 
  70         var blocker = new ForkJoinPool.ManagedBlocker() {
  71             V result;
  72             Throwable exception;
  73             boolean done;
  74             @Override
  75             public boolean block() {
  76                 if (!done) {
  77                     try {
  78                         result = task.call();
  79                     } catch (Throwable e) {
  80                         exception = e;
  81                     } finally {
  82                         done = true;
  83                     }
  84                 }
  85                 return true;
  86             }
  87             @Override
  88             public boolean isReleasable() {
  89                 return done;
  90             }
  91         };
  92 
  93         try {
  94             ForkJoinPool.managedBlock(blocker);
  95         } catch (InterruptedException e) {
  96             U.throwException(e);
  97         }
  98 
  99         Throwable e = blocker.exception;
 100         if (e != null) {
 101             U.throwException(e);
 102         }
 103         return blocker.result;
 104     }
 105 }