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    *   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.channel.epoll;
17  
18  import io.netty.channel.DefaultSelectStrategyFactory;
19  import io.netty.channel.EventLoopGroup;
20  import io.netty.channel.EventLoopTaskQueueFactory;
21  import io.netty.channel.IoEventLoop;
22  import io.netty.channel.IoHandler;
23  import io.netty.channel.MultiThreadIoEventLoopGroup;
24  import io.netty.channel.SelectStrategyFactory;
25  import io.netty.channel.SingleThreadEventLoop;
26  import io.netty.util.concurrent.EventExecutorChooserFactory;
27  import io.netty.util.concurrent.RejectedExecutionHandler;
28  import io.netty.util.concurrent.RejectedExecutionHandlers;
29  import io.netty.util.internal.logging.InternalLogger;
30  import io.netty.util.internal.logging.InternalLoggerFactory;
31  
32  import java.util.concurrent.Executor;
33  import java.util.concurrent.ThreadFactory;
34  
35  /**
36   * {@link EventLoopGroup} which uses epoll under the covers. Because of this
37   * it only works on linux.
38   *
39   * @deprecated Use {@link MultiThreadIoEventLoopGroup} with {@link EpollIoHandler}.
40   */
41  @Deprecated
42  public final class EpollEventLoopGroup extends MultiThreadIoEventLoopGroup {
43  
44      // This does not use static by design to ensure the class can be loaded and only do the check when its actually
45      // instanced.
46      {
47          // Ensure JNI is initialized by the time this class is loaded.
48          Epoll.ensureAvailability();
49      }
50      private static final InternalLogger LOGGER = InternalLoggerFactory.getInstance(EpollEventLoopGroup.class);
51  
52      /**
53       * Create a new instance using the default number of threads and the default {@link ThreadFactory}.
54       */
55      public EpollEventLoopGroup() {
56          this(0);
57      }
58  
59      /**
60       * Create a new instance using the specified number of threads and the default {@link ThreadFactory}.
61       */
62      public EpollEventLoopGroup(int nThreads) {
63          this(nThreads, (ThreadFactory) null);
64      }
65  
66      /**
67       * Create a new instance using the default number of threads and the given {@link ThreadFactory}.
68       */
69      @SuppressWarnings("deprecation")
70      public EpollEventLoopGroup(ThreadFactory threadFactory) {
71          this(0, threadFactory, 0);
72      }
73  
74      /**
75       * Create a new instance using the specified number of threads and the default {@link ThreadFactory}.
76       */
77      @SuppressWarnings("deprecation")
78      public EpollEventLoopGroup(int nThreads, SelectStrategyFactory selectStrategyFactory) {
79          this(nThreads, (ThreadFactory) null, selectStrategyFactory);
80      }
81  
82      /**
83       * Create a new instance using the specified number of threads and the given {@link ThreadFactory}.
84       */
85      @SuppressWarnings("deprecation")
86      public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory) {
87          this(nThreads, threadFactory, 0);
88      }
89  
90      public EpollEventLoopGroup(int nThreads, Executor executor) {
91          this(nThreads, executor, DefaultSelectStrategyFactory.INSTANCE);
92      }
93  
94      /**
95       * Create a new instance using the specified number of threads and the given {@link ThreadFactory}.
96       */
97      @SuppressWarnings("deprecation")
98      public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, SelectStrategyFactory selectStrategyFactory) {
99          this(nThreads, threadFactory, 0, selectStrategyFactory);
100     }
101 
102     /**
103      * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given
104      * maximal amount of epoll events to handle per epollWait(...).
105      *
106      * @deprecated  Use {@link #EpollEventLoopGroup(int)} or {@link #EpollEventLoopGroup(int, ThreadFactory)}
107      */
108     @Deprecated
109     public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce) {
110         this(nThreads, threadFactory, maxEventsAtOnce, DefaultSelectStrategyFactory.INSTANCE);
111     }
112 
113     /**
114      * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given
115      * maximal amount of epoll events to handle per epollWait(...).
116      *
117      * @deprecated  Use {@link #EpollEventLoopGroup(int)}, {@link #EpollEventLoopGroup(int, ThreadFactory)}, or
118      * {@link #EpollEventLoopGroup(int, SelectStrategyFactory)}
119      */
120     @Deprecated
121     public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce,
122                                SelectStrategyFactory selectStrategyFactory) {
123         super(nThreads, threadFactory, EpollIoHandler.newFactory(maxEventsAtOnce, selectStrategyFactory),
124                 RejectedExecutionHandlers.reject());
125     }
126 
127     public EpollEventLoopGroup(int nThreads, Executor executor, SelectStrategyFactory selectStrategyFactory) {
128         super(nThreads, executor, EpollIoHandler.newFactory(0, selectStrategyFactory),
129                 RejectedExecutionHandlers.reject());
130     }
131 
132     public EpollEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
133                                SelectStrategyFactory selectStrategyFactory) {
134         super(nThreads, executor, EpollIoHandler.newFactory(0, selectStrategyFactory), chooserFactory,
135                 RejectedExecutionHandlers.reject());
136     }
137 
138     public EpollEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
139                                SelectStrategyFactory selectStrategyFactory,
140                                RejectedExecutionHandler rejectedExecutionHandler) {
141         super(nThreads, executor, EpollIoHandler.newFactory(0, selectStrategyFactory), chooserFactory,
142                 rejectedExecutionHandler);
143     }
144 
145     public EpollEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
146                                SelectStrategyFactory selectStrategyFactory,
147                                RejectedExecutionHandler rejectedExecutionHandler,
148                                EventLoopTaskQueueFactory queueFactory) {
149         super(nThreads, executor, EpollIoHandler.newFactory(0, selectStrategyFactory), chooserFactory,
150                 rejectedExecutionHandler, queueFactory);
151     }
152 
153     /**
154      * @param nThreads the number of threads that will be used by this instance.
155      * @param executor the Executor to use, or {@code null} if default one should be used.
156      * @param chooserFactory the {@link EventExecutorChooserFactory} to use.
157      * @param selectStrategyFactory the {@link SelectStrategyFactory} to use.
158      * @param rejectedExecutionHandler the {@link RejectedExecutionHandler} to use.
159      * @param taskQueueFactory the {@link EventLoopTaskQueueFactory} to use for
160      *                         {@link SingleThreadEventLoop#execute(Runnable)},
161      *                         or {@code null} if default one should be used.
162      * @param tailTaskQueueFactory the {@link EventLoopTaskQueueFactory} to use for
163      *                             {@link SingleThreadEventLoop#executeAfterEventLoopIteration(Runnable)},
164      *                             or {@code null} if default one should be used.
165      */
166     public EpollEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
167                                SelectStrategyFactory selectStrategyFactory,
168                                RejectedExecutionHandler rejectedExecutionHandler,
169                                EventLoopTaskQueueFactory taskQueueFactory,
170                                EventLoopTaskQueueFactory tailTaskQueueFactory) {
171         super(nThreads, executor, EpollIoHandler.newFactory(0, selectStrategyFactory),
172                 chooserFactory, rejectedExecutionHandler, taskQueueFactory, tailTaskQueueFactory);
173     }
174 
175     /**
176      * This method is a no-op.
177      *
178      * @deprecated
179      */
180     @Deprecated
181     public void setIoRatio(int ioRatio) {
182         LOGGER.debug("EpollEventLoopGroup.setIoRatio(int) logic was removed, this is a no-op");
183     }
184 
185     @Override
186     protected IoEventLoop newChild(Executor executor, IoHandler handler, Object... args) {
187         RejectedExecutionHandler rejectedExecutionHandler = (RejectedExecutionHandler) args[0];
188         EventLoopTaskQueueFactory taskQueueFactory = null;
189         EventLoopTaskQueueFactory tailTaskQueueFactory = null;
190 
191         int argsLength = args.length;
192         if (argsLength > 1) {
193             taskQueueFactory = (EventLoopTaskQueueFactory) args[1];
194         }
195         if (argsLength > 2) {
196             tailTaskQueueFactory = (EventLoopTaskQueueFactory) args[2];
197         }
198         return new EpollEventLoop(this, executor, handler, taskQueueFactory, tailTaskQueueFactory,
199                 rejectedExecutionHandler);
200     }
201 }