mirror of
https://github.com/apple/swift-nio
synced 2026-11-27 05:04:22 +00:00
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.
This commit is contained in:
1 parent
5eb21324fc
commit
f4036e2b0d
2 files changed
+30
-32
No files matched your search
+28
-32
@@ -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<ScheduledTask>()
|
||||
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
|
||||
|
||||
@@ -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<T>(_ 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)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user