|
| 1 | +// |
| 2 | +// Java Does USB |
| 3 | +// Copyright (c) 2022 Manuel Bleichenbacher |
| 4 | +// Licensed under MIT License |
| 5 | +// https://opensource.org/licenses/MIT |
| 6 | +// |
| 7 | + |
| 8 | +package net.codecrete.usb.linux; |
| 9 | + |
| 10 | +import net.codecrete.usb.linux.gen.epoll.epoll_event; |
| 11 | +import net.codecrete.usb.linux.gen.errno.errno; |
| 12 | + |
| 13 | +import java.lang.foreign.*; |
| 14 | +import java.lang.invoke.MethodHandle; |
| 15 | +import java.lang.invoke.VarHandle; |
| 16 | + |
| 17 | +import static java.lang.foreign.ValueLayout.ADDRESS; |
| 18 | +import static java.lang.foreign.ValueLayout.JAVA_INT; |
| 19 | +import static net.codecrete.usb.linux.Linux.allocateErrorState; |
| 20 | +import static net.codecrete.usb.linux.LinuxUsbException.throwLastError; |
| 21 | +import static net.codecrete.usb.linux.gen.epoll.epoll.*; |
| 22 | + |
| 23 | +@SuppressWarnings({"OptionalGetWithoutIsPresent", "SameParameterValue", "java:S100"}) |
| 24 | +public class EPoll { |
| 25 | + private EPoll() {} |
| 26 | + |
| 27 | + private static final Linker linker = Linker.nativeLinker(); |
| 28 | + |
| 29 | + private static final FunctionDescriptor epoll_create$FUNC = FunctionDescriptor.of(JAVA_INT, JAVA_INT); |
| 30 | + private static final MethodHandle epoll_create$MH = linker.downcallHandle(linker.defaultLookup().find( |
| 31 | + "epoll_create").get(), epoll_create$FUNC, Linux.ERRNO_STATE); |
| 32 | + |
| 33 | + private static final FunctionDescriptor epoll_ctl$FUNC = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS); |
| 34 | + private static final MethodHandle epoll_ctl$MH = linker.downcallHandle(linker.defaultLookup().find( |
| 35 | + "epoll_ctl").get(), epoll_ctl$FUNC, Linux.ERRNO_STATE); |
| 36 | + |
| 37 | + private static final FunctionDescriptor epoll_wait$FUNC = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, JAVA_INT, JAVA_INT); |
| 38 | + private static final MethodHandle epoll_wait$MH = linker.downcallHandle(linker.defaultLookup().find( |
| 39 | + "epoll_wait").get(), epoll_wait$FUNC, Linux.ERRNO_STATE); |
| 40 | + |
| 41 | + private static final VarHandle epoll_event_data_fd$VH = epoll_event.$LAYOUT().varHandle( |
| 42 | + MemoryLayout.PathElement.groupElement("data"), |
| 43 | + MemoryLayout.PathElement.groupElement("fd") |
| 44 | + ); |
| 45 | + |
| 46 | + static int epoll_create(int size, MemorySegment errno) { |
| 47 | + try { |
| 48 | + return (int) epoll_create$MH.invokeExact(errno, size); |
| 49 | + } catch (Throwable ex) { |
| 50 | + throw new AssertionError(ex); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private static int epoll_ctl(int epfd, int op, int fd, MemorySegment event, MemorySegment errno) { |
| 55 | + try { |
| 56 | + return (int) epoll_ctl$MH.invokeExact(errno, epfd, op, fd, event); |
| 57 | + } catch (Throwable ex) { |
| 58 | + throw new AssertionError(ex); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + static int epoll_wait(int epfd, MemorySegment events, int maxevent, int timeout, MemorySegment errno) { |
| 63 | + try { |
| 64 | + return (int) epoll_wait$MH.invokeExact(errno, epfd, events, maxevent, timeout); |
| 65 | + } catch (Throwable ex) { |
| 66 | + throw new AssertionError(ex); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + static void addFileDescriptor(int epfd, int op, int fd) { |
| 71 | + try (var arena = Arena.ofConfined()) { |
| 72 | + var errorState = allocateErrorState(arena); |
| 73 | + |
| 74 | + var event = arena.allocate(epoll_event.$LAYOUT()); |
| 75 | + epoll_event.events$set(event, op); |
| 76 | + epoll_event_data_fd$VH.set(event, fd); |
| 77 | + var ret = epoll_ctl(epfd, EPOLL_CTL_ADD(), fd, event, errorState); |
| 78 | + if (ret < 0) |
| 79 | + throwLastError(errorState, "internal error (epoll_ctl_add)"); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + static void removeFileDescriptor(int epfd, int fd) { |
| 84 | + try (var arena = Arena.ofConfined()) { |
| 85 | + var errorState = allocateErrorState(arena); |
| 86 | + |
| 87 | + var event = arena.allocate(epoll_event.$LAYOUT()); |
| 88 | + epoll_event.events$set(event, 0); |
| 89 | + epoll_event_data_fd$VH.set(event, fd); |
| 90 | + var ret = epoll_ctl(epfd, EPOLL_CTL_DEL(), fd, event, errorState); |
| 91 | + if (ret < 0) { |
| 92 | + var err = Linux.getErrno(errorState); |
| 93 | + if (err != errno.ENOENT()) |
| 94 | + throwLastError(errorState, "internal error (epoll_ctl_del)"); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments