Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of lock.withLockVoid to simplify code #242

Merged
merged 2 commits into from Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 29 additions & 33 deletions Sources/NIO/EventLoop.swift
Expand Up @@ -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()
})

Expand All @@ -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()
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

// 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)
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()

// 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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The break is not needed anymore as tasksCopy.isEmpty will be true and so we break out the while loop.

// 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
Expand Down
2 changes: 2 additions & 0 deletions Sources/NIOConcurrencyHelpers/lock.swift
Expand Up @@ -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 {
Expand All @@ -84,6 +85,7 @@ extension Lock {
}

// specialise Void return (for performance)
@_inlineable
public func withLockVoid(_ body: () throws -> Void) rethrows -> Void {
try self.withLock(body)
}
Expand Down