1 /*
  2  * Copyright (c) 2021, 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. Thread containers can be arranged in linked list.
 32  */
 33 public interface ThreadContainer {
 34 
 35     /**
 36      * Return the container name, null if not named.
 37      */
 38     default String name() {
 39         return null;
 40     }
 41 
 42     /**
 43      * Return the owner, null if not owned.
 44      */
 45     default Thread owner() {
 46         return null;
 47     }
 48 
 49     /**
 50      * Returns the parent of this container or null if this is the root container.
 51      */
 52     default ThreadContainer parent() {
 53         return ThreadContainers.parent(this);
 54     }
 55 
 56     /**
 57      * Return the stream of children of this container.
 58      */
 59     default Stream<ThreadContainer> children() {
 60         return ThreadContainers.children(this);
 61     }
 62     
 63     /**
 64      * Return a count of the number of threads in this container.
 65      */
 66     default long threadCount() {
 67         return threads().mapToLong(e -> 1L).sum();
 68     }
 69 
 70     /**
 71      * Returns a stream of the threads in this container.
 72      */
 73     Stream<Thread> threads();
 74 
 75     /**
 76      * Invoked when thread starts in the container.
 77      */
 78     default void onStart(Thread thread) { }
 79 
 80     /**
 81      * Invoked when thread in container terminates.
 82      */
 83     default void onExit(Thread thread) { }
 84 
 85     /**
 86      * The scope locals captured when the thread container was created.
 87      */
 88     default Object scopeLocalBindings() { return null; }
 89 
 90     /**
 91      * Returns the previous thread container in the list.
 92      */
 93     default ThreadContainer previous() {
 94         return null;
 95     }
 96 
 97     /**
 98      * Sets the previous thread container in the list.
 99      */
100     default void setPrevious(ThreadContainer container) {
101         throw new UnsupportedOperationException();
102     }
103 }