Path: blob/master/src/java.base/linux/classes/sun/nio/ch/EPoll.java
40948 views
/*1* Copyright (c) 2008, 2018, 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 the Linux epoll facility.32*/3334class EPoll {35private EPoll() { }3637private static final Unsafe unsafe = Unsafe.getUnsafe();3839/**40* typedef union epoll_data {41* void *ptr;42* int fd;43* __uint32_t u32;44* __uint64_t u64;45* } epoll_data_t;46*47* struct epoll_event {48* __uint32_t events;49* epoll_data_t data;50* }51*/52private static final int SIZEOF_EPOLLEVENT = eventSize();53private static final int OFFSETOF_EVENTS = eventsOffset();54private static final int OFFSETOF_FD = dataOffset();5556// opcodes57static final int EPOLL_CTL_ADD = 1;58static final int EPOLL_CTL_DEL = 2;59static final int EPOLL_CTL_MOD = 3;6061// events62static final int EPOLLIN = 0x1;63static final int EPOLLOUT = 0x4;6465// flags66static final int EPOLLONESHOT = (1 << 30);6768/**69* Allocates a poll array to handle up to {@code count} events.70*/71static long allocatePollArray(int count) {72return unsafe.allocateMemory(count * SIZEOF_EPOLLEVENT);73}7475/**76* Free a poll array77*/78static void freePollArray(long address) {79unsafe.freeMemory(address);80}8182/**83* Returns event[i];84*/85static long getEvent(long address, int i) {86return address + (SIZEOF_EPOLLEVENT*i);87}8889/**90* Returns event->data.fd91*/92static int getDescriptor(long eventAddress) {93return unsafe.getInt(eventAddress + OFFSETOF_FD);94}9596/**97* Returns event->events98*/99static int getEvents(long eventAddress) {100return unsafe.getInt(eventAddress + OFFSETOF_EVENTS);101}102103// -- Native methods --104105private static native int eventSize();106107private static native int eventsOffset();108109private static native int dataOffset();110111static native int create() throws IOException;112113static native int ctl(int epfd, int opcode, int fd, int events);114115static native int wait(int epfd, long pollAddress, int numfds, int timeout)116throws IOException;117118static {119IOUtil.load();120}121}122123124