Remove most of atomix.utils.*
[controller.git] / third-party / atomix / utils / src / main / java / io / atomix / utils / concurrent / ThreadContext.java
1 /*
2  * Copyright 2015-present Open Networking Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.atomix.utils.concurrent;
17
18 import java.util.concurrent.Executor;
19
20 import static com.google.common.base.Preconditions.checkState;
21
22 /**
23  * Thread context.
24  * <p>
25  * The thread context is used by Catalyst to determine the correct thread on which to execute asynchronous callbacks.
26  * All threads created within Catalyst must be instances of {@link AtomixThread}. Once
27  * a thread has been created, the context is stored in the thread object via
28  * {@link AtomixThread#setContext(ThreadContext)}. This means there is a one-to-one relationship
29  * between a context and a thread. That is, a context is representative of a thread and provides an interface for firing
30  * events on that thread.
31  * <p>
32  * Components of the framework that provide custom threads should use {@link AtomixThreadFactory}
33  * to allocate new threads and provide a custom {@link ThreadContext} implementation.
34  *
35  * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
36  */
37 public interface ThreadContext extends AutoCloseable, Executor, Scheduler {
38
39   /**
40    * Returns the current thread context.
41    *
42    * @return The current thread context or {@code null} if no context exists.
43    */
44   static ThreadContext currentContext() {
45     Thread thread = Thread.currentThread();
46     return thread instanceof AtomixThread ? ((AtomixThread) thread).getContext() : null;
47   }
48
49   /**
50    * @throws IllegalStateException if the current thread is not a catalyst thread
51    */
52   static ThreadContext currentContextOrThrow() {
53     ThreadContext context = currentContext();
54     checkState(context != null, "not on a Catalyst thread");
55     return context;
56   }
57
58   /**
59    * Returns a boolean indicating whether the current thread is in this context.
60    *
61    * @return Indicates whether the current thread is in this context.
62    */
63   default boolean isCurrentContext() {
64     return currentContext() == this;
65   }
66
67   /**
68    * Checks that the current thread is the correct context thread.
69    */
70   default void checkThread() {
71     checkState(currentContext() == this, "not on a Catalyst thread");
72   }
73
74   /**
75    * Returns whether the thread context is currently marked blocked.
76    *
77    * @return whether the thread context is currently marked blocked
78    */
79   boolean isBlocked();
80
81   /**
82    * Marks the thread context as blocked.
83    */
84   void block();
85
86   /**
87    * Marks the thread context as unblocked.
88    */
89   void unblock();
90
91   /**
92    * Closes the context.
93    */
94   @Override
95   void close();
96
97 }