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