001: /*
002: * Copyright 2001-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.nio.ch;
027:
028: import sun.misc.*;
029:
030: /**
031: * Manipulates a native array of pollfd structs on Solaris:
032: *
033: * typedef struct pollfd {
034: * int fd;
035: * short events;
036: * short revents;
037: * } pollfd_t;
038: *
039: * @author Mike McCloskey
040: * @version 1.18, 07/05/05
041: * @since 1.4
042: */
043:
044: class PollArrayWrapper extends AbstractPollArrayWrapper {
045:
046: static final short POLLCONN = POLLOUT;
047:
048: // File descriptor to write for interrupt
049: int interruptFD;
050:
051: PollArrayWrapper(int newSize) {
052: newSize = (newSize + 1) * SIZE_POLLFD;
053: pollArray = new AllocatedNativeObject(newSize, false);
054: pollArrayAddress = pollArray.address();
055: totalChannels = 1;
056: }
057:
058: void initInterrupt(int fd0, int fd1) {
059: interruptFD = fd1;
060: putDescriptor(0, fd0);
061: putEventOps(0, POLLIN);
062: putReventOps(0, 0);
063: }
064:
065: void release(int i) {
066: return;
067: }
068:
069: void free() {
070: pollArray.free();
071: }
072:
073: /**
074: * Prepare another pollfd struct for use.
075: */
076: void addEntry(SelChImpl sc) {
077: putDescriptor(totalChannels, IOUtil.fdVal(sc.getFD()));
078: putEventOps(totalChannels, 0);
079: putReventOps(totalChannels, 0);
080: totalChannels++;
081: }
082:
083: /**
084: * Writes the pollfd entry from the source wrapper at the source index
085: * over the entry in the target wrapper at the target index. The source
086: * array remains unchanged unless the source array and the target are
087: * the same array.
088: */
089: static void replaceEntry(PollArrayWrapper source, int sindex,
090: PollArrayWrapper target, int tindex) {
091: target.putDescriptor(tindex, source.getDescriptor(sindex));
092: target.putEventOps(tindex, source.getEventOps(sindex));
093: target.putReventOps(tindex, source.getReventOps(sindex));
094: }
095:
096: /**
097: * Grows the pollfd array to a size that will accommodate newSize
098: * pollfd entries. This method does no checking of the newSize
099: * to determine if it is in fact bigger than the old size: it
100: * always reallocates an array of the new size.
101: */
102: void grow(int newSize) {
103: // create new array
104: PollArrayWrapper temp = new PollArrayWrapper(newSize);
105:
106: // Copy over existing entries
107: for (int i = 0; i < totalChannels; i++)
108: replaceEntry(this , i, temp, i);
109:
110: // Swap new array into pollArray field
111: pollArray.free();
112: pollArray = temp.pollArray;
113: pollArrayAddress = pollArray.address();
114: }
115:
116: int poll(int numfds, int offset, long timeout) {
117: return poll0(pollArrayAddress + (offset * SIZE_POLLFD), numfds,
118: timeout);
119: }
120:
121: public void interrupt() {
122: interrupt(interruptFD);
123: }
124:
125: private native int poll0(long pollAddress, int numfds, long timeout);
126:
127: private static native void interrupt(int fd);
128:
129: }
|