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