You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Motivation:
Usually, you spawn a new MultiThreadedEventLoopGroup to run NIO
programs. But if you're a very simple command line utility, that may not
be necessary, so why not just re-use the main thread?
Modifications:
Allow taking over threads and making them EventLoops.
Result:
Fewer threads if you want.
Co-authored-by: Cory Benfield <[email protected]>
// Block until there are events to handle or the selector was woken up
374
386
/* for macOS: in case any calls we make to Foundation put objects into an autoreleasepool */
375
387
trywithAutoReleasePool{
376
-
try _selector.whenReady(strategy:currentSelectorStrategy(nextReadyTask: nextReadyTask)){ ev in
388
+
tryself._selector.whenReady(strategy:currentSelectorStrategy(nextReadyTask: nextReadyTask)){ ev in
377
389
switch ev.registration {
378
390
case.serverSocketChannel(let chan, _):
379
391
self.handleEvent(ev.io, channel: chan)
@@ -397,7 +409,7 @@ internal final class SelectableEventLoop: EventLoop {
397
409
// We need to ensure we process all tasks, even if a task added another task again
398
410
whiletrue{
399
411
// TODO: Better locking
400
-
_tasksLock.withLockVoid{
412
+
self._tasksLock.withLockVoid{
401
413
if !self._scheduledTasks.isEmpty {
402
414
// We only fetch the time one time as this may be expensive and is generally good enough as if we miss anything we will just do a non-blocking select again anyway.
403
415
letnow:NIODeadline=.now()
@@ -406,7 +418,7 @@ internal final class SelectableEventLoop: EventLoop {
406
418
while tasksCopy.count < tasksCopy.capacity,let task =self._scheduledTasks.peek(){
407
419
if task.readyIn(now)<=.nanoseconds(0){
408
420
self._scheduledTasks.pop()
409
-
tasksCopy.append(task.task)
421
+
self.tasksCopy.append(task.task)
410
422
}else{
411
423
nextReadyTask = task
412
424
break
@@ -419,19 +431,19 @@ internal final class SelectableEventLoop: EventLoop {
419
431
}
420
432
421
433
// all pending tasks are set to occur in the future, so we can stop looping.
422
-
if tasksCopy.isEmpty {
434
+
ifself.tasksCopy.isEmpty {
423
435
break
424
436
}
425
437
426
438
// Execute all the tasks that were summited
427
-
fortaskin tasksCopy {
439
+
fortaskinself.tasksCopy {
428
440
/* for macOS: in case any calls we make to Foundation put objects into an autoreleasepool */
429
441
withAutoReleasePool{
430
442
task()
431
443
}
432
444
}
433
445
// Drop everything (but keep the capacity) so we can fill it again on the next iteration.
434
-
tasksCopy.removeAll(keepingCapacity:true)
446
+
self.tasksCopy.removeAll(keepingCapacity:true)
435
447
}
436
448
}
437
449
@@ -490,7 +502,9 @@ internal final class SelectableEventLoop: EventLoop {
490
502
}
491
503
}
492
504
493
-
internalfunc syncFinaliseClose(){
505
+
internalfunc syncFinaliseClose(joinThread:Bool){
506
+
// This may not be true in the future but today we need to join all ELs that can't be shut down individually.
0 commit comments