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    *   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.channel;
17  
18  import io.netty.util.concurrent.EventExecutorGroup;
19  import io.netty.util.concurrent.RejectedExecutionHandler;
20  import io.netty.util.concurrent.RejectedExecutionHandlers;
21  import io.netty.util.concurrent.SingleThreadEventExecutor;
22  import io.netty.util.internal.SystemPropertyUtil;
23  
24  import java.util.concurrent.ThreadFactory;
25  
26  /**
27   * Abstract base class for {@link EventLoop}'s that execute all its submitted tasks in a single thread.
28   *
29   */
30  public abstract class SingleThreadEventLoop extends SingleThreadEventExecutor implements EventLoop {
31  
32      protected static final int DEFAULT_MAX_PENDING_TASKS = Math.max(16,
33              SystemPropertyUtil.getInt("io.netty.eventLoop.maxPendingTasks", Integer.MAX_VALUE));
34  
35      /**
36       * @see {@link SingleThreadEventExecutor#SingleThreadEventExecutor(EventExecutorGroup, ThreadFactory, boolean)}
37       */
38      protected SingleThreadEventLoop(EventLoopGroup parent, ThreadFactory threadFactory, boolean addTaskWakesUp) {
39          this(parent, threadFactory, addTaskWakesUp, DEFAULT_MAX_PENDING_TASKS, RejectedExecutionHandlers.reject());
40      }
41  
42      protected SingleThreadEventLoop(EventLoopGroup parent, ThreadFactory threadFactory,
43                                      boolean addTaskWakesUp, int maxPendingTasks,
44                                      RejectedExecutionHandler rejectedExecutionHandler) {
45          super(parent, threadFactory, addTaskWakesUp, maxPendingTasks, rejectedExecutionHandler);
46      }
47  
48      @Override
49      public EventLoopGroup parent() {
50          return (EventLoopGroup) super.parent();
51      }
52  
53      @Override
54      public EventLoop next() {
55          return (EventLoop) super.next();
56      }
57  
58      @Override
59      public ChannelFuture register(Channel channel) {
60          return register(channel, new DefaultChannelPromise(channel, this));
61      }
62  
63      @Override
64      public ChannelFuture register(final Channel channel, final ChannelPromise promise) {
65          if (channel == null) {
66              throw new NullPointerException("channel");
67          }
68          if (promise == null) {
69              throw new NullPointerException("promise");
70          }
71  
72          channel.unsafe().register(this, promise);
73          return promise;
74      }
75  
76      @Override
77      protected boolean wakesUpForTask(Runnable task) {
78          return !(task instanceof NonWakeupRunnable);
79      }
80  
81      /**
82       * Marker interface for {@link Runnable} that will not trigger an {@link #wakeup(boolean)} in all cases.
83       */
84      interface NonWakeupRunnable extends Runnable { }
85  }