Skip to content

Commit b0f4a2a

Browse files
authored
Merge pull request #30 from JuliaSIMD/resetonerror
Reset threads on error
2 parents eb39080 + 64c7bd8 commit b0f4a2a

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

src/ThreadingUtilities.jl

+1-9
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,14 @@ include("atomics.jl")
3030
include("threadtasks.jl")
3131
include("warnings.jl")
3232

33-
function initialize_task(tid::Int)
33+
function initialize_task(tid)
3434
_atomic_store!(taskpointer(tid), WAIT)
3535
t = Task(ThreadTask(taskpointer(tid)));
3636
t.sticky = true # create and pin
3737
# set to tid, we have tasks 2...nthread, from 1-based ind perspective
3838
ccall(:jl_set_task_tid, Cvoid, (Any, Cint), t, tid % Cint)
3939
TASKS[tid] = t
4040
end
41-
function reinitialize_tasks!(verbose::Bool = true)
42-
for (tid,t) enumerate(TASKS)
43-
if istaskfailed(t)
44-
verbose && dump(t)
45-
initialize_task(tid)
46-
end
47-
end
48-
end
4941

5042
function __init__()
5143
_print_exclusivity_warning()

src/threadtasks.jl

+17-3
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,27 @@ end
5151
ccall(:jl_wakeup_thread, Cvoid, (Int16,), tid % Int16)
5252
end
5353

54+
@noinline function checktask(tid)
55+
t = TASKS[tid]
56+
if istaskfailed(t)
57+
dump(t)
58+
initialize_task(tid)
59+
return true
60+
end
61+
yield()
62+
false
63+
end
5464
# 1-based tid
55-
@inline wait(tid::Integer) = wait(taskpointer(tid))
56-
@inline function wait(p::Ptr{UInt})
65+
@inline wait(tid::Integer) = wait(taskpointer(tid), tid)
66+
@inline wait(p::Ptr{UInt}) = wait(p, (p - THREADPOOLPTR[]) ÷ (THREADBUFFERSIZE))
67+
@inline function wait(p::Ptr{UInt}, tid)
5768
counter = 0x00000000
5869
while _atomic_state(p) == TASK
5970
pause()
60-
((counter += 0x00000001) > 0x00010000) && yield()
71+
if ((counter += 0x00000001) > 0x00010000)
72+
checktask(tid) && return true
73+
end
6174
end
75+
false
6276
end
6377

test/threadingutilities.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ function test_copy(tid, N = 100_000)
3838
GC.@preserve a b c x y z begin
3939
launch_thread_copy!(tid, x, a)
4040
yield()
41-
ThreadingUtilities.wait(tid)
41+
@assert !ThreadingUtilities.wait(tid)
4242
launch_thread_copy!(tid, y, b)
4343
yield()
44-
ThreadingUtilities.wait(tid)
44+
@assert !ThreadingUtilities.wait(tid)
4545
launch_thread_copy!(tid, z, c)
4646
yield()
47-
ThreadingUtilities.wait(tid)
47+
@assert !ThreadingUtilities.wait(ThreadingUtilities.taskpointer(tid))
4848
end
4949
@test a == x
5050
@test b == y
@@ -76,7 +76,7 @@ end
7676
end
7777
yield()
7878
@test all(istaskfailed, ThreadingUtilities.TASKS)
79-
ThreadingUtilities.reinitialize_tasks!(false)
79+
@test all(ThreadingUtilities.wait, eachindex(ThreadingUtilities.TASKS))
8080
@test !any(istaskfailed, ThreadingUtilities.TASKS)
8181
# test copy on the reinitialized tasks
8282
foreach(test_copy, eachindex(ThreadingUtilities.TASKS))

0 commit comments

Comments
 (0)