jdk/src/solaris/classes/sun/nio/ch/PollArrayWrapper.java
author alanb
Wed, 28 Aug 2013 15:50:03 +0100
changeset 19607 bee007586d06
parent 14342 8435a30053c1
child 22604 9b394795e216
permissions -rw-r--r--
8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 11823
diff changeset
     2
 * Copyright (c) 2001, 2012, 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
package sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.misc.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
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 pollfd structs on Solaris:
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
 *    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *    short events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *    short revents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * } pollfd_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 5506
diff changeset
    44
public class PollArrayWrapper extends AbstractPollArrayWrapper {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 5506
diff changeset
    46
    public static final short POLLCONN = POLLOUT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // File descriptor to write for interrupt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    int interruptFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    PollArrayWrapper(int newSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        newSize = (newSize + 1) * SIZE_POLLFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        pollArray = new AllocatedNativeObject(newSize, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        pollArrayAddress = pollArray.address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        totalChannels = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    void initInterrupt(int fd0, int fd1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        interruptFD = fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        putDescriptor(0, fd0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        putEventOps(0, POLLIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        putReventOps(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    void release(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    void free() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        pollArray.free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Prepare another pollfd struct for use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    void addEntry(SelChImpl sc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        putDescriptor(totalChannels, IOUtil.fdVal(sc.getFD()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        putEventOps(totalChannels, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        putReventOps(totalChannels, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        totalChannels++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Writes the pollfd entry from the source wrapper at the source index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * over the entry in the target wrapper at the target index. The source
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * array remains unchanged unless the source array and the target are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * the same array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static void replaceEntry(PollArrayWrapper source, int sindex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                      PollArrayWrapper target, int tindex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        target.putDescriptor(tindex, source.getDescriptor(sindex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        target.putEventOps(tindex, source.getEventOps(sindex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        target.putReventOps(tindex, source.getReventOps(sindex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Grows the pollfd array to a size that will accommodate newSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * pollfd entries. This method does no checking of the newSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * to determine if it is in fact bigger than the old size: it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * always reallocates an array of the new size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    void grow(int newSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // create new array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        PollArrayWrapper temp = new PollArrayWrapper(newSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // Copy over existing entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        for (int i=0; i<totalChannels; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            replaceEntry(this, i, temp, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // Swap new array into pollArray field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        pollArray.free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        pollArray = temp.pollArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        pollArrayAddress = pollArray.address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    int poll(int numfds, int offset, long timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return poll0(pollArrayAddress + (offset * SIZE_POLLFD),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                     numfds, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public void interrupt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        interrupt(interruptFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private native int poll0(long pollAddress, int numfds, long timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private static native void interrupt(int fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
19607
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 14342
diff changeset
   129
    static {
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 14342
diff changeset
   130
        IOUtil.load();
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 14342
diff changeset
   131
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}