jdk/src/windows/classes/sun/nio/ch/PollArrayWrapper.java
author dxu
Tue, 08 Jan 2013 20:37:27 +0000
changeset 15007 31d8a6072b16
parent 5506 202f599c92aa
child 15136 c17824042364
permissions -rw-r--r--
8002306: (se) Selector.open fails if invoked with thread interrupt status set [win] Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, 2002, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Manipulates a native array of structs corresponding to (fd, events) pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * typedef struct pollfd {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *    SOCKET fd;            // 4 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *    short events;         // 2 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * } pollfd_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Konstantin Kladko
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class PollArrayWrapper {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private AllocatedNativeObject pollArray; // The fd array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    long pollArrayAddress; // pollArrayAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final short FD_OFFSET     = 0; // fd offset in pollfd
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final short EVENT_OFFSET  = 4; // events offset in pollfd
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static short SIZE_POLLFD = 8; // sizeof pollfd struct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // events masks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static final short POLLIN     = AbstractPollArrayWrapper.POLLIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static final short POLLOUT    = AbstractPollArrayWrapper.POLLOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static final short POLLERR    = AbstractPollArrayWrapper.POLLERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static final short POLLHUP    = AbstractPollArrayWrapper.POLLHUP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static final short POLLNVAL   = AbstractPollArrayWrapper.POLLNVAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static final short POLLREMOVE = AbstractPollArrayWrapper.POLLREMOVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static final short POLLCONN   = 0x0002;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private int size; // Size of the pollArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    PollArrayWrapper(int newSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        int allocationSize = newSize * SIZE_POLLFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        pollArray = new AllocatedNativeObject(allocationSize, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        pollArrayAddress = pollArray.address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.size = newSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // Prepare another pollfd struct for use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    void addEntry(int index, SelectionKeyImpl ski) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        putDescriptor(index, ski.channel.getFDVal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // Writes the pollfd entry from the source wrapper at the source index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // over the entry in the target wrapper at the target index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    void replaceEntry(PollArrayWrapper source, int sindex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                     PollArrayWrapper target, int tindex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        target.putDescriptor(tindex, source.getDescriptor(sindex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        target.putEventOps(tindex, source.getEventOps(sindex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // Grows the pollfd array to new size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    void grow(int newSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        PollArrayWrapper temp = new PollArrayWrapper(newSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        for (int i = 0; i < size; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            replaceEntry(this, i, temp, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        pollArray.free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        pollArray = temp.pollArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        this.size = temp.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        pollArrayAddress = pollArray.address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    void free() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        pollArray.free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // Access methods for fd structures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    void putDescriptor(int i, int fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        pollArray.putInt(SIZE_POLLFD * i + FD_OFFSET, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    void putEventOps(int i, int event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        pollArray.putShort(SIZE_POLLFD * i + EVENT_OFFSET, (short)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    int getEventOps(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return pollArray.getShort(SIZE_POLLFD * i + EVENT_OFFSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    int getDescriptor(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
       return pollArray.getInt(SIZE_POLLFD * i + FD_OFFSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // Adds Windows wakeup socket at a given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    void addWakeupSocket(int fdVal, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        putDescriptor(index, fdVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        putEventOps(index, POLLIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}