View Javadoc

1   /*
2    * Copyright 2014 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    *   http://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.epoll;
17  
18  import io.netty.channel.unix.FileDescriptor;
19  import io.netty.util.internal.PlatformDependent;
20  
21  /**
22   * Tells if <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a> is supported.
23   */
24  public final class Epoll {
25  
26      private static final Throwable UNAVAILABILITY_CAUSE;
27  
28      static  {
29          Throwable cause = null;
30          FileDescriptor epollFd = null;
31          FileDescriptor eventFd = null;
32          try {
33              epollFd = Native.newEpollCreate();
34              eventFd = Native.newEventFd();
35          } catch (Throwable t) {
36              cause = t;
37          } finally {
38              if (epollFd != null) {
39                  try {
40                      epollFd.close();
41                  } catch (Exception ignore) {
42                      // ignore
43                  }
44              }
45              if (eventFd != null) {
46                  try {
47                      eventFd.close();
48                  } catch (Exception ignore) {
49                      // ignore
50                  }
51              }
52          }
53  
54          if (cause != null) {
55              UNAVAILABILITY_CAUSE = cause;
56          } else {
57              UNAVAILABILITY_CAUSE = PlatformDependent.hasUnsafe()
58                      ? null
59                      : new IllegalStateException(
60                              "sun.misc.Unsafe not available",
61                              PlatformDependent.getUnsafeUnavailabilityCause());
62          }
63      }
64  
65      /**
66       * Returns {@code true} if and only if the
67       * <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a> is available.
68       */
69      public static boolean isAvailable() {
70          return UNAVAILABILITY_CAUSE == null;
71      }
72  
73      /**
74       * Ensure that <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a> is
75       * available.
76       *
77       * @throws UnsatisfiedLinkError if unavailable
78       */
79      public static void ensureAvailability() {
80          if (UNAVAILABILITY_CAUSE != null) {
81              throw (Error) new UnsatisfiedLinkError(
82                      "failed to load the required native library").initCause(UNAVAILABILITY_CAUSE);
83          }
84      }
85  
86      /**
87       * Returns the cause of unavailability of
88       * <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a>.
89       *
90       * @return the cause if unavailable. {@code null} if available.
91       */
92      public static Throwable unavailabilityCause() {
93          return UNAVAILABILITY_CAUSE;
94      }
95  
96      private Epoll() { }
97  }