Skip to content

Commit

Permalink
Add throw keyword argument in waitany
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkn committed Mar 6, 2024
1 parent 0501a1a commit dace55a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
8 changes: 4 additions & 4 deletions base/task.jl
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,10 @@ function wait(t::Task)
end

# Wait multiple tasks
waitany(tasks) = _wait_multiple(tasks)
waitall(tasks; failfast=false, throw=false) = _wait_multiple(tasks, true, failfast, throw)
waitany(tasks; throw=false) = _wait_multiple(tasks, throw)
waitall(tasks; failfast=false, throw=false) = _wait_multiple(tasks, throw, true, failfast)

function _wait_multiple(waiting_tasks, all=false, failfast=false, throwexc=false)
function _wait_multiple(waiting_tasks, throwexc=false, all=false, failfast=false)
tasks = Task[]

for t in waiting_tasks
Expand Down Expand Up @@ -409,7 +409,7 @@ function _wait_multiple(waiting_tasks, all=false, failfast=false, throwexc=false
if nremaining == 0
return tasks, Task[]
elseif any(done_mask) && (!all || (failfast && exception))
if throwexc && failfast && exception
if throwexc && (!all || failfast) && exception
exceptions = [t.exception for t in tasks[done_mask] if istaskfailed(t)]
throw(CompositeException(exceptions))
else
Expand Down
31 changes: 22 additions & 9 deletions test/threads_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1218,15 +1218,28 @@ end

for tasks_type in (Vector{Task}, Set{Task}, Tuple{Task})
@testset "waitany" begin
tasks, event = create_tasks()
sleep(0.1)
done, pending = waitany(convert_tasks(tasks_type, tasks))
@test length(done) == 2
@test tasks[1] done
@test tasks[2] done
@test length(pending) == 1
@test tasks[3] pending
teardown(tasks, event)
@testset "no options" begin
tasks, event = create_tasks()
sleep(0.1)
done, pending = waitany(convert_tasks(tasks_type, tasks))
@test length(done) == 2
@test tasks[1] done
@test tasks[2] done
@test length(pending) == 1
@test tasks[3] pending
teardown(tasks, event)
end

@testset "throw=true" begin
tasks, event = create_tasks()
push!(tasks, Threads.@spawn error("Error"))

@test_throws CompositeException begin
waitany(convert_tasks(tasks_type, tasks); throw=true)
end

teardown(tasks, event)
end
end

@testset "waitall" begin
Expand Down

0 comments on commit dace55a

Please sign in to comment.