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