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