Path: blob/master/src/java.base/macosx/classes/sun/nio/ch/KQueue.java
41137 views
/*1* Copyright (c) 2012, 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 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_DELETE = 0x0002;61static final int EV_ONESHOT = 0x0010;62static final int EV_CLEAR = 0x0020;6364/**65* Allocates a poll array to handle up to {@code count} events.66*/67static long allocatePollArray(int count) {68return unsafe.allocateMemory(count * SIZEOF_KQUEUEEVENT);69}7071/**72* Free a poll array73*/74static void freePollArray(long address) {75unsafe.freeMemory(address);76}7778/**79* Returns kevent[i].80*/81static long getEvent(long address, int i) {82return address + (SIZEOF_KQUEUEEVENT*i);83}8485/**86* Returns the file descriptor from a kevent (assuming it is in the ident field)87*/88static int getDescriptor(long address) {89return unsafe.getInt(address + OFFSET_IDENT);90}9192static short getFilter(long address) {93return unsafe.getShort(address + OFFSET_FILTER);94}9596static short getFlags(long address) {97return unsafe.getShort(address + OFFSET_FLAGS);98}99100// -- Native methods --101102private static native int keventSize();103104private static native int identOffset();105106private static native int filterOffset();107108private static native int flagsOffset();109110static native int create() throws IOException;111112static native int register(int kqfd, int fd, int filter, int flags);113114static native int poll(int kqfd, long pollAddress, int nevents, long timeout)115throws IOException;116117static {118IOUtil.load();119}120}121122123