2014-06-26 12:20:47 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2014 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
package io.netty.channel.epoll;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tells if <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a> is supported.
|
|
|
|
*/
|
|
|
|
public final class Epoll {
|
|
|
|
|
2014-08-05 00:03:25 +02:00
|
|
|
private static final Throwable UNAVAILABILITY_CAUSE;
|
2014-06-26 12:20:47 +02:00
|
|
|
|
|
|
|
static {
|
2014-08-05 00:03:25 +02:00
|
|
|
Throwable cause = null;
|
2014-06-26 12:20:47 +02:00
|
|
|
int epollFd = -1;
|
|
|
|
int eventFd = -1;
|
|
|
|
try {
|
|
|
|
epollFd = Native.epollCreate();
|
|
|
|
eventFd = Native.eventFd();
|
2014-08-05 00:03:25 +02:00
|
|
|
} catch (Throwable t) {
|
|
|
|
cause = t;
|
2014-06-26 12:20:47 +02:00
|
|
|
} finally {
|
|
|
|
if (epollFd != -1) {
|
|
|
|
try {
|
|
|
|
Native.close(epollFd);
|
|
|
|
} catch (Exception ignore) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (eventFd != -1) {
|
|
|
|
try {
|
|
|
|
Native.close(eventFd);
|
|
|
|
} catch (Exception ignore) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 00:03:25 +02:00
|
|
|
|
|
|
|
if (cause != null) {
|
|
|
|
UNAVAILABILITY_CAUSE = cause;
|
|
|
|
} else {
|
|
|
|
UNAVAILABILITY_CAUSE = null;
|
|
|
|
}
|
2014-06-26 12:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns {@code true} if and only if the
|
2014-08-05 00:03:25 +02:00
|
|
|
* <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a> is available.
|
2014-06-26 12:20:47 +02:00
|
|
|
*/
|
|
|
|
public static boolean isAvailable() {
|
2014-08-05 00:03:25 +02:00
|
|
|
return UNAVAILABILITY_CAUSE == null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a> is
|
|
|
|
* available.
|
|
|
|
*
|
|
|
|
* @throws UnsatisfiedLinkError if unavailable
|
|
|
|
*/
|
|
|
|
public static void ensureAvailability() {
|
|
|
|
if (UNAVAILABILITY_CAUSE != null) {
|
|
|
|
throw (Error) new UnsatisfiedLinkError(
|
|
|
|
"failed to load the required native library").initCause(UNAVAILABILITY_CAUSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the cause of unavailability of
|
|
|
|
* <a href="http://netty.io/wiki/native-transports.html">{@code netty-transport-native-epoll}</a>.
|
|
|
|
*
|
|
|
|
* @return the cause if unavailable. {@code null} if available.
|
|
|
|
*/
|
|
|
|
public static Throwable unavailabilityCause() {
|
|
|
|
return UNAVAILABILITY_CAUSE;
|
2014-06-26 12:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private Epoll() { }
|
|
|
|
}
|