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.Channel;
19  import io.netty.channel.DefaultSelectStrategyFactory;
20  import io.netty.channel.EventLoopTaskQueueFactory;
21  import io.netty.channel.IoEventLoopGroup;
22  import io.netty.channel.IoEventLoop;
23  import io.netty.channel.IoHandlerFactory;
24  import io.netty.channel.MultiThreadIoEventLoopGroup;
25  import io.netty.channel.SelectStrategyFactory;
26  import io.netty.channel.SingleThreadEventLoop;
27  import io.netty.channel.SingleThreadIoEventLoop;
28  import io.netty.util.concurrent.EventExecutorChooserFactory;
29  import io.netty.util.concurrent.RejectedExecutionHandler;
30  import io.netty.util.concurrent.RejectedExecutionHandlers;
31  
32  import java.util.Iterator;
33  import java.util.Queue;
34  import java.util.concurrent.Executor;
35  import java.util.concurrent.ThreadFactory;
36  
37  
38  /**
39   * @deprecated Use {@link MultiThreadIoEventLoopGroup} with {@link KQueueIoHandler#newFactory()}.
40   */
41  @Deprecated
42  public final class KQueueEventLoopGroup extends MultiThreadIoEventLoopGroup {
43  
44      // This does not use static by design to ensure the class can be loaded and only do the check when its actually
45      // instanced.
46      {
47          // Ensure JNI is initialized by the time this class is loaded by this time!
48          KQueue.ensureAvailability();
49      }
50  
51      /**
52       * Create a new instance using the default number of threads and the default {@link ThreadFactory}.
53       */
54      public KQueueEventLoopGroup() {
55          this(0);
56      }
57  
58      /**
59       * Create a new instance using the specified number of threads and the default {@link ThreadFactory}.
60       */
61      public KQueueEventLoopGroup(int nThreads) {
62          this(nThreads, (ThreadFactory) null);
63      }
64  
65      /**
66       * Create a new instance using the default number of threads and the given {@link ThreadFactory}.
67       */
68      @SuppressWarnings("deprecation")
69      public KQueueEventLoopGroup(ThreadFactory threadFactory) {
70          this(0, threadFactory, 0);
71      }
72  
73      /**
74       * Create a new instance using the specified number of threads and the default {@link ThreadFactory}.
75       */
76      @SuppressWarnings("deprecation")
77      public KQueueEventLoopGroup(int nThreads, SelectStrategyFactory selectStrategyFactory) {
78          this(nThreads, (ThreadFactory) null, selectStrategyFactory);
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          this(nThreads, threadFactory, 0);
87      }
88  
89      public KQueueEventLoopGroup(int nThreads, Executor executor) {
90          this(nThreads, executor, DefaultSelectStrategyFactory.INSTANCE);
91      }
92  
93      /**
94       * Create a new instance using the specified number of threads and the given {@link ThreadFactory}.
95       */
96      @SuppressWarnings("deprecation")
97      public KQueueEventLoopGroup(int nThreads, ThreadFactory threadFactory,
98                                  SelectStrategyFactory selectStrategyFactory) {
99          this(nThreads, threadFactory, 0, selectStrategyFactory);
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 #KQueueEventLoopGroup(int)} or {@link #KQueueEventLoopGroup(int, ThreadFactory)}
107      */
108     @Deprecated
109     public KQueueEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce) {
110         this(nThreads, threadFactory, maxEventsAtOnce, DefaultSelectStrategyFactory.INSTANCE);
111     }
112 
113     /**
114      * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given
115      * maximal amount of epoll events to handle per epollWait(...).
116      *
117      * @deprecated  Use {@link #KQueueEventLoopGroup(int)}, {@link #KQueueEventLoopGroup(int, ThreadFactory)}, or
118      * {@link #KQueueEventLoopGroup(int, SelectStrategyFactory)}
119      */
120     @Deprecated
121     public KQueueEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce,
122                                SelectStrategyFactory selectStrategyFactory) {
123         super(nThreads, threadFactory, KQueueIoHandler.newFactory(maxEventsAtOnce, selectStrategyFactory),
124                 RejectedExecutionHandlers.reject());
125     }
126 
127     public KQueueEventLoopGroup(int nThreads, Executor executor, SelectStrategyFactory selectStrategyFactory) {
128         super(nThreads, executor, KQueueIoHandler.newFactory(0, selectStrategyFactory),
129                 RejectedExecutionHandlers.reject());
130     }
131 
132     public KQueueEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
133                                SelectStrategyFactory selectStrategyFactory) {
134         super(nThreads, executor, KQueueIoHandler.newFactory(0, selectStrategyFactory),
135                 chooserFactory, RejectedExecutionHandlers.reject());
136     }
137 
138     public KQueueEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
139                                SelectStrategyFactory selectStrategyFactory,
140                                RejectedExecutionHandler rejectedExecutionHandler) {
141         super(nThreads, executor,  KQueueIoHandler.newFactory(0, selectStrategyFactory), chooserFactory,
142                 rejectedExecutionHandler);
143     }
144 
145     public KQueueEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
146                                 SelectStrategyFactory selectStrategyFactory,
147                                 RejectedExecutionHandler rejectedExecutionHandler,
148                                 EventLoopTaskQueueFactory queueFactory) {
149         super(nThreads, executor, KQueueIoHandler.newFactory(0, selectStrategyFactory), chooserFactory,
150                 rejectedExecutionHandler, queueFactory);
151     }
152 
153     /**
154      * @param nThreads the number of threads that will be used by this instance.
155      * @param executor the Executor to use, or {@code null} if default one should be used.
156      * @param chooserFactory the {@link EventExecutorChooserFactory} to use.
157      * @param selectStrategyFactory the {@link SelectStrategyFactory} to use.
158      * @param rejectedExecutionHandler the {@link RejectedExecutionHandler} to use.
159      * @param taskQueueFactory the {@link EventLoopTaskQueueFactory} to use for
160      *                         {@link SingleThreadEventLoop#execute(Runnable)},
161      *                         or {@code null} if default one should be used.
162      * @param tailTaskQueueFactory the {@link EventLoopTaskQueueFactory} to use for
163      *                             {@link SingleThreadEventLoop#executeAfterEventLoopIteration(Runnable)},
164      *                             or {@code null} if default one should be used.
165      */
166     public KQueueEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
167                                SelectStrategyFactory selectStrategyFactory,
168                                RejectedExecutionHandler rejectedExecutionHandler,
169                                EventLoopTaskQueueFactory taskQueueFactory,
170                                EventLoopTaskQueueFactory tailTaskQueueFactory) {
171         super(nThreads, executor, KQueueIoHandler.newFactory(0, selectStrategyFactory), chooserFactory,
172                 rejectedExecutionHandler, taskQueueFactory, tailTaskQueueFactory);
173     }
174 
175     @Override
176     protected IoEventLoop newChild(Executor executor, IoHandlerFactory ioHandlerFactory, Object... args) {
177         RejectedExecutionHandler rejectedExecutionHandler = null;
178         EventLoopTaskQueueFactory taskQueueFactory = null;
179         EventLoopTaskQueueFactory tailTaskQueueFactory = null;
180         int argsLength = args.length;
181         if (argsLength > 0) {
182             rejectedExecutionHandler = (RejectedExecutionHandler) args[0];
183         }
184         if (argsLength > 1) {
185             taskQueueFactory = (EventLoopTaskQueueFactory) args[2];
186         }
187         if (argsLength > 2) {
188             tailTaskQueueFactory = (EventLoopTaskQueueFactory) args[1];
189         }
190         return new KQueueEventLoop(this, executor, ioHandlerFactory,
191                 KQueueEventLoop.newTaskQueue(taskQueueFactory),
192                 KQueueEventLoop.newTaskQueue(tailTaskQueueFactory),
193                 rejectedExecutionHandler);
194     }
195 
196     private static final class KQueueEventLoop extends SingleThreadIoEventLoop {
197         KQueueEventLoop(IoEventLoopGroup parent, Executor executor, IoHandlerFactory ioHandlerFactory,
198                         Queue<Runnable> taskQueue, Queue<Runnable> tailTaskQueue,
199                         RejectedExecutionHandler rejectedExecutionHandler) {
200             super(parent, executor, ioHandlerFactory, taskQueue, tailTaskQueue, rejectedExecutionHandler);
201         }
202 
203         static Queue<Runnable> newTaskQueue(
204                 EventLoopTaskQueueFactory queueFactory) {
205             if (queueFactory == null) {
206                 return newTaskQueue0(DEFAULT_MAX_PENDING_TASKS);
207             }
208             return queueFactory.newTaskQueue(DEFAULT_MAX_PENDING_TASKS);
209         }
210 
211         @Override
212         public int registeredChannels() {
213             assert inEventLoop();
214             return ((KQueueIoHandler) ioHandler()).numRegisteredChannels();
215         }
216 
217         @Override
218         public Iterator<Channel> registeredChannelsIterator() {
219             assert inEventLoop();
220             return ((KQueueIoHandler) ioHandler()).registeredChannelsList().iterator();
221         }
222     }
223 }