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