1 /*
 2  * Copyright (c) 2019, 2024, 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  * Defines static methods to mark the beginning and end of a possibly blocking
33  * operation. The methods are intended to be used with try-finally as follows:
34  * {@snippet lang=java :
35  *     boolean attempted = Blocker.begin();
36  *     try {
37  *         // blocking operation
38  *     } finally {
39  *         Blocker.end(attempted);
40  *     }
41  * }
42  * If invoked from a virtual thread and the underlying carrier thread is a
43  * CarrierThread then the code in the block runs as if it were in run in
44  * ForkJoinPool.ManagedBlocker. This means the pool can be expanded to support
45  * additional parallelism during the blocking operation.
46  */
47 public class Blocker {
48     private static final JavaLangAccess JLA;
49     static {
50         JLA = SharedSecrets.getJavaLangAccess();
51         if (JLA == null) {
52             throw new InternalError("JavaLangAccess not setup");
53         }
54     }
55 
56     private Blocker() { }
57 
58     private static Thread currentCarrierThread() {
59         return JLA.currentCarrierThread();
60     }
61 
62     /**
63      * Marks the beginning of a blocking operation.
64      * @return true if tryCompensate attempted
65      */
66     public static boolean begin() {
67         if (VM.isBooted()
68                 && Thread.currentThread().isVirtual()
69                 && currentCarrierThread() instanceof CarrierThread ct) {
70             ct.beginBlocking();
71             return true;
72         }
73         return false;
74     }
75 
76     /**
77      * Marks the beginning of a possibly blocking operation.
78      * @param blocking true if the operation may block, otherwise false
79      * @return true if tryCompensate attempted
80      */
81     public static boolean begin(boolean blocking) {
82         return (blocking) ? begin() : false;
83     }
84 
85     /**
86      * Marks the end of an operation that may have blocked.
87      * @param attempted if tryCompensate attempted
88      */
89     public static void end(boolean attempted) {
90         if (attempted) {
91             CarrierThread ct = (CarrierThread) currentCarrierThread();
92             ct.endBlocking();
93         }
94     }
95 }