src/java.base/windows/classes/sun/nio/ch/PollArrayWrapper.java
author michaelm
Wed, 30 Oct 2019 11:53:07 +0000
branchunixdomainchannels
changeset 58856 b2f0339a4cad
parent 49526 cad4c844902a
permissions -rw-r--r--
removing unnecessary diffs from mainline
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2018, 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
15136
c17824042364 8005856: build-infra: Remove special handling of base module classes header generation
erikj
parents: 5506
diff changeset
    31
import java.lang.annotation.Native;
c17824042364 8005856: build-infra: Remove special handling of base module classes header generation
erikj
parents: 5506
diff changeset
    32
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Manipulates a native array of structs corresponding to (fd, events) pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * typedef struct pollfd {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *    SOCKET fd;            // 4 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *    short events;         // 2 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * } pollfd_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Konstantin Kladko
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
class PollArrayWrapper {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private AllocatedNativeObject pollArray; // The fd array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    long pollArrayAddress; // pollArrayAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
15136
c17824042364 8005856: build-infra: Remove special handling of base module classes header generation
erikj
parents: 5506
diff changeset
    51
    @Native private static final short FD_OFFSET     = 0; // fd offset in pollfd
c17824042364 8005856: build-infra: Remove special handling of base module classes header generation
erikj
parents: 5506
diff changeset
    52
    @Native private static final short EVENT_OFFSET  = 4; // events offset in pollfd
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static short SIZE_POLLFD = 8; // sizeof pollfd struct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private int size; // Size of the pollArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    PollArrayWrapper(int newSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        int allocationSize = newSize * SIZE_POLLFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        pollArray = new AllocatedNativeObject(allocationSize, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        pollArrayAddress = pollArray.address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.size = newSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // Prepare another pollfd struct for use.
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 47216
diff changeset
    66
    void putEntry(int index, SelectionKeyImpl ski) {
49526
cad4c844902a 8200583: (se) Selector clean-up, part 4
alanb
parents: 49493
diff changeset
    67
        putDescriptor(index, ski.getFDVal());
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 47216
diff changeset
    68
        putEventOps(index, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // Writes the pollfd entry from the source wrapper at the source index
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // over the entry in the target wrapper at the target index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    void replaceEntry(PollArrayWrapper source, int sindex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                     PollArrayWrapper target, int tindex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        target.putDescriptor(tindex, source.getDescriptor(sindex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        target.putEventOps(tindex, source.getEventOps(sindex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // Grows the pollfd array to new size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    void grow(int newSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        PollArrayWrapper temp = new PollArrayWrapper(newSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        for (int i = 0; i < size; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            replaceEntry(this, i, temp, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        pollArray.free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        pollArray = temp.pollArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this.size = temp.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        pollArrayAddress = pollArray.address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    void free() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        pollArray.free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // Access methods for fd structures
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    void putDescriptor(int i, int fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        pollArray.putInt(SIZE_POLLFD * i + FD_OFFSET, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    void putEventOps(int i, int event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        pollArray.putShort(SIZE_POLLFD * i + EVENT_OFFSET, (short)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    int getEventOps(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return pollArray.getShort(SIZE_POLLFD * i + EVENT_OFFSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    int getDescriptor(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
       return pollArray.getInt(SIZE_POLLFD * i + FD_OFFSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // Adds Windows wakeup socket at a given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    void addWakeupSocket(int fdVal, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        putDescriptor(index, fdVal);
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 15136
diff changeset
   114
        putEventOps(index, Net.POLLIN);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
}