|
27 | 27 | Base.schedule(t::StableTask) = (schedule(t.t); t)
|
28 | 28 | Base.schedule(t, val; error=false) = (schedule(t.t, val; error); t)
|
29 | 29 |
|
| 30 | +""" |
| 31 | + set_task_tid!(task::Task, tid::Integer) |
| 32 | +
|
| 33 | +Sets the thread ID of `task` to `tid`. Retries up to 10 times in case |
| 34 | +the ccall fails. |
| 35 | +
|
| 36 | +Calls `jl_set_task_tid` internally. |
| 37 | +
|
| 38 | +## Extended help |
| 39 | +
|
| 40 | +Copied from [Dagger.jl](https://github.com/JuliaParallel/Dagger.jl/blob/62f8307a46436bfed1f12414b812a776147a6851/src/utils/tasks.jl#L1), |
| 41 | +credit to Julian Samaroo for the implementation and for letting us know about this! |
| 42 | +""" |
| 43 | +function set_task_tid!(task::Task, tid::Integer) |
| 44 | + task.sticky = true |
| 45 | + ctr = 0 |
| 46 | + while true |
| 47 | + ret = ccall(:jl_set_task_tid, Cint, (Any, Cint), task, tid-1) |
| 48 | + if ret == 1 |
| 49 | + break |
| 50 | + elseif ret == 0 |
| 51 | + yield() |
| 52 | + else |
| 53 | + error("Unexpected retcode from jl_set_task_tid: $ret") |
| 54 | + end |
| 55 | + ctr += 1 |
| 56 | + if ctr > 10 |
| 57 | + @warn "Setting task TID to $tid failed, giving up!" |
| 58 | + return |
| 59 | + end |
| 60 | + end |
| 61 | + @assert Threads.threadid(task) == tid "jl_set_task_tid failed!" |
| 62 | +end |
| 63 | + |
30 | 64 | """
|
31 | 65 | @spawn [:default|:interactive] expr
|
32 | 66 |
|
@@ -107,7 +141,7 @@ macro spawnat(thrdid, ex)
|
107 | 141 | thunk_wrap = () -> (ret[] = thunk(); nothing)
|
108 | 142 | local task = Task(thunk_wrap)
|
109 | 143 | task.sticky = true
|
110 |
| - ccall(:jl_set_task_tid, Cvoid, (Any, Cint), task, $tid - 1) |
| 144 | + set_task_tid!(task, $tid) |
111 | 145 | if $(Expr(:islocal, var))
|
112 | 146 | put!($var, task)
|
113 | 147 | end
|
|
0 commit comments