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