Lookup constants via JNI

Motivation:

We should better use JNI to lookup constants so we are sure we not mess
things up

Modifications:

Use JNI calls to lookup constants once

Result:

Less error prone code
This commit is contained in:
Norman Maurer 2020-09-05 09:40:02 +02:00
parent c6db51ba1f
commit dfca811648
6 changed files with 26 additions and 10 deletions

View File

@ -337,6 +337,14 @@ static jint netty_io_uring_sockCloexec(JNIEnv* env, jclass clazz) {
return SOCK_CLOEXEC; return SOCK_CLOEXEC;
} }
static jint netty_io_uring_etime(JNIEnv* env, jclass clazz) {
return ETIME;
}
static jint netty_io_uring_ecanceled(JNIEnv* env, jclass clazz) {
return ECANCELED;
}
static jint netty_io_uring_pollin(JNIEnv* env, jclass clazz) { static jint netty_io_uring_pollin(JNIEnv* env, jclass clazz) {
return POLLIN; return POLLIN;
} }
@ -381,6 +389,10 @@ static jint netty_io_uring_ioringOpConnect(JNIEnv* env, jclass clazz) {
return IORING_OP_CONNECT; return IORING_OP_CONNECT;
} }
static jint netty_io_uring_ioringEnterGetevents(JNIEnv* env, jclass clazz) {
return IORING_ENTER_GETEVENTS;
}
static jint netty_io_uring_iosqeAsync(JNIEnv* env, jclass clazz) { static jint netty_io_uring_iosqeAsync(JNIEnv* env, jclass clazz) {
return IOSQE_ASYNC; return IOSQE_ASYNC;
} }
@ -389,6 +401,8 @@ static jint netty_io_uring_iosqeAsync(JNIEnv* env, jclass clazz) {
static const JNINativeMethod statically_referenced_fixed_method_table[] = { static const JNINativeMethod statically_referenced_fixed_method_table[] = {
{ "sockNonblock", "()I", (void *) netty_io_uring_sockNonblock }, { "sockNonblock", "()I", (void *) netty_io_uring_sockNonblock },
{ "sockCloexec", "()I", (void *) netty_io_uring_sockCloexec }, { "sockCloexec", "()I", (void *) netty_io_uring_sockCloexec },
{ "etime", "()I", (void *) netty_io_uring_etime },
{ "ecanceled", "()I", (void *) netty_io_uring_ecanceled },
{ "pollin", "()I", (void *) netty_io_uring_pollin }, { "pollin", "()I", (void *) netty_io_uring_pollin },
{ "pollout", "()I", (void *) netty_io_uring_pollout }, { "pollout", "()I", (void *) netty_io_uring_pollout },
{ "pollrdhup", "()I", (void *) netty_io_uring_pollrdhup }, { "pollrdhup", "()I", (void *) netty_io_uring_pollrdhup },
@ -400,7 +414,9 @@ static const JNINativeMethod statically_referenced_fixed_method_table[] = {
{ "ioringOpRead", "()I", (void *) netty_io_uring_ioringOpRead }, { "ioringOpRead", "()I", (void *) netty_io_uring_ioringOpRead },
{ "ioringOpWrite", "()I", (void *) netty_io_uring_ioringOpWrite }, { "ioringOpWrite", "()I", (void *) netty_io_uring_ioringOpWrite },
{ "ioringOpConnect", "()I", (void *) netty_io_uring_ioringOpConnect }, { "ioringOpConnect", "()I", (void *) netty_io_uring_ioringOpConnect },
{ "ioringEnterGetevents", "()I", (void *) netty_io_uring_ioringEnterGetevents },
{ "iosqeAsync", "()I", (void *) netty_io_uring_iosqeAsync } { "iosqeAsync", "()I", (void *) netty_io_uring_iosqeAsync }
}; };
static const jint statically_referenced_fixed_method_table_size = sizeof(statically_referenced_fixed_method_table) / sizeof(statically_referenced_fixed_method_table[0]); static const jint statically_referenced_fixed_method_table_size = sizeof(statically_referenced_fixed_method_table) / sizeof(statically_referenced_fixed_method_table[0]);

View File

@ -27,8 +27,6 @@ final class IOUringCompletionQueue {
private static final int CQE_SIZE = 16; private static final int CQE_SIZE = 16;
private static final int IORING_ENTER_GETEVENTS = 1;
//these unsigned integer pointers(shared with the kernel) will be changed by the kernel //these unsigned integer pointers(shared with the kernel) will be changed by the kernel
private final long kHeadAddress; private final long kHeadAddress;
private final long kTailAddress; private final long kTailAddress;
@ -95,7 +93,7 @@ final class IOUringCompletionQueue {
public boolean ioUringWaitCqe() { public boolean ioUringWaitCqe() {
//IORING_ENTER_GETEVENTS -> wait until an event is completely processed //IORING_ENTER_GETEVENTS -> wait until an event is completely processed
int ret = Native.ioUringEnter(ringFd, 0, 1, IORING_ENTER_GETEVENTS); int ret = Native.ioUringEnter(ringFd, 0, 1, Native.IORING_ENTER_GETEVENTS);
if (ret < 0) { if (ret < 0) {
//Todo throw exception! //Todo throw exception!
return false; return false;

View File

@ -34,9 +34,6 @@ final class IOUringEventLoop extends SingleThreadEventLoop implements
IOUringCompletionQueue.IOUringCompletionQueueCallback { IOUringCompletionQueue.IOUringCompletionQueueCallback {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(IOUringEventLoop.class); private static final InternalLogger logger = InternalLoggerFactory.getInstance(IOUringEventLoop.class);
private static final long ETIME = -62;
static final long ECANCELED = -125;
private final IntObjectMap<AbstractIOUringChannel> channels = new IntObjectHashMap<AbstractIOUringChannel>(4096); private final IntObjectMap<AbstractIOUringChannel> channels = new IntObjectHashMap<AbstractIOUringChannel>(4096);
private final RingBuffer ringBuffer; private final RingBuffer ringBuffer;
@ -176,7 +173,7 @@ final class IOUringEventLoop extends SingleThreadEventLoop implements
} else if (op == Native.IORING_OP_WRITEV || op == Native.IORING_OP_WRITE) { } else if (op == Native.IORING_OP_WRITEV || op == Native.IORING_OP_WRITE) {
handleWrite(fd, res); handleWrite(fd, res);
} else if (op == Native.IORING_OP_POLL_ADD) { } else if (op == Native.IORING_OP_POLL_ADD) {
if (res == ECANCELED) { if (res == Native.ERRNO_ECANCELED_NEGATIVE) {
logger.trace("IORING_POLL_ADD cancelled"); logger.trace("IORING_POLL_ADD cancelled");
return true; return true;
} }
@ -195,7 +192,7 @@ final class IOUringEventLoop extends SingleThreadEventLoop implements
} else if (op == Native.IORING_OP_CONNECT) { } else if (op == Native.IORING_OP_CONNECT) {
handleConnect(fd, res); handleConnect(fd, res);
} else if (op == Native.IORING_OP_TIMEOUT) { } else if (op == Native.IORING_OP_TIMEOUT) {
if (res == ETIME) { if (res == Native.ERRNO_ETIME_NEGATIVE) {
prevDeadlineNanos = NONE; prevDeadlineNanos = NONE;
} }
} }

View File

@ -28,7 +28,6 @@ import java.io.IOException;
import java.nio.channels.Selector; import java.nio.channels.Selector;
import java.util.Locale; import java.util.Locale;
final class Native { final class Native {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(Native.class); private static final InternalLogger logger = InternalLoggerFactory.getInstance(Native.class);
// Todo expose this via the EventLoopGroup constructor as well. // Todo expose this via the EventLoopGroup constructor as well.
@ -68,6 +67,8 @@ final class Native {
static final int POLLIN = NativeStaticallyReferencedJniMethods.pollin(); static final int POLLIN = NativeStaticallyReferencedJniMethods.pollin();
static final int POLLOUT = NativeStaticallyReferencedJniMethods.pollout(); static final int POLLOUT = NativeStaticallyReferencedJniMethods.pollout();
static final int POLLRDHUP = NativeStaticallyReferencedJniMethods.pollrdhup(); static final int POLLRDHUP = NativeStaticallyReferencedJniMethods.pollrdhup();
static final int ERRNO_ECANCELED_NEGATIVE = -NativeStaticallyReferencedJniMethods.ecanceled();
static final int ERRNO_ETIME_NEGATIVE = -NativeStaticallyReferencedJniMethods.etime();
static final int IORING_OP_POLL_ADD = NativeStaticallyReferencedJniMethods.ioringOpPollAdd(); static final int IORING_OP_POLL_ADD = NativeStaticallyReferencedJniMethods.ioringOpPollAdd();
static final int IORING_OP_TIMEOUT = NativeStaticallyReferencedJniMethods.ioringOpTimeout(); static final int IORING_OP_TIMEOUT = NativeStaticallyReferencedJniMethods.ioringOpTimeout();
@ -77,6 +78,7 @@ final class Native {
static final int IORING_OP_POLL_REMOVE = NativeStaticallyReferencedJniMethods.ioringOpPollRemove(); static final int IORING_OP_POLL_REMOVE = NativeStaticallyReferencedJniMethods.ioringOpPollRemove();
static final int IORING_OP_CONNECT = NativeStaticallyReferencedJniMethods.ioringOpConnect(); static final int IORING_OP_CONNECT = NativeStaticallyReferencedJniMethods.ioringOpConnect();
static final int IORING_OP_WRITEV = NativeStaticallyReferencedJniMethods.ioringOpWritev(); static final int IORING_OP_WRITEV = NativeStaticallyReferencedJniMethods.ioringOpWritev();
static final int IORING_ENTER_GETEVENTS = NativeStaticallyReferencedJniMethods.ioringEnterGetevents();
static final int IOSQE_ASYNC = NativeStaticallyReferencedJniMethods.iosqeAsync(); static final int IOSQE_ASYNC = NativeStaticallyReferencedJniMethods.iosqeAsync();
public static RingBuffer createRingBuffer(int ringSize) { public static RingBuffer createRingBuffer(int ringSize) {

View File

@ -32,6 +32,8 @@ final class NativeStaticallyReferencedJniMethods {
static native int sockNonblock(); static native int sockNonblock();
static native int sockCloexec(); static native int sockCloexec();
static native int etime();
static native int ecanceled();
static native int pollin(); static native int pollin();
static native int pollout(); static native int pollout();
static native int pollrdhup(); static native int pollrdhup();
@ -44,5 +46,6 @@ final class NativeStaticallyReferencedJniMethods {
static native int ioringOpRead(); static native int ioringOpRead();
static native int ioringOpWrite(); static native int ioringOpWrite();
static native int ioringOpConnect(); static native int ioringOpConnect();
static native int ioringEnterGetevents();
static native int iosqeAsync(); static native int iosqeAsync();
} }

View File

@ -239,7 +239,7 @@ public class NativeTest {
@Override @Override
public boolean handle(int fd, int res, long flags, int op, int mask) { public boolean handle(int fd, int res, long flags, int op, int mask) {
if (op == Native.IORING_OP_POLL_ADD) { if (op == Native.IORING_OP_POLL_ADD) {
assertEquals(IOUringEventLoop.ECANCELED, res); assertEquals(Native.ERRNO_ECANCELED_NEGATIVE, res);
} else if (op == Native.IORING_OP_POLL_REMOVE) { } else if (op == Native.IORING_OP_POLL_REMOVE) {
assertEquals(0, res); assertEquals(0, res);
} else { } else {