blob: ab135c9b105a4a5c2abe5f863247b161bd6ec645 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.nio.ch;
27
28import java.io.IOException;
29import java.nio.channels.*;
30import java.nio.channels.spi.*;
31import java.util.*;
32import sun.misc.*;
33
34
35/**
36 * An implementation of Selector for Solaris.
37 */
38
39class PollSelectorImpl
40 extends AbstractPollSelectorImpl
41{
42
43 // File descriptors used for interrupt
44 private int fd0;
45 private int fd1;
46
47 // Lock for interrupt triggering and clearing
48 private Object interruptLock = new Object();
49 private boolean interruptTriggered = false;
50
51 /**
52 * Package private constructor called by factory method in
53 * the abstract superclass Selector.
54 */
55 PollSelectorImpl(SelectorProvider sp) {
56 super(sp, 1, 1);
57 int[] fdes = new int[2];
58 IOUtil.initPipe(fdes, false);
59 fd0 = fdes[0];
60 fd1 = fdes[1];
61 pollWrapper = new PollArrayWrapper(INIT_CAP);
62 pollWrapper.initInterrupt(fd0, fd1);
63 channelArray = new SelectionKeyImpl[INIT_CAP];
64 }
65
66 protected int doSelect(long timeout)
67 throws IOException
68 {
69 if (channelArray == null)
70 throw new ClosedSelectorException();
71 processDeregisterQueue();
72 try {
73 begin();
74 pollWrapper.poll(totalChannels, 0, timeout);
75 } finally {
76 end();
77 }
78 processDeregisterQueue();
79 int numKeysUpdated = updateSelectedKeys();
80 if (pollWrapper.getReventOps(0) != 0) {
81 // Clear the wakeup pipe
82 pollWrapper.putReventOps(0, 0);
83 synchronized (interruptLock) {
84 IOUtil.drain(fd0);
85 interruptTriggered = false;
86 }
87 }
88 return numKeysUpdated;
89 }
90
91 protected void implCloseInterrupt() throws IOException {
92 // prevent further wakeup
93 synchronized (interruptLock) {
94 interruptTriggered = true;
95 }
96 FileDispatcher.closeIntFD(fd0);
97 FileDispatcher.closeIntFD(fd1);
98 fd0 = -1;
99 fd1 = -1;
100 pollWrapper.release(0);
101 }
102
103 public Selector wakeup() {
104 synchronized (interruptLock) {
105 if (!interruptTriggered) {
106 pollWrapper.interrupt();
107 interruptTriggered = true;
108 }
109 }
110 return this;
111 }
112
113}