Path: blob/master/src/java.base/windows/classes/sun/nio/ch/WEPoll.java
41139 views
/*1* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.ch;2627import java.io.IOException;28import jdk.internal.misc.Unsafe;2930/**31* Provides access to wepoll.32*/33class WEPoll {34private static final Unsafe UNSAFE = Unsafe.getUnsafe();35private static final int ADDRESS_SIZE = UNSAFE.addressSize();3637private WEPoll() { }3839/**40* typedef union epoll_data {41* void *ptr;42* int fd;43* uint32_t u32;44* uint64_t u64;45* SOCKET sock; // Windows specific46* HANDLE hnd; // Windows specific47* } epoll_data_t;48*49* struct epoll_event {50* uint32_t events;51* epoll_data_t data;52* }53*/54private static final int SIZEOF_EPOLLEVENT = eventSize();55private static final int OFFSETOF_EVENTS = eventsOffset();56private static final int OFFSETOF_SOCK = dataOffset();5758// opcodes59static final int EPOLL_CTL_ADD = 1;60static final int EPOLL_CTL_MOD = 2;61static final int EPOLL_CTL_DEL = 3;6263// events64static final int EPOLLIN = (1 << 0);65static final int EPOLLPRI = (1 << 1);66static final int EPOLLOUT = (1 << 2);67static final int EPOLLERR = (1 << 3);68static final int EPOLLHUP = (1 << 4);6970// flags71static final int EPOLLONESHOT = (1 << 31);7273/**74* Allocates a poll array to handle up to {@code count} events.75*/76static long allocatePollArray(int count) {77long size = (long) count * SIZEOF_EPOLLEVENT;78long base = UNSAFE.allocateMemory(size);79UNSAFE.setMemory(base, size, (byte) 0);80return base;81}8283/**84* Free a poll array85*/86static void freePollArray(long address) {87UNSAFE.freeMemory(address);88}8990/**91* Returns event[i];92*/93static long getEvent(long address, int i) {94return address + (SIZEOF_EPOLLEVENT*i);95}9697/**98* Returns event->data.socket99*/100static long getSocket(long eventAddress) {101if (ADDRESS_SIZE == 8) {102return UNSAFE.getLong(eventAddress + OFFSETOF_SOCK);103} else {104return UNSAFE.getInt(eventAddress + OFFSETOF_SOCK);105}106}107108/**109* Return event->data.socket as an int file descriptor110*/111static int getDescriptor(long eventAddress) {112long s = getSocket(eventAddress);113int fd = (int) s;114assert ((long) fd) == s;115return fd;116}117118/**119* Returns event->events120*/121static int getEvents(long eventAddress) {122return UNSAFE.getInt(eventAddress + OFFSETOF_EVENTS);123}124125// -- Native methods --126127private static native int eventSize();128129private static native int eventsOffset();130131private static native int dataOffset();132133static native long create() throws IOException;134135static native int ctl(long h, int opcode, long s, int events);136137static native int wait(long h, long pollAddress, int numfds, int timeout)138throws IOException;139140static native void close(long h);141142static {143IOUtil.load();144}145}146147148