comparison 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
comparison
equal deleted inserted replaced
952:669769bcdf5c 953:7db4a488fc82
62 * ProxyConnect. The connection may change for an SCEP as it is upgraded 62 * ProxyConnect. The connection may change for an SCEP as it is upgraded
63 * from HTTP to proxy connect or websocket. 63 * from HTTP to proxy connect or websocket.
64 */ 64 */
65 private volatile AsyncConnection _connection; 65 private volatile AsyncConnection _connection;
66 66
67 private static final int STATE_NEEDS_DISPATCH=-1; 67 private static final int STATE_NEEDS_DISPATCH = -1;
68 private static final int STATE_UNDISPATCHED=0; 68 private static final int STATE_UNDISPATCHED = 0;
69 private static final int STATE_DISPATCHED=1; 69 private static final int STATE_DISPATCHED = 1;
70 private int _state; 70 private int _state;
71 71
72 private boolean _onIdle; 72 private boolean _onIdle;
73 73
74 /** true if the last write operation succeed and wrote all offered bytes */ 74 /** true if the last write operation succeed and wrote all offered bytes */
124 return _connection; 124 return _connection;
125 } 125 }
126 126
127 public void setConnection(Connection connection) 127 public void setConnection(Connection connection)
128 { 128 {
129 _connection=(AsyncConnection)connection; 129 _connection = (AsyncConnection)connection;
130 } 130 }
131 131
132 /* ------------------------------------------------------------ */ 132 /* ------------------------------------------------------------ */
133 /** Called by selectSet to schedule handling 133 /** Called by selectSet to schedule handling
134 * 134 *
135 */ 135 */
136 public void schedule() 136 public synchronized void schedule()
137 { 137 {
138 synchronized (this) 138 // If there is no key, then do nothing
139 { 139 if (_key == null || !_key.isValid())
140 // If there is no key, then do nothing 140 {
141 if (_key == null || !_key.isValid()) 141 _readBlocked=false;
142 { 142 _writeBlocked=false;
143 this.notifyAll();
144 return;
145 }
146
147 // If there are threads dispatched reading and writing
148 if (_readBlocked || _writeBlocked)
149 {
150 // assert _dispatched;
151 if (_readBlocked && _key.isReadable())
143 _readBlocked=false; 152 _readBlocked=false;
153 if (_writeBlocked && _key.isWritable())
144 _writeBlocked=false; 154 _writeBlocked=false;
145 this.notifyAll(); 155
146 return; 156 // wake them up is as good as a dispatched.
147 } 157 this.notifyAll();
148 158
149 // If there are threads dispatched reading and writing 159 // we are not interested in further selecting
150 if (_readBlocked || _writeBlocked) 160 _key.interestOps(0);
151 { 161 if (_state<STATE_DISPATCHED)
152 // assert _dispatched; 162 updateKey();
153 if (_readBlocked && _key.isReadable()) 163 return;
154 _readBlocked=false; 164 }
155 if (_writeBlocked && _key.isWritable()) 165
156 _writeBlocked=false; 166 // Remove writeable op
157 167 if ((_key.readyOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE && (_key.interestOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE)
158 // wake them up is as good as a dispatched. 168 {
159 this.notifyAll();
160
161 // we are not interested in further selecting
162 _key.interestOps(0);
163 if (_state<STATE_DISPATCHED)
164 updateKey();
165 return;
166 }
167
168 // Remove writeable op 169 // Remove writeable op
169 if ((_key.readyOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE && (_key.interestOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE) 170 _interestOps = _key.interestOps() & ~SelectionKey.OP_WRITE;
170 { 171 _key.interestOps(_interestOps);
171 // Remove writeable op 172 _writable = true; // Once writable is in ops, only removed with dispatch.
172 _interestOps = _key.interestOps() & ~SelectionKey.OP_WRITE; 173 }
173 _key.interestOps(_interestOps); 174
174 _writable = true; // Once writable is in ops, only removed with dispatch. 175 // If dispatched, then deregister interest
175 } 176 if (_state>=STATE_DISPATCHED)
176 177 _key.interestOps(0);
177 // If dispatched, then deregister interest 178 else
178 if (_state>=STATE_DISPATCHED) 179 {
179 _key.interestOps(0); 180 // other wise do the dispatch
180 else 181 dispatch();
181 {
182 // other wise do the dispatch
183 dispatch();
184 }
185 } 182 }
186 } 183 }
187 184
188 public synchronized void dispatch() 185 public synchronized void dispatch()
189 { 186 {
214 */ 211 */
215 protected synchronized void undispatch() 212 protected synchronized void undispatch()
216 { 213 {
217 _state = STATE_UNDISPATCHED; 214 _state = STATE_UNDISPATCHED;
218 updateKey(); 215 updateKey();
219 // return true;
220 } 216 }
221 217
222 public void setCheckForIdle(boolean check) 218 public void setCheckForIdle(boolean check)
223 { 219 {
224 if (check) 220 if (check)
439 } 435 }
440 } 436 }
441 return true; 437 return true;
442 } 438 }
443 439
444 /* ------------------------------------------------------------ */
445 public boolean hasProgressed() 440 public boolean hasProgressed()
446 { 441 {
447 return false; 442 return false;
448 } 443 }
449 444
471 { 466 {
472 current_ops = ((_key!=null && _key.isValid())?_key.interestOps():-1); 467 current_ops = ((_key!=null && _key.isValid())?_key.interestOps():-1);
473 } 468 }
474 catch(Exception e) 469 catch(Exception e)
475 { 470 {
476 _key=null; 471 _key = null;
477 LOG.trace("",e); 472 LOG.trace("",e);
478 } 473 }
479 } 474 }
480 changed=_interestOps!=current_ops; 475 changed=_interestOps!=current_ops;
481 } 476 }
507 } 502 }
508 else 503 else
509 { 504 {
510 try 505 try
511 { 506 {
512 _key = _selectSet.getSelector().register((SelectableChannel)getChannel(),_interestOps,this); 507 _key = _selectSet.getSelector().register(sc,_interestOps,this);
513 } 508 }
514 catch (Exception e) 509 catch (Exception e)
515 { 510 {
516 LOG.trace("",e); 511 LOG.trace("",e);
517 if (_key!=null && _key.isValid()) 512 if (_key!=null && _key.isValid())
536 else 531 else
537 { 532 {
538 if (_key!=null && _key.isValid()) 533 if (_key!=null && _key.isValid())
539 _key.interestOps(0); 534 _key.interestOps(0);
540 else 535 else
541 _key=null; 536 _key = null;
542 } 537 }
543 } 538 }
544 else 539 else
545 { 540 {
546 if (_key!=null && _key.isValid()) 541 if (_key!=null && _key.isValid())