Mercurial Hosting > luan
view src/org/eclipse/jetty/io/nio/SaneSelector.java @ 1036:b87f97f6418a
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 03 Nov 2016 21:42:41 -0600 |
parents | a778413aefc0 |
children |
line wrap: on
line source
/* 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. */ package org.eclipse.jetty.io.nio; import java.io.IOException; import java.nio.channels.Selector; import java.nio.channels.SelectionKey; import java.nio.channels.SelectableChannel; import java.nio.channels.ClosedChannelException; import java.util.Set; public final class SaneSelector { private final Selector selector; private boolean inSelect = false; private boolean inUpdate = false; public SaneSelector() throws IOException { selector = Selector.open(); } public void close() throws IOException { selector.close(); } public boolean isOpen() { return selector.isOpen(); } public int select() throws IOException { synchronized(this) { inSelect = true; } try { while(true) { int n = selector.select(); synchronized(this) { boolean wasInUpdate = inUpdate; inUpdate = false; if( n > 0 || !wasInUpdate ) return n; } } } finally { synchronized(this) { inSelect = false; } } } public Set<SelectionKey> selectedKeys() { return selector.selectedKeys(); } public Set<SelectionKey> keys() { return selector.keys(); } public synchronized SelectionKey register(SelectableChannel channel,int ops,Object att) throws ClosedChannelException { update(); return channel.register(selector,ops,att); } public synchronized void update() { if( inSelect ) { inUpdate = true; selector.wakeup(); } } }