@@ -1191,35 +1191,44 @@ end
1191
1191
1192
1192
checktaskempty = Partr. multiq_check_empty
1193
1193
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 ())
1195
1207
task = trypoptask (W)
1196
1208
if ! (task isa Task)
1209
+ # didn't find a task to run, try again
1197
1210
process_events ()
1198
1211
task = trypoptask (W)
1199
1212
end
1200
1213
if ! (task isa Task)
1214
+ # No tasks to run; switch to the scheduler task to run the
1215
+ # thread sleep logic.
1201
1216
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
1204
1220
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 )
1207
1223
end
1208
1224
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
1223
1232
return result
1224
1233
end
1225
1234
0 commit comments