View Javadoc
1   /*
2    * Copyright 2013 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    *   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, 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.netty.util.concurrent;
17  
18  import io.netty.util.internal.logging.InternalLogger;
19  import io.netty.util.internal.logging.InternalLoggerFactory;
20  
21  import java.util.ArrayDeque;
22  import java.util.Queue;
23  import java.util.concurrent.TimeUnit;
24  
25  /**
26   * Executes {@link Runnable} objects in the caller's thread. If the {@link #execute(Runnable)} is reentrant it will be
27   * queued until the original {@link Runnable} finishes execution.
28   * <p>
29   * All {@link Throwable} objects thrown from {@link #execute(Runnable)} will be swallowed and logged. This is to ensure
30   * that all queued {@link Runnable} objects have the chance to be run.
31   */
32  public final class ImmediateEventExecutor extends AbstractEventExecutor {
33      private static final InternalLogger logger = InternalLoggerFactory.getInstance(ImmediateEventExecutor.class);
34      public static final ImmediateEventExecutor INSTANCE = new ImmediateEventExecutor();
35      /**
36       * A Runnable will be queued if we are executing a Runnable. This is to prevent a {@link StackOverflowError}.
37       */
38      private static final FastThreadLocal<Queue<Runnable>> DELAYED_RUNNABLES = new FastThreadLocal<Queue<Runnable>>() {
39          @Override
40          protected Queue<Runnable> initialValue() throws Exception {
41              return new ArrayDeque<Runnable>();
42          }
43      };
44      /**
45       * Set to {@code true} if we are executing a runnable.
46       */
47      private static final FastThreadLocal<Boolean> RUNNING = new FastThreadLocal<Boolean>() {
48          @Override
49          protected Boolean initialValue() throws Exception {
50              return false;
51          }
52      };
53  
54      private final Future<?> terminationFuture = new FailedFuture<Object>(
55              GlobalEventExecutor.INSTANCE, new UnsupportedOperationException());
56  
57      private ImmediateEventExecutor() { }
58  
59      @Override
60      public boolean inEventLoop() {
61          return true;
62      }
63  
64      @Override
65      public boolean inEventLoop(Thread thread) {
66          return true;
67      }
68  
69      @Override
70      public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {
71          return terminationFuture();
72      }
73  
74      @Override
75      public Future<?> terminationFuture() {
76          return terminationFuture;
77      }
78  
79      @Override
80      @Deprecated
81      public void shutdown() { }
82  
83      @Override
84      public boolean isShuttingDown() {
85          return false;
86      }
87  
88      @Override
89      public boolean isShutdown() {
90          return false;
91      }
92  
93      @Override
94      public boolean isTerminated() {
95          return false;
96      }
97  
98      @Override
99      public boolean awaitTermination(long timeout, TimeUnit unit) {
100         return false;
101     }
102 
103     @Override
104     public void execute(Runnable command) {
105         if (command == null) {
106             throw new NullPointerException("command");
107         }
108         if (!RUNNING.get()) {
109             RUNNING.set(true);
110             try {
111                 command.run();
112             } catch (Throwable cause) {
113                 logger.info("Throwable caught while executing Runnable {}", command, cause);
114             } finally {
115                 Queue<Runnable> delayedRunnables = DELAYED_RUNNABLES.get();
116                 Runnable runnable;
117                 while ((runnable = delayedRunnables.poll()) != null) {
118                     try {
119                         runnable.run();
120                     } catch (Throwable cause) {
121                         logger.info("Throwable caught while executing Runnable {}", runnable, cause);
122                     }
123                 }
124                 RUNNING.set(false);
125             }
126         } else {
127             DELAYED_RUNNABLES.get().add(command);
128         }
129     }
130 
131     @Override
132     public <V> Promise<V> newPromise() {
133         return new ImmediatePromise<V>(this);
134     }
135 
136     @Override
137     public <V> ProgressivePromise<V> newProgressivePromise() {
138         return new ImmediateProgressivePromise<V>(this);
139     }
140 
141     static class ImmediatePromise<V> extends DefaultPromise<V> {
142         ImmediatePromise(EventExecutor executor) {
143             super(executor);
144         }
145 
146         @Override
147         protected void checkDeadLock() {
148             // No check
149         }
150     }
151 
152     static class ImmediateProgressivePromise<V> extends DefaultProgressivePromise<V> {
153         ImmediateProgressivePromise(EventExecutor executor) {
154             super(executor);
155         }
156 
157         @Override
158         protected void checkDeadLock() {
159             // No check
160         }
161     }
162 }