Motivation: fix checkstyle errors and many classes are unnecessarily public Modification: -fixed maven checkstyle errors -using package-private and final classes Result: better code quality
76 lines
2.6 KiB
Java
76 lines
2.6 KiB
Java
/*
|
|
* Copyright 2020 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.uring;
|
|
|
|
import io.netty.channel.unix.Socket;
|
|
import io.netty.util.internal.NativeLibraryLoader;
|
|
import io.netty.util.internal.PlatformDependent;
|
|
import io.netty.util.internal.SystemPropertyUtil;
|
|
import io.netty.util.internal.ThrowableUtil;
|
|
import java.util.Locale;
|
|
|
|
final class Native {
|
|
|
|
private static final int DEFAULT_RING_SIZE = SystemPropertyUtil.getInt("io.netty.uring.ringSize", 32);
|
|
static {
|
|
loadNativeLibrary();
|
|
Socket.initialize();
|
|
}
|
|
|
|
public static RingBuffer createRingBuffer(int ringSize) {
|
|
//Todo throw Exception if it's null
|
|
return ioUringSetup(ringSize);
|
|
}
|
|
|
|
public static RingBuffer createRingBuffer() {
|
|
//Todo throw Exception if it's null
|
|
return ioUringSetup(DEFAULT_RING_SIZE);
|
|
}
|
|
|
|
private static native RingBuffer ioUringSetup(int entries);
|
|
|
|
public static native int ioUringEnter(int ringFd, int toSubmit, int minComplete, int flags);
|
|
|
|
// for testing(it is only temporary)
|
|
public static native int createFile();
|
|
|
|
private Native() {
|
|
// utility
|
|
}
|
|
|
|
// From io_uring native library
|
|
private static void loadNativeLibrary() {
|
|
String name = PlatformDependent.normalizedOs().toLowerCase(Locale.UK).trim();
|
|
if (!name.startsWith("linux")) {
|
|
throw new IllegalStateException("Only supported on Linux");
|
|
}
|
|
String staticLibName = "netty_transport_native_io_uring";
|
|
String sharedLibName = staticLibName + '_' + PlatformDependent.normalizedArch();
|
|
ClassLoader cl = PlatformDependent.getClassLoader(Native.class);
|
|
try {
|
|
NativeLibraryLoader.load(sharedLibName, cl);
|
|
} catch (UnsatisfiedLinkError e1) {
|
|
try {
|
|
NativeLibraryLoader.load(staticLibName, cl);
|
|
System.out.println("Failed to load io_uring");
|
|
} catch (UnsatisfiedLinkError e2) {
|
|
ThrowableUtil.addSuppressed(e1, e2);
|
|
throw e1;
|
|
}
|
|
}
|
|
}
|
|
}
|