1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.channel.socket.nio;
17
18 import org.jboss.netty.logging.InternalLogger;
19 import org.jboss.netty.logging.InternalLoggerFactory;
20 import org.jboss.netty.util.internal.SystemPropertyUtil;
21
22 import java.io.IOException;
23 import java.nio.channels.CancelledKeyException;
24 import java.nio.channels.Selector;
25 import java.util.concurrent.TimeUnit;
26
27 final class SelectorUtil {
28 private static final InternalLogger logger =
29 InternalLoggerFactory.getInstance(SelectorUtil.class);
30
31 static final int DEFAULT_IO_THREADS = Runtime.getRuntime().availableProcessors() * 2;
32 static final long DEFAULT_SELECT_TIMEOUT = 500;
33 static final long SELECT_TIMEOUT =
34 SystemPropertyUtil.getLong("org.jboss.netty.selectTimeout", DEFAULT_SELECT_TIMEOUT);
35 static final long SELECT_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(SELECT_TIMEOUT);
36 static final boolean EPOLL_BUG_WORKAROUND =
37 SystemPropertyUtil.getBoolean("org.jboss.netty.epollBugWorkaround", false);
38
39
40
41
42
43
44 static {
45 String key = "sun.nio.ch.bugLevel";
46 try {
47 String buglevel = System.getProperty(key);
48 if (buglevel == null) {
49 System.setProperty(key, "");
50 }
51 } catch (SecurityException e) {
52 if (logger.isDebugEnabled()) {
53 logger.debug("Unable to get/set System Property '" + key + '\'', e);
54 }
55 }
56 if (logger.isDebugEnabled()) {
57 logger.debug("Using select timeout of " + SELECT_TIMEOUT);
58 logger.debug("Epoll-bug workaround enabled = " + EPOLL_BUG_WORKAROUND);
59 }
60 }
61
62 static Selector open() throws IOException {
63 return Selector.open();
64 }
65
66 static int select(Selector selector) throws IOException {
67 try {
68 return selector.select(SELECT_TIMEOUT);
69 } catch (CancelledKeyException e) {
70 if (logger.isDebugEnabled()) {
71 logger.debug(
72 CancelledKeyException.class.getSimpleName() +
73 " raised by a Selector - JDK bug?", e);
74 }
75
76 }
77 return -1;
78 }
79
80 private SelectorUtil() {
81
82 }
83 }