From f4036e2b0d0a412efa6eb4fd093a0128e2dd2076 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 27 Mar 2018 16:22:57 +0200 Subject: [PATCH] Make use of lock.withLockVoid to simplify code (#242) Motivation: We should make use of lock.withLockVoid to simplify our code and make it more robust. Modifications: - Replace manual lock() / unlock() calls by withLockVoid(...) - Mark lock.withLock* as @_inlineable to ensure we not get an allocation for the closure. Result: Code is cleaner and easier to understand. --- Sources/NIO/EventLoop.swift | 60 +++++++++++------------- Sources/NIOConcurrencyHelpers/lock.swift | 2 + 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/Sources/NIO/EventLoop.swift b/Sources/NIO/EventLoop.swift index 4606bf080..2826a20ca 100644 --- a/Sources/NIO/EventLoop.swift +++ b/Sources/NIO/EventLoop.swift @@ -385,9 +385,9 @@ internal final class SelectableEventLoop: EventLoop { },`in`) let scheduled = Scheduled(promise: promise, cancellationTask: { - self.tasksLock.lock() - self.scheduledTasks.remove(task) - self.tasksLock.unlock() + self.tasksLock.withLockVoid { + self.scheduledTasks.remove(task) + } self.wakeupSelector() }) @@ -403,10 +403,9 @@ internal final class SelectableEventLoop: EventLoop { /// Add the `ScheduledTask` to be executed. private func schedule0(_ task: ScheduledTask) { - tasksLock.lock() - scheduledTasks.push(task) - tasksLock.unlock() - + tasksLock.withLockVoid { + scheduledTasks.push(task) + } wakeupSelector() } @@ -464,13 +463,13 @@ internal final class SelectableEventLoop: EventLoop { precondition(self.inEventLoop, "tried to run the EventLoop on the wrong thread.") defer { var scheduledTasksCopy = ContiguousArray() - tasksLock.lock() - // reserve the correct capacity so we don't need to realloc later on. - scheduledTasksCopy.reserveCapacity(scheduledTasks.count) - while let sched = scheduledTasks.pop() { - scheduledTasksCopy.append(sched) + tasksLock.withLockVoid { + // reserve the correct capacity so we don't need to realloc later on. + scheduledTasksCopy.reserveCapacity(scheduledTasks.count) + while let sched = scheduledTasks.pop() { + scheduledTasksCopy.append(sched) + } } - tasksLock.unlock() // Fail all the scheduled tasks. for task in scheduledTasksCopy { @@ -498,30 +497,27 @@ internal final class SelectableEventLoop: EventLoop { // We need to ensure we process all tasks, even if a task added another task again while true { // TODO: Better locking - tasksLock.lock() - if scheduledTasks.isEmpty { - // Reset nextReadyTask to nil which means we will do a blocking select. - nextReadyTask = nil - tasksLock.unlock() - break - } + tasksLock.withLockVoid { + if !scheduledTasks.isEmpty { + // 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. + let now = DispatchTime.now() - // 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. - let now = DispatchTime.now() - - // Make a copy of the tasks so we can execute these while not holding the lock anymore - while tasksCopy.count < tasksCopy.capacity, let task = scheduledTasks.peek() { - if task.readyIn(now) <= .nanoseconds(0) { - _ = scheduledTasks.pop() - tasksCopy.append(task.task) + // Make a copy of the tasks so we can execute these while not holding the lock anymore + while tasksCopy.count < tasksCopy.capacity, let task = scheduledTasks.peek() { + if task.readyIn(now) <= .nanoseconds(0) { + _ = scheduledTasks.pop() + tasksCopy.append(task.task) + } else { + nextReadyTask = task + break + } + } } else { - nextReadyTask = task - break + // Reset nextReadyTask to nil which means we will do a blocking select. + nextReadyTask = nil } } - tasksLock.unlock() - // all pending tasks are set to occur in the future, so we can stop looping. if tasksCopy.isEmpty { break diff --git a/Sources/NIOConcurrencyHelpers/lock.swift b/Sources/NIOConcurrencyHelpers/lock.swift index 52e4fddbd..2919f0a9f 100644 --- a/Sources/NIOConcurrencyHelpers/lock.swift +++ b/Sources/NIOConcurrencyHelpers/lock.swift @@ -75,6 +75,7 @@ extension Lock { /// /// - Parameter body: The block to execute while holding the lock. /// - Returns: The value returned by the block. + @_inlineable public func withLock(_ body: () throws -> T) rethrows -> T { self.lock() defer { @@ -84,6 +85,7 @@ extension Lock { } // specialise Void return (for performance) + @_inlineable public func withLockVoid(_ body: () throws -> Void) rethrows -> Void { try self.withLock(body) }