View Javadoc
1   /*
2    * Copyright 2014 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.epoll;
17  
18  import io.netty.channel.DefaultSelectStrategyFactory;
19  import io.netty.channel.EventLoopGroup;
20  import io.netty.channel.MultithreadEventLoopGroup;
21  import io.netty.channel.SelectStrategyFactory;
22  import io.netty.util.concurrent.EventExecutor;
23  import io.netty.util.concurrent.RejectedExecutionHandler;
24  import io.netty.util.concurrent.RejectedExecutionHandlers;
25  
26  import java.util.concurrent.ThreadFactory;
27  
28  /**
29   * {@link EventLoopGroup} which uses epoll under the covers. Because of this
30   * it only works on linux.
31   */
32  public final class EpollEventLoopGroup extends MultithreadEventLoopGroup {
33      {
34          // Ensure JNI is initialized by the time this class is loaded.
35          Epoll.ensureAvailability();
36      }
37  
38      /**
39       * Create a new instance using the default number of threads and the default {@link ThreadFactory}.
40       */
41      public EpollEventLoopGroup() {
42          this(0);
43      }
44  
45      /**
46       * Create a new instance using the specified number of threads and the default {@link ThreadFactory}.
47       */
48      public EpollEventLoopGroup(int nThreads) {
49          this(nThreads, (ThreadFactory) null);
50      }
51  
52      /**
53       * Create a new instance using the specified number of threads and the default {@link ThreadFactory}.
54       */
55      @SuppressWarnings("deprecation")
56      public EpollEventLoopGroup(int nThreads, SelectStrategyFactory selectStrategyFactory) {
57          this(nThreads, null, selectStrategyFactory);
58      }
59  
60      /**
61       * Create a new instance using the specified number of threads and the given {@link ThreadFactory}.
62       */
63      @SuppressWarnings("deprecation")
64      public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory) {
65          this(nThreads, threadFactory, 0);
66      }
67  
68      /**
69       * Create a new instance using the specified number of threads and the given {@link ThreadFactory}.
70       */
71      @SuppressWarnings("deprecation")
72      public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, SelectStrategyFactory selectStrategyFactory) {
73          this(nThreads, threadFactory, 0, selectStrategyFactory);
74      }
75  
76      /**
77       * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given
78       * maximal amount of epoll events to handle per epollWait(...).
79       *
80       * @deprecated  Use {@link #EpollEventLoopGroup(int)} or {@link #EpollEventLoopGroup(int, ThreadFactory)}
81       */
82      @Deprecated
83      public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce) {
84          this(nThreads, threadFactory, maxEventsAtOnce, DefaultSelectStrategyFactory.INSTANCE);
85      }
86  
87      /**
88       * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given
89       * maximal amount of epoll events to handle per epollWait(...).
90       *
91       * @deprecated  Use {@link #EpollEventLoopGroup(int)}, {@link #EpollEventLoopGroup(int, ThreadFactory)}, or
92       * {@link #EpollEventLoopGroup(int, SelectStrategyFactory)}
93       */
94      @Deprecated
95      public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce,
96                                 SelectStrategyFactory selectStrategyFactory) {
97          super(nThreads, threadFactory, maxEventsAtOnce, selectStrategyFactory, RejectedExecutionHandlers.reject());
98      }
99  
100     public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce,
101                                SelectStrategyFactory selectStrategyFactory,
102                                RejectedExecutionHandler rejectedExecutionHandler) {
103         super(nThreads, threadFactory, maxEventsAtOnce, selectStrategyFactory, rejectedExecutionHandler);
104     }
105 
106     /**
107      * Sets the percentage of the desired amount of time spent for I/O in the child event loops.  The default value is
108      * {@code 50}, which means the event loop will try to spend the same amount of time for I/O as for non-I/O tasks.
109      */
110     public void setIoRatio(int ioRatio) {
111         for (EventExecutor e: children()) {
112             ((EpollEventLoop) e).setIoRatio(ioRatio);
113         }
114     }
115 
116     @Override
117     protected EventExecutor newChild(ThreadFactory threadFactory, Object... args) throws Exception {
118         return new EpollEventLoop(this, threadFactory, (Integer) args[0],
119                 ((SelectStrategyFactory) args[1]).newSelectStrategy(), (RejectedExecutionHandler) args[2]);
120     }
121 }