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 java.io.IOException;
19 import java.nio.channels.CancelledKeyException;
20 import java.nio.channels.Selector;
21 import java.util.concurrent.TimeUnit;
22
23 import org.jboss.netty.logging.InternalLogger;
24 import org.jboss.netty.logging.InternalLoggerFactory;
25 import org.jboss.netty.util.internal.SystemPropertyUtil;
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 int select(Selector selector) throws IOException {
63 try {
64 return selector.select(SELECT_TIMEOUT);
65 } catch (CancelledKeyException e) {
66 if (logger.isDebugEnabled()) {
67 logger.debug(
68 CancelledKeyException.class.getSimpleName() +
69 " raised by a Selector - JDK bug?", e);
70 }
71
72 }
73 return -1;
74 }
75
76 private SelectorUtil() {
77
78 }
79 }