jdk/src/solaris/classes/sun/nio/ch/EPollArrayWrapper.java
author alanb
Wed, 06 Jun 2012 17:59:29 +0100
changeset 12872 16fa902b1469
parent 5506 202f599c92aa
child 14342 8435a30053c1
permissions -rw-r--r--
7172826: (se) Selector based on the Solaris event port mechanism Reviewed-by: coffeys, chegar
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: 4183
diff changeset
     2
 * Copyright (c) 2005, 2009, 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: 4183
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: 4183
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: 4183
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4183
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4183
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 java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.HashSet;
4183
0e342cecb3a9 6897993: (se) Close or cancel performance issue when number of pending updates is high (lnx)
martin
parents: 2433
diff changeset
    31
import java.util.Iterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Manipulates a native array of epoll_event structs on Linux:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * typedef union epoll_data {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *     void *ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *     int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *     __uint32_t u32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *     __uint64_t u64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *  } epoll_data_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * struct epoll_event {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *     __uint32_t events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *     epoll_data_t data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * The system call to wait for I/O events is epoll_wait(2). It populates an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * array of epoll_event structures that are passed to the call. The data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * member of the epoll_event structure contains the same data as was set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * when the file descriptor was registered to epoll via epoll_ctl(2). In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * this implementation we set data.fd to be the file descriptor that we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * register. That way, we have the file descriptor available when we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * process the events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * All file descriptors registered with epoll have the POLLHUP and POLLERR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * events enabled even when registered with an event set of 0. To ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * that epoll_wait doesn't poll an idle file descriptor when the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * connection is closed or reset then its registration is deleted from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * epoll (it will be re-added again if the event set is changed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
class EPollArrayWrapper {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // EPOLL_EVENTS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static final int EPOLLIN      = 0x001;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // opcodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static final int EPOLL_CTL_ADD      = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static final int EPOLL_CTL_DEL      = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static final int EPOLL_CTL_MOD      = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // Miscellaneous constants
1142
e01e390f6551 6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents: 2
diff changeset
    73
    static final int SIZE_EPOLLEVENT  = sizeofEPollEvent();
e01e390f6551 6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents: 2
diff changeset
    74
    static final int EVENT_OFFSET     = 0;
e01e390f6551 6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents: 2
diff changeset
    75
    static final int DATA_OFFSET      = offsetofData();
e01e390f6551 6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents: 2
diff changeset
    76
    static final int FD_OFFSET        = DATA_OFFSET;
12872
16fa902b1469 7172826: (se) Selector based on the Solaris event port mechanism
alanb
parents: 5506
diff changeset
    77
    static final int NUM_EPOLLEVENTS  = Math.min(IOUtil.fdLimit(), 8192);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // Base address of the native pollArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private final long pollArrayAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
    82
    // Set of "idle" channels
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
    83
    private final HashSet<SelChImpl> idleSet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    EPollArrayWrapper() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // creates the epoll file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        epfd = epollCreate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // the epoll_event array passed to epoll_wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        int allocationSize = NUM_EPOLLEVENTS * SIZE_EPOLLEVENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        pollArray = new AllocatedNativeObject(allocationSize, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        pollArrayAddress = pollArray.address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        for (int i=0; i<NUM_EPOLLEVENTS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            putEventOps(i, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            putData(i, 0L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // create idle set
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   100
        idleSet = new HashSet<SelChImpl>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // Used to update file description registrations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static class Updator {
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   105
        SelChImpl channel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        int opcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int events;
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   108
        Updator(SelChImpl channel, int opcode, int events) {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   109
            this.channel = channel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            this.opcode = opcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            this.events = events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   113
        Updator(SelChImpl channel, int opcode) {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   114
            this(channel, opcode, 0);
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   115
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private LinkedList<Updator> updateList = new LinkedList<Updator>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // The epoll_event array for results from epoll_wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private AllocatedNativeObject pollArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    // The fd of the epoll driver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    final int epfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    // The fd of the interrupt line going out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    int outgoingInterruptFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    // The fd of the interrupt line coming in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    int incomingInterruptFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    // The index of the interrupt FD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    int interruptedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // Number of updated pollfd entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    int updated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    void initInterrupt(int fd0, int fd1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        outgoingInterruptFD = fd1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        incomingInterruptFD = fd0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        epollCtl(epfd, EPOLL_CTL_ADD, fd0, EPOLLIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    void putEventOps(int i, int event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int offset = SIZE_EPOLLEVENT * i + EVENT_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        pollArray.putInt(offset, event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    void putData(int i, long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        int offset = SIZE_EPOLLEVENT * i + DATA_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        pollArray.putLong(offset, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    void putDescriptor(int i, int fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        int offset = SIZE_EPOLLEVENT * i + FD_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        pollArray.putInt(offset, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    int getEventOps(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        int offset = SIZE_EPOLLEVENT * i + EVENT_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return pollArray.getInt(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    int getDescriptor(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        int offset = SIZE_EPOLLEVENT * i + FD_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return pollArray.getInt(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   170
     * Update the events for a given channel.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   172
    void setInterest(SelChImpl channel, int mask) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        synchronized (updateList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            // if the previous pending operation is to add this file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            // to epoll then update its event set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (updateList.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                Updator last = updateList.getLast();
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   178
                if (last.channel == channel && last.opcode == EPOLL_CTL_ADD) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    last.events = mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            // update existing registration
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   185
            updateList.add(new Updator(channel, EPOLL_CTL_MOD, mask));
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   186
        }
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   187
    }
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   188
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   189
    /**
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   190
     * Add a channel's file descriptor to epoll
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   191
     */
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   192
    void add(SelChImpl channel) {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   193
        synchronized (updateList) {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   194
            updateList.add(new Updator(channel, EPOLL_CTL_ADD));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   199
     * Remove a channel's file descriptor from epoll
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   201
    void release(SelChImpl channel) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        synchronized (updateList) {
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   203
            // flush any pending updates
4183
0e342cecb3a9 6897993: (se) Close or cancel performance issue when number of pending updates is high (lnx)
martin
parents: 2433
diff changeset
   204
            for (Iterator<Updator> it = updateList.iterator(); it.hasNext();) {
0e342cecb3a9 6897993: (se) Close or cancel performance issue when number of pending updates is high (lnx)
martin
parents: 2433
diff changeset
   205
                if (it.next().channel == channel) {
0e342cecb3a9 6897993: (se) Close or cancel performance issue when number of pending updates is high (lnx)
martin
parents: 2433
diff changeset
   206
                    it.remove();
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   207
                }
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   208
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   210
            // remove from the idle set (if present)
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   211
            idleSet.remove(channel);
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   212
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   213
            // remove from epoll (if registered)
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   214
            epollCtl(epfd, EPOLL_CTL_DEL, channel.getFDVal(), 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Close epoll file descriptor and free poll array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    void closeEPollFD() throws IOException {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   222
        FileDispatcherImpl.closeIntFD(epfd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        pollArray.free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    int poll(long timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        updateRegistrations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        updated = epollWait(pollArrayAddress, NUM_EPOLLEVENTS, timeout, epfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        for (int i=0; i<updated; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (getDescriptor(i) == incomingInterruptFD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                interruptedIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                interrupted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return updated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Update the pending registrations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    void updateRegistrations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        synchronized (updateList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            Updator u = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            while ((u = updateList.poll()) != null) {
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   246
                SelChImpl ch = u.channel;
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   247
                if (!ch.isOpen())
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   248
                    continue;
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   249
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   250
                // if the events are 0 then file descriptor is put into "idle
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   251
                // set" to prevent it being polled
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   252
                if (u.events == 0) {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   253
                    boolean added = idleSet.add(u.channel);
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   254
                    // if added to idle set then remove from epoll if registered
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   255
                    if (added && (u.opcode == EPOLL_CTL_MOD))
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   256
                        epollCtl(epfd, EPOLL_CTL_DEL, ch.getFDVal(), 0);
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   257
                } else {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   258
                    // events are specified. If file descriptor was in idle set
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   259
                    // it must be re-registered (by converting opcode to ADD)
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   260
                    boolean idle = false;
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   261
                    if (!idleSet.isEmpty())
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   262
                        idle = idleSet.remove(u.channel);
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   263
                    int opcode = (idle) ? EPOLL_CTL_ADD : u.opcode;
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   264
                    epollCtl(epfd, opcode, ch.getFDVal(), u.events);
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents: 2057
diff changeset
   265
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    // interrupt support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    boolean interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public void interrupt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        interrupt(outgoingInterruptFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public int interruptedIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return interruptedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    boolean interrupted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return interrupted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    void clearInterrupted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private native int epollCreate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    private native void epollCtl(int epfd, int opcode, int fd, int events);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    private native int epollWait(long pollAddress, int numfds, long timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                 int epfd) throws IOException;
1142
e01e390f6551 6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents: 2
diff changeset
   297
    private static native int sizeofEPollEvent();
e01e390f6551 6728542: (se) epoll based SelectorProvider should be portable to platforms other than x86 and x64
alanb
parents: 2
diff changeset
   298
    private static native int offsetofData();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    private static native void interrupt(int fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    private static native void init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
}