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