Skip to content

Commit 290f4a2

Browse files
committed
try/catch around taskloop
1 parent e7f2f4b commit 290f4a2

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/threadtasks.jl

+21-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ struct ThreadTask
33
end
44
Base.pointer(tt::ThreadTask) = tt.p
55

6-
@inline taskpointer(tid::T) where {T} = THREADPOOLPTR[] + tid*(THREADBUFFERSIZE%T)
6+
@inline taskpointer(tid::T) where {T} = THREADPOOLPTR[] + tid * (THREADBUFFERSIZE % T)
77

88
@inline function _call(p::Ptr{UInt})
99
fptr = load(p + sizeof(UInt), Ptr{Cvoid})
@@ -23,25 +23,30 @@ function (tt::ThreadTask)()
2323
p = pointer(tt)
2424
max_wait = one(UInt32) << 20
2525
wait_counter = max_wait
26-
GC.@preserve THREADPOOL begin
27-
while true
28-
if _atomic_state(p) == TASK
29-
_call(p)
30-
wait_counter = zero(UInt32)
31-
continue
32-
end
33-
pause()
34-
if (wait_counter += one(UInt32)) > max_wait
35-
wait_counter = zero(UInt32)
36-
_atomic_cas_cmp!(p, SPIN, WAIT) && Base.wait()
26+
try
27+
GC.@preserve THREADPOOL begin
28+
while true
29+
if _atomic_state(p) == TASK
30+
_call(p)
31+
wait_counter = zero(UInt32)
32+
continue
33+
end
34+
pause()
35+
if (wait_counter += one(UInt32)) > max_wait
36+
wait_counter = zero(UInt32)
37+
_atomic_cas_cmp!(p, SPIN, WAIT) && Base.wait()
38+
end
3739
end
3840
end
41+
catch
42+
show(stderr, MIME"text/plain"(), Base.current_task())
43+
println()
3944
end
4045
end
4146

4247
function _sleep(p::Ptr{UInt})
4348
_atomic_store!(p, WAIT)
44-
Base.wait();
49+
Base.wait()
4550
return nothing
4651
end
4752

@@ -58,7 +63,7 @@ function sleep_all_tasks()
5863
end
5964

6065
# 1-based tid, pushes into task 2-nthreads()
61-
@noinline function wake_thread!(_tid::T) where {T <: Integer}
66+
@noinline function wake_thread!(_tid::T) where {T<:Integer}
6267
tid = _tid % Int
6368
tidp1 = tid + one(tid)
6469
assume(unsigned(length(Base.Workqueues)) > unsigned(tid))
@@ -70,17 +75,16 @@ end
7075
@noinline function checktask(tid)
7176
t = TASKS[tid]
7277
if istaskfailed(t)
73-
show(stderr, MIME"text/plain"(), t)
74-
println()
7578
initialize_task(tid)
7679
return true
7780
end
7881
yield()
7982
false
8083
end
8184
# 1-based tid
85+
@inline tasktid(p::Ptr{UInt}) = (p - THREADPOOLPTR[]) ÷ (THREADBUFFERSIZE)
8286
@inline wait(tid::Integer) = wait(taskpointer(tid), tid)
83-
@inline wait(p::Ptr{UInt}) = wait(p, (p - THREADPOOLPTR[]) ÷ (THREADBUFFERSIZE))
87+
@inline wait(p::Ptr{UInt}) = wait(p, tasktid(p))
8488
@inline function wait(p::Ptr{UInt}, tid)
8589
counter = 0x00000000
8690
while _atomic_state(p) == TASK

0 commit comments

Comments
 (0)