1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.kqueue;
17
18 import io.netty.channel.ChannelOption;
19 import io.netty.channel.unix.FileDescriptor;
20 import io.netty.util.internal.SystemPropertyUtil;
21 import io.netty.util.internal.logging.InternalLogger;
22 import io.netty.util.internal.logging.InternalLoggerFactory;
23
24
25
26
27 public final class KQueue {
28 private static final Throwable UNAVAILABILITY_CAUSE;
29
30 static {
31 Throwable cause = null;
32 if (SystemPropertyUtil.getBoolean("io.netty.transport.noNative", false)) {
33 cause = new UnsupportedOperationException(
34 "Native transport was explicit disabled with -Dio.netty.transport.noNative=true");
35 } else {
36 FileDescriptor kqueueFd = null;
37 try {
38 kqueueFd = Native.newKQueue();
39 } catch (Throwable t) {
40 cause = t;
41 } finally {
42 if (kqueueFd != null) {
43 try {
44 kqueueFd.close();
45 } catch (Exception ignore) {
46
47 }
48 }
49 }
50 }
51 if (cause != null) {
52 InternalLogger logger = InternalLoggerFactory.getInstance(KQueue.class);
53 if (logger.isTraceEnabled()) {
54 logger.debug("KQueue support is not available", cause);
55 } else if (logger.isDebugEnabled()) {
56 logger.debug("KQueue support is not available: {}", cause.getMessage());
57 }
58 }
59 UNAVAILABILITY_CAUSE = cause;
60 }
61
62
63
64
65
66 public static boolean isAvailable() {
67 return UNAVAILABILITY_CAUSE == null;
68 }
69
70
71
72
73
74
75
76 public static void ensureAvailability() {
77 if (UNAVAILABILITY_CAUSE != null) {
78 throw (Error) new UnsatisfiedLinkError(
79 "failed to load the required native library").initCause(UNAVAILABILITY_CAUSE);
80 }
81 }
82
83
84
85
86
87
88
89 public static Throwable unavailabilityCause() {
90 return UNAVAILABILITY_CAUSE;
91 }
92
93
94
95
96
97
98
99 public static boolean isTcpFastOpenClientSideAvailable() {
100 return isAvailable() && Native.IS_SUPPORTING_TCP_FASTOPEN_CLIENT;
101 }
102
103
104
105
106
107
108
109 public static boolean isTcpFastOpenServerSideAvailable() {
110 return isAvailable() && Native.IS_SUPPORTING_TCP_FASTOPEN_SERVER;
111 }
112
113 private KQueue() {
114 }
115 }