annotate src/org/eclipse/jetty/io/nio/SaneSelector.java @ 972:5ee36654b383

simplify AbstractHttpConnection
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 15 Oct 2016 22:42:05 -0600
parents a778413aefc0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
950
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 /*
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 Thread synchronization in java.nio.channels.Selector is completely fucked up, unsurprisingly since NIO was developed in this demented century. This class works around the modern insanity.
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 */
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 package org.eclipse.jetty.io.nio;
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import java.io.IOException;
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 import java.nio.channels.Selector;
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 import java.nio.channels.SelectionKey;
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 import java.nio.channels.SelectableChannel;
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 import java.nio.channels.ClosedChannelException;
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 import java.util.Set;
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 public final class SaneSelector {
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 private final Selector selector;
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 private boolean inSelect = false;
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 private boolean inUpdate = false;
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 public SaneSelector() throws IOException {
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 selector = Selector.open();
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 }
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 public void close() throws IOException {
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 selector.close();
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 }
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 public boolean isOpen() {
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 return selector.isOpen();
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 }
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 public int select() throws IOException {
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 synchronized(this) {
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 inSelect = true;
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 }
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 try {
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 while(true) {
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 int n = selector.select();
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 synchronized(this) {
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 boolean wasInUpdate = inUpdate;
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 inUpdate = false;
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 if( n > 0 || !wasInUpdate )
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 return n;
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 }
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 }
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 } finally {
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 synchronized(this) {
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 inSelect = false;
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 }
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 }
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 }
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 public Set<SelectionKey> selectedKeys() {
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 return selector.selectedKeys();
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 }
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 public Set<SelectionKey> keys() {
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 return selector.keys();
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 }
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 public synchronized SelectionKey register(SelectableChannel channel,int ops,Object att) throws ClosedChannelException {
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 update();
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 return channel.register(selector,ops,att);
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 }
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 public synchronized void update() {
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 if( inSelect ) {
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 inUpdate = true;
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 selector.wakeup();
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 }
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 }
a778413aefc0 add SaneSelector
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 }