1 /*
 2  * Copyright (c) 2021, 2022, 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.vm;
27 
28 import java.util.stream.Stream;
29 
30 /**
31  * A container of threads.
32  */
33 public abstract class ThreadContainer extends StackableScope {
34 
35     /**
36      * Creates a ThreadContainer.
37      * @param shared true for a shared container, false for a container
38      * owned by the current thread
39      */
40     protected ThreadContainer(boolean shared) {
41         super(shared);
42     }
43 
44     /**
45      * Returns the parent of this container or null if this is the root container.
46      */
47     public ThreadContainer parent() {
48         return ThreadContainers.parent(this);
49     }
50 
51     /**
52      * Return the stream of children of this container.
53      */
54     public final Stream<ThreadContainer> children() {
55         return ThreadContainers.children(this);
56     }
57 
58     /**
59      * Return a count of the number of threads in this container.
60      */
61     public long threadCount() {
62         return threads().mapToLong(e -> 1L).sum();
63     }
64 
65     /**
66      * Returns a stream of the live threads in this container.
67      */
68     public abstract Stream<Thread> threads();
69 
70     /**
71      * Invoked by Thread::start before the given Thread is started.
72      */
73     public void onStart(Thread thread) {
74         // do nothing
75     }
76 
77     /**
78      * Invoked when a Thread terminates or starting it fails.
79      *
80      * For a platform thread, this method is invoked by the thread itself when it
81      * terminates. For a virtual thread, this method is invoked on its carrier
82      * after the virtual thread has terminated.
83      *
84      * If starting the Thread failed then this method is invoked on the thread
85      * that invoked onStart.
86      */
87     public void onExit(Thread thread) {
88         // do nothing
89     }
90 
91     /**
92      * The extent locals captured when the thread container was created.
93      */
94     public ExtentLocalContainer.BindingsSnapshot extentLocalBindings() {
95         return null;
96     }
97 }