diff src/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java @ 953:7db4a488fc82

simplify SelectorManager
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 12 Oct 2016 22:16:36 -0600
parents 669769bcdf5c
children 6f49b8dfffe6
line wrap: on
line diff
--- a/src/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java	Wed Oct 12 19:47:45 2016 -0600
+++ b/src/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java	Wed Oct 12 22:16:36 2016 -0600
@@ -64,9 +64,9 @@
 	 */
 	private volatile AsyncConnection _connection;
 
-	private static final int STATE_NEEDS_DISPATCH=-1;
-	private static final int STATE_UNDISPATCHED=0;
-	private static final int STATE_DISPATCHED=1;
+	private static final int STATE_NEEDS_DISPATCH = -1;
+	private static final int STATE_UNDISPATCHED = 0;
+	private static final int STATE_DISPATCHED = 1;
 	private int _state;
 	
 	private boolean _onIdle;
@@ -126,62 +126,59 @@
 
 	public void setConnection(Connection connection)
 	{
-		_connection=(AsyncConnection)connection;
+		_connection = (AsyncConnection)connection;
 	}
 
 	/* ------------------------------------------------------------ */
 	/** Called by selectSet to schedule handling
 	 *
 	 */
-	public void schedule()
+	public synchronized void schedule()
 	{
-		synchronized (this)
+		// If there is no key, then do nothing
+		if (_key == null || !_key.isValid())
 		{
-			// If there is no key, then do nothing
-			if (_key == null || !_key.isValid())
-			{
+			_readBlocked=false;
+			_writeBlocked=false;
+			this.notifyAll();
+			return;
+		}
+
+		// If there are threads dispatched reading and writing
+		if (_readBlocked || _writeBlocked)
+		{
+			// assert _dispatched;
+			if (_readBlocked && _key.isReadable())
 				_readBlocked=false;
+			if (_writeBlocked && _key.isWritable())
 				_writeBlocked=false;
-				this.notifyAll();
-				return;
-			}
 
-			// If there are threads dispatched reading and writing
-			if (_readBlocked || _writeBlocked)
-			{
-				// assert _dispatched;
-				if (_readBlocked && _key.isReadable())
-					_readBlocked=false;
-				if (_writeBlocked && _key.isWritable())
-					_writeBlocked=false;
-
-				// wake them up is as good as a dispatched.
-				this.notifyAll();
+			// wake them up is as good as a dispatched.
+			this.notifyAll();
 
-				// we are not interested in further selecting
-				_key.interestOps(0);
-				if (_state<STATE_DISPATCHED)
-					updateKey();
-				return;
-			}
+			// we are not interested in further selecting
+			_key.interestOps(0);
+			if (_state<STATE_DISPATCHED)
+				updateKey();
+			return;
+		}
 
+		// Remove writeable op
+		if ((_key.readyOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE && (_key.interestOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE)
+		{
 			// Remove writeable op
-			if ((_key.readyOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE && (_key.interestOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE)
-			{
-				// Remove writeable op
-				_interestOps = _key.interestOps() & ~SelectionKey.OP_WRITE;
-				_key.interestOps(_interestOps);
-				_writable = true; // Once writable is in ops, only removed with dispatch.
-			}
+			_interestOps = _key.interestOps() & ~SelectionKey.OP_WRITE;
+			_key.interestOps(_interestOps);
+			_writable = true; // Once writable is in ops, only removed with dispatch.
+		}
 
-			// If dispatched, then deregister interest
-			if (_state>=STATE_DISPATCHED)
-				_key.interestOps(0);
-			else
-			{
-				// other wise do the dispatch
-				dispatch();
-			}
+		// If dispatched, then deregister interest
+		if (_state>=STATE_DISPATCHED)
+			_key.interestOps(0);
+		else
+		{
+			// other wise do the dispatch
+			dispatch();
 		}
 	}
 
@@ -216,7 +213,6 @@
 	{
 		_state = STATE_UNDISPATCHED;
 		updateKey();
-//		return true;
 	}
 
 	public void setCheckForIdle(boolean check)
@@ -441,7 +437,6 @@
 		return true;
 	}
 
-	/* ------------------------------------------------------------ */
 	public boolean hasProgressed()
 	{
 		return false;
@@ -473,7 +468,7 @@
 				}
 				catch(Exception e)
 				{
-					_key=null;
+					_key = null;
 					LOG.trace("",e);
 				}
 			}
@@ -509,7 +504,7 @@
 					{
 						try
 						{
-							_key = _selectSet.getSelector().register((SelectableChannel)getChannel(),_interestOps,this);
+							_key = _selectSet.getSelector().register(sc,_interestOps,this);
 						}
 						catch (Exception e)
 						{
@@ -538,7 +533,7 @@
 				if (_key!=null && _key.isValid())
 					_key.interestOps(0);
 				else
-					_key=null;
+					_key = null;
 			}
 		}
 		else