Skip to content

Commit b2a7f66

Browse files
authored
Merge pull request #46 from JuliaSIMD/catch
Catch
2 parents e7f2f4b + 7946fe6 commit b2a7f66

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ThreadingUtilities"
22
uuid = "8290d209-cae3-49c0-8002-c8c24d57dab5"
33
authors = ["Chris Elrod <elrodc@gmail.com> and contributors"]
4-
version = "0.5.1"
4+
version = "0.5.2"
55

66
[deps]
77
ManualMemory = "d125e4d3-2237-4719-b19c-fa641b8a4667"

src/threadtasks.jl

Lines changed: 5 additions & 6 deletions
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})
@@ -41,7 +41,7 @@ end
4141

4242
function _sleep(p::Ptr{UInt})
4343
_atomic_store!(p, WAIT)
44-
Base.wait();
44+
Base.wait()
4545
return nothing
4646
end
4747

@@ -58,7 +58,7 @@ function sleep_all_tasks()
5858
end
5959

6060
# 1-based tid, pushes into task 2-nthreads()
61-
@noinline function wake_thread!(_tid::T) where {T <: Integer}
61+
@noinline function wake_thread!(_tid::T) where {T<:Integer}
6262
tid = _tid % Int
6363
tidp1 = tid + one(tid)
6464
assume(unsigned(length(Base.Workqueues)) > unsigned(tid))
@@ -70,17 +70,16 @@ end
7070
@noinline function checktask(tid)
7171
t = TASKS[tid]
7272
if istaskfailed(t)
73-
show(stderr, MIME"text/plain"(), t)
74-
println()
7573
initialize_task(tid)
7674
return true
7775
end
7876
yield()
7977
false
8078
end
8179
# 1-based tid
80+
@inline tasktid(p::Ptr{UInt}) = (p - THREADPOOLPTR[]) ÷ (THREADBUFFERSIZE)
8281
@inline wait(tid::Integer) = wait(taskpointer(tid), tid)
83-
@inline wait(p::Ptr{UInt}) = wait(p, (p - THREADPOOLPTR[]) ÷ (THREADBUFFERSIZE))
82+
@inline wait(p::Ptr{UInt}) = wait(p, tasktid(p))
8483
@inline function wait(p::Ptr{UInt}, tid)
8584
counter = 0x00000000
8685
while _atomic_state(p) == TASK

test/threadingutilities.jl

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
struct Copy{P} end
22
function (::Copy{P})(p::Ptr{UInt}) where {P}
3-
_, (ptry,ptrx,N) = ThreadingUtilities.load(p, P, 2*sizeof(UInt))
3+
_, (ptry, ptrx, N) = ThreadingUtilities.load(p, P, 2 * sizeof(UInt))
44
N > 0 || throw("This function throws if N == 0 for testing purposes.")
55
@simd ivdep for n 1:N
66
unsafe_store!(ptry, unsafe_load(ptrx, n), n)
@@ -20,18 +20,18 @@ function setup_copy!(p, y, x)
2020
px = pointer(x)
2121
fptr = copy_ptr(py, px)
2222
offset = ThreadingUtilities.store!(p, fptr, sizeof(UInt))
23-
ThreadingUtilities.store!(p, (py,px,N), offset)
23+
ThreadingUtilities.store!(p, (py, px, N), offset)
2424
end
2525

2626
@inline launch_thread_copy!(tid, y, x) = ThreadingUtilities.launch(setup_copy!, tid, y, x)
2727

2828
function test_copy(tid, N = 100_000)
29-
a = rand(N);
30-
b = rand(N);
31-
c = rand(N);
32-
x = similar(a) .= NaN;
33-
y = similar(b) .= NaN;
34-
z = similar(c) .= NaN;
29+
a = rand(N)
30+
b = rand(N)
31+
c = rand(N)
32+
x = similar(a) .= NaN
33+
y = similar(b) .= NaN
34+
z = similar(c) .= NaN
3535
GC.@preserve a b c x y z begin
3636
launch_thread_copy!(tid, x, a)
3737
yield()
@@ -53,25 +53,34 @@ end
5353
@test unsafe_load(Ptr{UInt32}(ThreadingUtilities.taskpointer(tid))) == 0x00000001
5454
end
5555
@test all(eachindex(ThreadingUtilities.TASKS)) do tid
56-
ThreadingUtilities.load(ThreadingUtilities.taskpointer(tid), ThreadingUtilities.ThreadState) === ThreadingUtilities.WAIT
56+
ThreadingUtilities.load(
57+
ThreadingUtilities.taskpointer(tid),
58+
ThreadingUtilities.ThreadState,
59+
) === ThreadingUtilities.WAIT
5760
end
5861
@test all(eachindex(ThreadingUtilities.TASKS)) do tid
59-
ThreadingUtilities._atomic_load(reinterpret(Ptr{UInt32}, ThreadingUtilities.taskpointer(tid))) === reinterpret(UInt32, ThreadingUtilities.WAIT)
62+
ThreadingUtilities._atomic_load(
63+
reinterpret(Ptr{UInt32}, ThreadingUtilities.taskpointer(tid)),
64+
) === reinterpret(UInt32, ThreadingUtilities.WAIT)
6065
end
6166
foreach(test_copy, eachindex(ThreadingUtilities.TASKS))
62-
63-
x = rand(UInt, 3);
67+
68+
x = rand(UInt, 3)
6469
GC.@preserve x begin
6570
ThreadingUtilities._atomic_store!(pointer(x), zero(UInt))
66-
@test ThreadingUtilities._atomic_xchg!(pointer(x), ThreadingUtilities.WAIT) == ThreadingUtilities.TASK
67-
@test ThreadingUtilities._atomic_umax!(pointer(x), ThreadingUtilities.TASK) == ThreadingUtilities.WAIT
68-
@test ThreadingUtilities._atomic_umax!(pointer(x), ThreadingUtilities.SPIN) == ThreadingUtilities.WAIT
69-
@test ThreadingUtilities.load(pointer(x), ThreadingUtilities.ThreadState) == ThreadingUtilities.SPIN
71+
@test ThreadingUtilities._atomic_xchg!(pointer(x), ThreadingUtilities.WAIT) ==
72+
ThreadingUtilities.TASK
73+
@test ThreadingUtilities._atomic_umax!(pointer(x), ThreadingUtilities.TASK) ==
74+
ThreadingUtilities.WAIT
75+
@test ThreadingUtilities._atomic_umax!(pointer(x), ThreadingUtilities.SPIN) ==
76+
ThreadingUtilities.WAIT
77+
@test ThreadingUtilities.load(pointer(x), ThreadingUtilities.ThreadState) ==
78+
ThreadingUtilities.SPIN
7079
end
7180
for tid eachindex(ThreadingUtilities.TASKS)
7281
launch_thread_copy!(tid, Float64[], Float64[])
7382
end
74-
yield()
83+
sleep(1)
7584
@test all(istaskfailed, ThreadingUtilities.TASKS)
7685
@test all(ThreadingUtilities.wait, eachindex(ThreadingUtilities.TASKS))
7786
@test !any(istaskfailed, ThreadingUtilities.TASKS)

0 commit comments

Comments
 (0)