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.EventLoop;
20 import io.netty.channel.EventLoopGroup;
21 import io.netty.channel.EventLoopTaskQueueFactory;
22 import io.netty.channel.MultithreadEventLoopGroup;
23 import io.netty.channel.SelectStrategyFactory;
24 import io.netty.util.concurrent.EventExecutorChooserFactory;
25 import io.netty.util.concurrent.RejectedExecutionHandler;
26 import io.netty.util.concurrent.RejectedExecutionHandlers;
27
28 import java.util.concurrent.Executor;
29 import java.util.concurrent.ThreadFactory;
30
31 /**
32  * {@link EventLoopGroup} which uses epoll under the covers. Because of this
33  * it only works on linux.
34  */

35 public final class EpollEventLoopGroup extends MultithreadEventLoopGroup {
36     {
37         // Ensure JNI is initialized by the time this class is loaded.
38         Epoll.ensureAvailability();
39     }
40
41     /**
42      * Create a new instance using the default number of threads and the default {@link ThreadFactory}.
43      */

44     public EpollEventLoopGroup() {
45         this(0);
46     }
47
48     /**
49      * Create a new instance using the specified number of threads and the default {@link ThreadFactory}.
50      */

51     public EpollEventLoopGroup(int nThreads) {
52         this(nThreads, (ThreadFactory) null);
53     }
54
55     /**
56      * Create a new instance using the default number of threads and the given {@link ThreadFactory}.
57      */

58     @SuppressWarnings("deprecation")
59     public EpollEventLoopGroup(ThreadFactory threadFactory) {
60         this(0, threadFactory, 0);
61     }
62
63     /**
64      * Create a new instance using the specified number of threads and the default {@link ThreadFactory}.
65      */

66     @SuppressWarnings("deprecation")
67     public EpollEventLoopGroup(int nThreads, SelectStrategyFactory selectStrategyFactory) {
68         this(nThreads, (ThreadFactory) null, selectStrategyFactory);
69     }
70
71     /**
72      * Create a new instance using the specified number of threads and the given {@link ThreadFactory}.
73      */

74     @SuppressWarnings("deprecation")
75     public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory) {
76         this(nThreads, threadFactory, 0);
77     }
78
79     public EpollEventLoopGroup(int nThreads, Executor executor) {
80         this(nThreads, executor, DefaultSelectStrategyFactory.INSTANCE);
81     }
82
83     /**
84      * Create a new instance using the specified number of threads and the given {@link ThreadFactory}.
85      */

86     @SuppressWarnings("deprecation")
87     public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, SelectStrategyFactory selectStrategyFactory) {
88         this(nThreads, threadFactory, 0, selectStrategyFactory);
89     }
90
91     /**
92      * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given
93      * maximal amount of epoll events to handle per epollWait(...).
94      *
95      * @deprecated  Use {@link #EpollEventLoopGroup(int)} or {@link #EpollEventLoopGroup(int, ThreadFactory)}
96      */

97     @Deprecated
98     public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce) {
99         this(nThreads, threadFactory, maxEventsAtOnce, DefaultSelectStrategyFactory.INSTANCE);
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)}, {@link #EpollEventLoopGroup(int, ThreadFactory)}, or
107      * {@link #EpollEventLoopGroup(int, SelectStrategyFactory)}
108      */

109     @Deprecated
110     public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce,
111                                SelectStrategyFactory selectStrategyFactory) {
112         super(nThreads, threadFactory, maxEventsAtOnce, selectStrategyFactory, RejectedExecutionHandlers.reject());
113     }
114
115     public EpollEventLoopGroup(int nThreads, Executor executor, SelectStrategyFactory selectStrategyFactory) {
116         super(nThreads, executor, 0, selectStrategyFactory, RejectedExecutionHandlers.reject());
117     }
118
119     public EpollEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
120                                SelectStrategyFactory selectStrategyFactory) {
121         super(nThreads, executor, chooserFactory, 0, selectStrategyFactory, RejectedExecutionHandlers.reject());
122     }
123
124     public EpollEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
125                                SelectStrategyFactory selectStrategyFactory,
126                                RejectedExecutionHandler rejectedExecutionHandler) {
127         super(nThreads, executor, chooserFactory, 0, selectStrategyFactory, rejectedExecutionHandler);
128     }
129
130     public EpollEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
131                                SelectStrategyFactory selectStrategyFactory,
132                                RejectedExecutionHandler rejectedExecutionHandler,
133                                EventLoopTaskQueueFactory queueFactory) {
134         super(nThreads, executor, chooserFactory, 0, selectStrategyFactory, rejectedExecutionHandler, queueFactory);
135     }
136
137     /**
138      * @deprecated This method will be removed in future releases, and is not guaranteed to have any impacts.
139      */

140     @Deprecated
141     public void setIoRatio(int ioRatio) {
142         if (ioRatio <= 0 || ioRatio > 100) {
143             throw new IllegalArgumentException("ioRatio: " + ioRatio + " (expected: 0 < ioRatio <= 100)");
144         }
145     }
146
147     @Override
148     protected EventLoop newChild(Executor executor, Object... args) throws Exception {
149         EventLoopTaskQueueFactory queueFactory = args.length == 4 ? (EventLoopTaskQueueFactory) args[3] : null;
150         return new EpollEventLoop(this, executor, (Integer) args[0],
151                 ((SelectStrategyFactory) args[1]).newSelectStrategy(),
152                 (RejectedExecutionHandler) args[2], queueFactory);
153     }
154 }
155