Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/nio/ch/KQueue.java
32288 views
/*1* Copyright (c) 2012, 2013, 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 sun.misc.Unsafe;2930/**31* Provides access to the BSD kqueue facility.32*/3334class KQueue {35private KQueue() { }3637private static final Unsafe unsafe = Unsafe.getUnsafe();3839/**40* struct kevent {41* uintptr_t ident; // identifier for this event, usually the fd42* int16_t filter; // filter for event43* uint16_t flags; // general flags44* uint32_t fflags; // filter-specific flags45* intptr_t data; // filter-specific data46* void *udata; // opaque user data identifier47* };48*/49private static final int SIZEOF_KQUEUEEVENT = keventSize();50private static final int OFFSET_IDENT = identOffset();51private static final int OFFSET_FILTER = filterOffset();52private static final int OFFSET_FLAGS = flagsOffset();5354// filters55static final int EVFILT_READ = -1;56static final int EVFILT_WRITE = -2;5758// flags59static final int EV_ADD = 0x0001;60static final int EV_ONESHOT = 0x0010;61static final int EV_CLEAR = 0x0020;6263/**64* Allocates a poll array to handle up to {@code count} events.65*/66static long allocatePollArray(int count) {67return unsafe.allocateMemory(count * SIZEOF_KQUEUEEVENT);68}6970/**71* Free a poll array72*/73static void freePollArray(long address) {74unsafe.freeMemory(address);75}7677/**78* Returns kevent[i].79*/80static long getEvent(long address, int i) {81return address + (SIZEOF_KQUEUEEVENT*i);82}8384/**85* Returns the file descriptor from a kevent (assuming to be in ident field)86*/87static int getDescriptor(long address) {88return unsafe.getInt(address + OFFSET_IDENT);89}9091static int getFilter(long address) {92return unsafe.getShort(address + OFFSET_FILTER);93}9495static int getFlags(long address) {96return unsafe.getShort(address + OFFSET_FLAGS);97}9899// -- Native methods --100101private static native int keventSize();102103private static native int identOffset();104105private static native int filterOffset();106107private static native int flagsOffset();108109static native int kqueue() throws IOException;110111static native int keventRegister(int kqpfd, int fd, int filter, int flags);112113static native int keventPoll(int kqpfd, long pollAddress, int nevents)114throws IOException;115116static {117IOUtil.load();118}119}120121122