View Javadoc
1   /*
2    * Copyright 2019 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.util.internal;
17  
18  import io.netty5.util.concurrent.EventExecutor;
19  import io.netty5.util.concurrent.FastThreadLocal;
20  
21  import java.util.Objects;
22  import java.util.concurrent.Executor;
23  import java.util.concurrent.ThreadFactory;
24  
25  /**
26   * Allow to retrieve the {@link EventExecutor} for the calling {@link Thread}.
27   */
28  public final class ThreadExecutorMap {
29  
30      private static final FastThreadLocal<EventExecutor> mappings = new FastThreadLocal<>();
31  
32      private ThreadExecutorMap() { }
33  
34      /**
35       * Returns the current {@link EventExecutor} that uses the {@link Thread}, or {@code null} if none / unknown.
36       */
37      public static EventExecutor currentExecutor() {
38          return mappings.get();
39      }
40  
41      /**
42       * Set the current {@link EventExecutor} that is used by the {@link Thread}.
43       */
44      private static void setCurrentEventExecutor(EventExecutor executor) {
45          mappings.set(executor);
46      }
47  
48      /**
49       * Decorate the given {@link Executor} and ensure {@link #currentExecutor()} will return {@code eventExecutor}
50       * when called from within the {@link Runnable} during execution.
51       */
52      public static Executor apply(final Executor executor, final EventExecutor eventExecutor) {
53          Objects.requireNonNull(executor, "executor");
54          Objects.requireNonNull(eventExecutor, "eventExecutor");
55          return command -> executor.execute(apply(command, eventExecutor));
56      }
57  
58      /**
59       * Decorate the given {@link Runnable} and ensure {@link #currentExecutor()} will return {@code eventExecutor}
60       * when called from within the {@link Runnable} during execution.
61       */
62      public static Runnable apply(final Runnable command, final EventExecutor eventExecutor) {
63          Objects.requireNonNull(command, "command");
64          Objects.requireNonNull(eventExecutor, "eventExecutor");
65          return () -> {
66              setCurrentEventExecutor(eventExecutor);
67              try {
68                  command.run();
69              } finally {
70                  setCurrentEventExecutor(null);
71              }
72          };
73      }
74  
75      /**
76       * Decorate the given {@link ThreadFactory} and ensure {@link #currentExecutor()} will return {@code eventExecutor}
77       * when called from within the {@link Runnable} during execution.
78       */
79      public static ThreadFactory apply(final ThreadFactory threadFactory, final EventExecutor eventExecutor) {
80          Objects.requireNonNull(threadFactory, "command");
81          Objects.requireNonNull(eventExecutor, "eventExecutor");
82          return r -> threadFactory.newThread(apply(r, eventExecutor));
83      }
84  }