Skip to content

Commit 8de1806

Browse files
committed
Rearrange the code to make things cleaner
1 parent ba70cec commit 8de1806

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

base/task.jl

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,35 +1191,44 @@ end
11911191

11921192
checktaskempty = Partr.multiq_check_empty
11931193

1194-
@noinline function poptask(W::StickyWorkqueue)
1194+
function wait()
1195+
ct = current_task()
1196+
# [task] user_time -yield-or-done-> wait_time
1197+
record_running_time!(ct)
1198+
# let GC run
1199+
GC.safepoint()
1200+
# check for libuv events
1201+
process_events()
1202+
1203+
# get the next task to run
1204+
result = nothing
1205+
have_result = false
1206+
W = workqueue_for(Threads.threadid())
11951207
task = trypoptask(W)
11961208
if !(task isa Task)
1209+
# didn't find a task to run, try again
11971210
process_events()
11981211
task = trypoptask(W)
11991212
end
12001213
if !(task isa Task)
1214+
# No tasks to run; switch to the scheduler task to run the
1215+
# thread sleep logic.
12011216
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)
1217+
if ct !== sched_task
1218+
result = yieldto(sched_task)
1219+
have_result = true
12041220
else
1205-
yieldto(sched_task)
1206-
task = current_task()
1221+
task = ccall(:jl_task_get_next, Ref{Task}, (Any, Any, Any),
1222+
trypoptask, W, checktaskempty)
12071223
end
12081224
end
1209-
set_next_task(task)
1210-
nothing
1211-
end
1212-
1213-
function wait()
1214-
ct = current_task()
1215-
# [task] user_time -yield-or-done-> wait_time
1216-
record_running_time!(ct)
1217-
GC.safepoint()
1218-
W = workqueue_for(Threads.threadid())
1219-
poptask(W)
1220-
result = try_yieldto(ensure_rescheduled)
1221-
process_events()
1222-
# return when we come out of the queue
1225+
# We may have already switched tasks (via the scheduler task), so
1226+
# only switch if we haven't.
1227+
if !have_result
1228+
@assert task isa Task
1229+
set_next_task(task)
1230+
result = try_yieldto(ensure_rescheduled)
1231+
end
12231232
return result
12241233
end
12251234

0 commit comments

Comments
 (0)