Skip to content

Commit ba70cec

Browse files
committed
Use a scheduler task for thread sleep
1 parent e78cf6c commit ba70cec

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

base/task.jl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,16 @@ function throwto(t::Task, @nospecialize exc)
11451145
return try_yieldto(identity)
11461146
end
11471147

1148+
@inline function wait_forever()
1149+
while true
1150+
wait()
1151+
end
1152+
end
1153+
1154+
const get_sched_task = OncePerThread{Task}() do
1155+
@task wait_forever()
1156+
end
1157+
11481158
function ensure_rescheduled(othertask::Task)
11491159
ct = current_task()
11501160
W = workqueue_for(Threads.threadid())
@@ -1184,7 +1194,17 @@ checktaskempty = Partr.multiq_check_empty
11841194
@noinline function poptask(W::StickyWorkqueue)
11851195
task = trypoptask(W)
11861196
if !(task isa Task)
1187-
task = ccall(:jl_task_get_next, Ref{Task}, (Any, Any, Any), trypoptask, W, checktaskempty)
1197+
process_events()
1198+
task = trypoptask(W)
1199+
end
1200+
if !(task isa Task)
1201+
sched_task = get_sched_task()
1202+
if current_task() === sched_task
1203+
task = ccall(:jl_task_get_next, Ref{Task}, (Any, Any, Any), trypoptask, W, checktaskempty)
1204+
else
1205+
yieldto(sched_task)
1206+
task = current_task()
1207+
end
11881208
end
11891209
set_next_task(task)
11901210
nothing

0 commit comments

Comments
 (0)