Ensure we not hold a lock while executing the tasks submitted to the EventLoop

This commit is contained in:
Norman Maurer committed 2017-07-06 19:53:22 +02:00
1 parent 7467ab6a02
commit ef4cabc4b1
1 file changed
+18 -7
+18 -7
View File
@@ -188,15 +188,26 @@ final class SelectableEventLoop : EventLoop {
}
}
// TODO: Better locking
tasksLock.lock()
// Execute all the tasks that were summited
while let task = tasks.first {
task()
// We need to ensure we process all tasks, even if a task added another task again
while true {
// TODO: Better locking
tasksLock.lock()
if tasks.isEmpty {
tasksLock.unlock()
break
}
// Make a copy of the tasks sso we can execute these while not holding the lock anymore
var tasksCopy = Array(tasks)
tasks.removeAll()
tasksLock.unlock()
let _ = tasks.removeFirst()
// Execute all the tasks that were summited
while let task = tasksCopy.first {
task()
let _ = tasksCopy.removeFirst()
}
}
tasksLock.unlock()
}
}