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    *   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.kqueue;
17  
18  import io.netty.channel.DefaultSelectStrategyFactory;
19  import io.netty.channel.EventLoop;
20  import io.netty.channel.MultithreadEventLoopGroup;
21  import io.netty.channel.SelectStrategyFactory;
22  import io.netty.util.concurrent.EventExecutor;
23  import io.netty.util.concurrent.EventExecutorChooserFactory;
24  import io.netty.util.concurrent.RejectedExecutionHandler;
25  import io.netty.util.concurrent.RejectedExecutionHandlers;
26  import io.netty.util.internal.UnstableApi;
27  
28  import java.util.concurrent.Executor;
29  import java.util.concurrent.ThreadFactory;
30  
31  @UnstableApi
32  public final class KQueueEventLoopGroup extends MultithreadEventLoopGroup {
33      {
34          // Ensure JNI is initialized by the time this class is loaded by this time!
35          KQueue.ensureAvailability();
36      }
37      /**
38       * Create a new instance using the default number of threads and the default {@link ThreadFactory}.
39       */
40      public KQueueEventLoopGroup() {
41          this(0);
42      }
43  
44      /**
45       * Create a new instance using the specified number of threads and the default {@link ThreadFactory}.
46       */
47      public KQueueEventLoopGroup(int nThreads) {
48          this(nThreads, (ThreadFactory) null);
49      }
50  
51      /**
52       * Create a new instance using the specified number of threads and the default {@link ThreadFactory}.
53       */
54      @SuppressWarnings("deprecation")
55      public KQueueEventLoopGroup(int nThreads, SelectStrategyFactory selectStrategyFactory) {
56          this(nThreads, (ThreadFactory) null, selectStrategyFactory);
57      }
58  
59      /**
60       * Create a new instance using the specified number of threads and the given {@link ThreadFactory}.
61       */
62      @SuppressWarnings("deprecation")
63      public KQueueEventLoopGroup(int nThreads, ThreadFactory threadFactory) {
64          this(nThreads, threadFactory, 0);
65      }
66  
67      public KQueueEventLoopGroup(int nThreads, Executor executor) {
68          this(nThreads, executor, DefaultSelectStrategyFactory.INSTANCE);
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                                  SelectStrategyFactory selectStrategyFactory) {
77          this(nThreads, threadFactory, 0, selectStrategyFactory);
78      }
79  
80      /**
81       * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given
82       * maximal amount of epoll events to handle per epollWait(...).
83       *
84       * @deprecated  Use {@link #KQueueEventLoopGroup(int)} or {@link #KQueueEventLoopGroup(int, ThreadFactory)}
85       */
86      @Deprecated
87      public KQueueEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce) {
88          this(nThreads, threadFactory, maxEventsAtOnce, DefaultSelectStrategyFactory.INSTANCE);
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 #KQueueEventLoopGroup(int)}, {@link #KQueueEventLoopGroup(int, ThreadFactory)}, or
96       * {@link #KQueueEventLoopGroup(int, SelectStrategyFactory)}
97       */
98      @Deprecated
99      public KQueueEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce,
100                                SelectStrategyFactory selectStrategyFactory) {
101         super(nThreads, threadFactory, maxEventsAtOnce, selectStrategyFactory, RejectedExecutionHandlers.reject());
102     }
103 
104     public KQueueEventLoopGroup(int nThreads, Executor executor, SelectStrategyFactory selectStrategyFactory) {
105         super(nThreads, executor, 0, selectStrategyFactory, RejectedExecutionHandlers.reject());
106     }
107 
108     public KQueueEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
109                                SelectStrategyFactory selectStrategyFactory) {
110         super(nThreads, executor, chooserFactory, 0, selectStrategyFactory, RejectedExecutionHandlers.reject());
111     }
112 
113     public KQueueEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
114                                SelectStrategyFactory selectStrategyFactory,
115                                RejectedExecutionHandler rejectedExecutionHandler) {
116         super(nThreads, executor, chooserFactory, 0, selectStrategyFactory, rejectedExecutionHandler);
117     }
118 
119     /**
120      * Sets the percentage of the desired amount of time spent for I/O in the child event loops.  The default value is
121      * {@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.
122      */
123     public void setIoRatio(int ioRatio) {
124         for (EventExecutor e: this) {
125             ((KQueueEventLoop) e).setIoRatio(ioRatio);
126         }
127     }
128 
129     @Override
130     protected EventLoop newChild(Executor executor, Object... args) throws Exception {
131         return new KQueueEventLoop(this, executor, (Integer) args[0],
132                 ((SelectStrategyFactory) args[1]).newSelectStrategy(), (RejectedExecutionHandler) args[2]);
133     }
134 }