View Javadoc
1   /*
2    * Copyright 2012 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.netty.util.concurrent;
17  
18  import io.netty.util.internal.ObjectUtil;
19  import io.netty.util.internal.SystemPropertyUtil;
20  import io.netty.util.internal.ThreadExecutorMap;
21  import io.netty.util.internal.ThrowableUtil;
22  import io.netty.util.internal.logging.InternalLogger;
23  import io.netty.util.internal.logging.InternalLoggerFactory;
24  
25  import org.jetbrains.annotations.Async.Schedule;
26  
27  import java.security.AccessController;
28  import java.security.PrivilegedAction;
29  import java.util.Queue;
30  import java.util.concurrent.BlockingQueue;
31  import java.util.concurrent.Executors;
32  import java.util.concurrent.LinkedBlockingQueue;
33  import java.util.concurrent.RejectedExecutionException;
34  import java.util.concurrent.ThreadFactory;
35  import java.util.concurrent.TimeUnit;
36  import java.util.concurrent.atomic.AtomicBoolean;
37  
38  /**
39   * Single-thread singleton {@link EventExecutor}.  It starts the thread automatically and stops it when there is no
40   * task pending in the task queue for {@code io.netty.globalEventExecutor.quietPeriodSeconds} second
41   * (default is 1 second).  Please note it is not scalable to schedule large number of tasks to this executor;
42   * use a dedicated executor.
43   */
44  public final class GlobalEventExecutor extends AbstractScheduledEventExecutor implements OrderedEventExecutor {
45      private static final InternalLogger logger = InternalLoggerFactory.getInstance(GlobalEventExecutor.class);
46  
47      private static final long SCHEDULE_QUIET_PERIOD_INTERVAL;
48  
49      static {
50          int quietPeriod = SystemPropertyUtil.getInt("io.netty.globalEventExecutor.quietPeriodSeconds", 1);
51          if (quietPeriod <= 0) {
52              quietPeriod = 1;
53          }
54          logger.debug("-Dio.netty.globalEventExecutor.quietPeriodSeconds: {}", quietPeriod);
55  
56          SCHEDULE_QUIET_PERIOD_INTERVAL = TimeUnit.SECONDS.toNanos(quietPeriod);
57      }
58  
59      public static final GlobalEventExecutor INSTANCE = new GlobalEventExecutor();
60  
61      final BlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<Runnable>();
62      final ScheduledFutureTask<Void> quietPeriodTask = new ScheduledFutureTask<Void>(
63              this, Executors.<Void>callable(new Runnable() {
64          @Override
65          public void run() {
66              // NOOP
67          }
68      }, null),
69              // note: the getCurrentTimeNanos() call here only works because this is a final class, otherwise the method
70              // could be overridden leading to unsafe initialization here!
71              deadlineNanos(getCurrentTimeNanos(), SCHEDULE_QUIET_PERIOD_INTERVAL),
72              -SCHEDULE_QUIET_PERIOD_INTERVAL
73      );
74  
75      // because the GlobalEventExecutor is a singleton, tasks submitted to it can come from arbitrary threads and this
76      // can trigger the creation of a thread from arbitrary thread groups; for this reason, the thread factory must not
77      // be sticky about its thread group
78      // visible for testing
79      final ThreadFactory threadFactory;
80      private final TaskRunner taskRunner = new TaskRunner();
81      private final AtomicBoolean started = new AtomicBoolean();
82      volatile Thread thread;
83  
84      private final Future<?> terminationFuture;
85  
86      private GlobalEventExecutor() {
87          scheduledTaskQueue().add(quietPeriodTask);
88          threadFactory = ThreadExecutorMap.apply(new DefaultThreadFactory(
89                  DefaultThreadFactory.toPoolName(getClass()), false, Thread.NORM_PRIORITY, null), this);
90  
91          UnsupportedOperationException terminationFailure = new UnsupportedOperationException();
92          ThrowableUtil.unknownStackTrace(terminationFailure, GlobalEventExecutor.class, "terminationFuture");
93          terminationFuture = new FailedFuture<Object>(this, terminationFailure);
94      }
95  
96      /**
97       * Take the next {@link Runnable} from the task queue and so will block if no task is currently present.
98       *
99       * @return {@code null} if the executor thread has been interrupted or waken up.
100      */
101     Runnable takeTask() {
102         BlockingQueue<Runnable> taskQueue = this.taskQueue;
103         for (;;) {
104             ScheduledFutureTask<?> scheduledTask = peekScheduledTask();
105             if (scheduledTask == null) {
106                 Runnable task = null;
107                 try {
108                     task = taskQueue.take();
109                 } catch (InterruptedException e) {
110                     // Ignore
111                 }
112                 return task;
113             } else {
114                 long delayNanos = scheduledTask.delayNanos();
115                 Runnable task = null;
116                 if (delayNanos > 0) {
117                     try {
118                         task = taskQueue.poll(delayNanos, TimeUnit.NANOSECONDS);
119                     } catch (InterruptedException e) {
120                         // Waken up.
121                         return null;
122                     }
123                 }
124                 if (task == null) {
125                     // We need to fetch the scheduled tasks now as otherwise there may be a chance that
126                     // scheduled tasks are never executed if there is always one task in the taskQueue.
127                     // This is for example true for the read task of OIO Transport
128                     // See https://github.com/netty/netty/issues/1614
129                     fetchFromScheduledTaskQueue();
130                     task = taskQueue.poll();
131                 }
132 
133                 if (task != null) {
134                     return task;
135                 }
136             }
137         }
138     }
139 
140     private void fetchFromScheduledTaskQueue() {
141         long nanoTime = getCurrentTimeNanos();
142         Runnable scheduledTask = pollScheduledTask(nanoTime);
143         while (scheduledTask != null) {
144             taskQueue.add(scheduledTask);
145             scheduledTask = pollScheduledTask(nanoTime);
146         }
147     }
148 
149     /**
150      * Return the number of tasks that are pending for processing.
151      */
152     public int pendingTasks() {
153         return taskQueue.size();
154     }
155 
156     /**
157      * Add a task to the task queue, or throws a {@link RejectedExecutionException} if this instance was shutdown
158      * before.
159      */
160     private void addTask(Runnable task) {
161         taskQueue.add(ObjectUtil.checkNotNull(task, "task"));
162     }
163 
164     @Override
165     public boolean inEventLoop(Thread thread) {
166         return thread == this.thread;
167     }
168 
169     @Override
170     public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {
171         return terminationFuture();
172     }
173 
174     @Override
175     public Future<?> terminationFuture() {
176         return terminationFuture;
177     }
178 
179     @Override
180     @Deprecated
181     public void shutdown() {
182         throw new UnsupportedOperationException();
183     }
184 
185     @Override
186     public boolean isShuttingDown() {
187         return false;
188     }
189 
190     @Override
191     public boolean isShutdown() {
192         return false;
193     }
194 
195     @Override
196     public boolean isTerminated() {
197         return false;
198     }
199 
200     @Override
201     public boolean awaitTermination(long timeout, TimeUnit unit) {
202         return false;
203     }
204 
205     /**
206      * Waits until the worker thread of this executor has no tasks left in its task queue and terminates itself.
207      * Because a new worker thread will be started again when a new task is submitted, this operation is only useful
208      * when you want to ensure that the worker thread is terminated <strong>after</strong> your application is shut
209      * down and there's no chance of submitting a new task afterwards.
210      *
211      * @return {@code true} if and only if the worker thread has been terminated
212      */
213     public boolean awaitInactivity(long timeout, TimeUnit unit) throws InterruptedException {
214         ObjectUtil.checkNotNull(unit, "unit");
215 
216         final Thread thread = this.thread;
217         if (thread == null) {
218             throw new IllegalStateException("thread was not started");
219         }
220         thread.join(unit.toMillis(timeout));
221         return !thread.isAlive();
222     }
223 
224     @Override
225     public void execute(Runnable task) {
226         execute0(task);
227     }
228 
229     private void execute0(@Schedule Runnable task) {
230         addTask(ObjectUtil.checkNotNull(task, "task"));
231         if (!inEventLoop()) {
232             startThread();
233         }
234     }
235 
236     private void startThread() {
237         if (started.compareAndSet(false, true)) {
238             Thread callingThread = Thread.currentThread();
239             ClassLoader parentCCL = callingThread.getContextClassLoader();
240             // Avoid calling classloader leaking through Thread.inheritedAccessControlContext.
241             setContextClassLoader(callingThread, null);
242             try {
243                 final Thread t = threadFactory.newThread(taskRunner);
244                 // Set to null to ensure we not create classloader leaks by holds a strong reference to the inherited
245                 // classloader.
246                 // See:
247                 // - https://github.com/netty/netty/issues/7290
248                 // - https://bugs.openjdk.java.net/browse/JDK-7008595
249                 setContextClassLoader(t, null);
250 
251                 // Set the thread before starting it as otherwise inEventLoop() may return false and so produce
252                 // an assert error.
253                 // See https://github.com/netty/netty/issues/4357
254                 thread = t;
255                 t.start();
256             } finally {
257                 setContextClassLoader(callingThread, parentCCL);
258             }
259         }
260     }
261 
262     private static void setContextClassLoader(final Thread t, final ClassLoader cl) {
263         AccessController.doPrivileged(new PrivilegedAction<Void>() {
264             @Override
265             public Void run() {
266                 t.setContextClassLoader(cl);
267                 return null;
268             }
269         });
270     }
271 
272     final class TaskRunner implements Runnable {
273         @Override
274         public void run() {
275             for (;;) {
276                 Runnable task = takeTask();
277                 if (task != null) {
278                     try {
279                         runTask(task);
280                     } catch (Throwable t) {
281                         logger.warn("Unexpected exception from the global event executor: ", t);
282                     }
283 
284                     if (task != quietPeriodTask) {
285                         continue;
286                     }
287                 }
288 
289                 Queue<ScheduledFutureTask<?>> scheduledTaskQueue = GlobalEventExecutor.this.scheduledTaskQueue;
290                 // Terminate if there is no task in the queue (except the noop task).
291                 if (taskQueue.isEmpty() && (scheduledTaskQueue == null || scheduledTaskQueue.size() == 1)) {
292                     // Mark the current thread as stopped.
293                     // The following CAS must always success and must be uncontended,
294                     // because only one thread should be running at the same time.
295                     boolean stopped = started.compareAndSet(true, false);
296                     assert stopped;
297 
298                     // Check if there are pending entries added by execute() or schedule*() while we do CAS above.
299                     // Do not check scheduledTaskQueue because it is not thread-safe and can only be mutated from a
300                     // TaskRunner actively running tasks.
301                     if (taskQueue.isEmpty()) {
302                         // A) No new task was added and thus there's nothing to handle
303                         //    -> safe to terminate because there's nothing left to do
304                         // B) A new thread started and handled all the new tasks.
305                         //    -> safe to terminate the new thread will take care the rest
306                         break;
307                     }
308 
309                     // There are pending tasks added again.
310                     if (!started.compareAndSet(false, true)) {
311                         // startThread() started a new thread and set 'started' to true.
312                         // -> terminate this thread so that the new thread reads from taskQueue exclusively.
313                         break;
314                     }
315 
316                     // New tasks were added, but this worker was faster to set 'started' to true.
317                     // i.e. a new worker thread was not started by startThread().
318                     // -> keep this thread alive to handle the newly added entries.
319                 }
320             }
321         }
322     }
323 }