Move check for event loop shutdown into taskQueue (#127)

Motivation:

The event loop state is not protected for access on different threads.
This means it must only be accessed from the task queue.

Modifications:

Move check for event loop shutdown into taskQueue

Result:

Event Loop state only accessed from the task queue.
This commit is contained in:
Peter Adams 2021-08-19 10:57:47 +01:00 committed by GitHub
parent 5fd5ba4d3e
commit 9571a61d23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 5 deletions

View File

@ -121,16 +121,15 @@ internal class NIOTSEventLoop: QoSEventLoop {
public func scheduleTask<T>(deadline: NIODeadline, qos: DispatchQoS, _ task: @escaping () throws -> T) -> Scheduled<T> {
let p: EventLoopPromise<T> = self.makePromise()
guard self.state != .closed else {
p.fail(EventLoopError.shutdown)
return Scheduled(promise: p, cancellationTask: { } )
}
// Dispatch support for cancellation exists at the work-item level, so we explicitly create one here.
// We set the QoS on this work item and explicitly enforce it when the block runs.
let timerSource = DispatchSource.makeTimerSource(queue: self.taskQueue)
timerSource.schedule(deadline: DispatchTime(uptimeNanoseconds: deadline.uptimeNanoseconds))
timerSource.setEventHandler(qos: qos, flags: .enforceQoS) {
guard self.state != .closed else {
p.fail(EventLoopError.shutdown)
return
}
do {
p.succeed(try task())
} catch {