4700N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 4700N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4700N/A * This code is free software; you can redistribute it and/or modify it 4700N/A * under the terms of the GNU General Public License version 2 only, as 4700N/A * published by the Free Software Foundation. Oracle designates this 4700N/A * particular file as subject to the "Classpath" exception as provided 4700N/A * by Oracle in the LICENSE file that accompanied this code. 4700N/A * This code is distributed in the hope that it will be useful, but WITHOUT 4700N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4700N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4700N/A * version 2 for more details (a copy is included in the LICENSE file that 4700N/A * You should have received a copy of the GNU General Public License version 4700N/A * 2 along with this work; if not, write to the Free Software Foundation, 4700N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4700N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4700N/A * or visit www.oracle.com if you need additional information or have any 4700N/A * AsynchronousChannelGroup implementation based on the BSD kqueue facility. 4700N/A // maximum number of events to poll at a time 4700N/A // socket pair used for wakeup 4700N/A // number of wakeups pending 4700N/A // address of the poll array passed to kqueue_wait 4700N/A // encapsulates an event for a channel 4700N/A // queue of events for cases that a polling thread dequeues more than one 4700N/A // create socket pair for wakeup mechanism 4700N/A // register one end with kqueue 4700N/A // create the queue and offer the special event to ensure that the first 4700N/A // write byte to socketpair to force wakeup 4700N/A * If no tasks are running then just release resources; otherwise 4700N/A * write to the one end of the socketpair to wakeup any polling threads. 4700N/A // send interrupt to each thread 4700N/A // invoked by clients to register a file descriptor 4700N/A // We use a separate filter for read and write events. 4700N/A // TBD: Measure cost of EV_ONESHOT vs. EV_CLEAR, either will do here. 4700N/A * Task to process events from kqueue and dispatch to the channel's 4700N/A * Events are retreived from kqueue in batch and offered to a BlockingQueue 4700N/A * where they are consumed by handler threads. A special "NEED_TO_POLL" 4700N/A * event is used to signal one consumer to re-poll when all events have 4700N/A * 'n' events have been read. Here we map them to their 4700N/A * corresponding channel in batch and queue n-1 so that 4700N/A * they can be handled by other handler threads. The last 4700N/A * event is handled by this thread (and so is not queued). 4700N/A // no more wakeups so drain pipe 4700N/A // queue special event if there are more events 4700N/A // n-1 events are queued; This thread handles 4700N/A // the last one except for the wakeup 4700N/A // to ensure that some thread will poll when all events have 4700N/A // no events and this thread has been "selected" to 4700N/A // handle wakeup to execute task or shutdown 4700N/A // last handler to exit when shutdown releases resources