1 /*
   2  * Copyright (c) 2018, 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 jdk.internal.access.JavaLangAccess;
  29 import jdk.internal.access.SharedSecrets;
  30 
  31 /**
  32  * This class defines static methods to support thread-implementation agnostic
  33  * code in java.base and to support the execution of code in the context of
  34  * fibers.
  35  */
  36 
  37 public final class Strands {
  38     private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
  39     private Strands() { }
  40 
  41     /**
  42      * Returns the current strand.
  43      */
  44     public static Object currentStrand() {
  45         return JLA.currentStrand();
  46     }
  47 
  48     /**
  49      * Returns the current carrier thread.
  50      */
  51     public static Thread currentCarrierThread() {
  52         return JLA.currentCarrierThread();
  53     }
  54 
  55     /**
  56      * Interrupt the given strand.
  57      */
  58     public static void interrupt(Object strand) {
  59         JLA.interrupt(strand);
  60     }
  61 
  62     /**
  63      * Interrupt the current strand.
  64      */
  65     public static void interruptSelf() {
  66         interrupt(currentStrand());
  67     }
  68 
  69     /**
  70      * Tests whether the current strand has been interrupted. The <i>interrupted
  71      * status</i> of the strand is unaffected by this method.
  72      */
  73     public static boolean isInterrupted() {
  74         return JLA.isInterrupted();
  75     }
  76 
  77     /**
  78      * Tests whether the current strand has been interrupted.  The
  79      * <i>interrupted status</i> of the strand is cleared by this method.
  80      */
  81     public static boolean clearInterrupt() {
  82         return JLA.clearInterrupt();
  83     }
  84 
  85     /**
  86      * Disables the current fiber for scheduling purposes.
  87      */
  88     public static void parkFiber() {
  89         JLA.parkFiber();
  90     }
  91 
  92     /**
  93      * Disables the current fiber for scheduling purposes for up to the
  94      * given waiting time.
  95      */
  96     public static void parkFiber(long nanos) {
  97         JLA.parkFiber(nanos);
  98     }
  99 
 100     /**
 101      * Re-enables this fiber for scheduling.
 102      */
 103     public static void unparkFiber(Fiber<?> fiber) {
 104         JLA.unparkFiber(fiber);
 105     }
 106 
 107     /**
 108      * Returns the Fiber for the given shadow Thread object. Returns null if
 109      * the thread is not a shadow thread.
 110      */
 111     public static Fiber<?> getFiber(Thread thread) {
 112         return JLA.getFiber(thread);
 113     }
 114 
 115     /**
 116      * Returns the shadow thread for the given fiber or null if it does not
 117      * have a shadow thread.
 118      */
 119     public static Thread getShadowThread(Fiber<?> fiber) {
 120         return JLA.getShadowThread(fiber);
 121     }
 122 }