Skip to content

Commit

Permalink
Track idle primed state in one variable, instead of two
Browse files Browse the repository at this point in the history
  • Loading branch information
njsmith committed Jun 9, 2020
1 parent e71f696 commit 6b4341c
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions trio/_core/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2008,20 +2008,18 @@ def unrolled_run(runner, async_fn, args, host_uses_signal_set_wakeup_fd=False):
timeout = _MAX_TIMEOUT
timeout = min(max(0, timeout), _MAX_TIMEOUT)

idle_primed = False
idle_primed = None
if runner.waiting_for_idle:
cushion, tiebreaker, _ = runner.waiting_for_idle.keys()[0]
if cushion < timeout:
timeout = cushion
idle_primed = True
idle_prime_type = IdlePrimedTypes.WAITING_FOR_IDLE
idle_primed = IdlePrimedTypes.WAITING_FOR_IDLE
# We use 'elif' here because if there are tasks in
# wait_all_tasks_blocked, then those tasks will wake up without
# jumping the clock, so we don't need to autojump.
elif runner.clock_autojump_threshold < timeout:
timeout = runner.clock_autojump_threshold
idle_primed = True
idle_prime_type = IdlePrimedTypes.AUTOJUMP_CLOCK
idle_primed = IdlePrimedTypes.AUTOJUMP_CLOCK

if runner.instruments:
runner.instrument("before_io_wait", timeout)
Expand All @@ -2041,18 +2039,18 @@ def unrolled_run(runner, async_fn, args, host_uses_signal_set_wakeup_fd=False):
if deadline <= now:
# This removes the given scope from runner.deadlines:
cancel_scope.cancel()
idle_primed = False
idle_primed = None
else:
break

# idle_primed=True means: if the IO wait hit the timeout, and still
# nothing is happening, then we should start waking up
# idle_primed != None means: if the IO wait hit the timeout, and
# still nothing is happening, then we should start waking up
# wait_all_tasks_blocked tasks or autojump the clock. But there
# are some subtleties in defining "nothing is happening".
#
# 'not runner.runq' means that no tasks are currently runnable.
# 'not events' means that the last IO wait call hit its full
# timeout. These are very similar, and if idle_primed=True and
# timeout. These are very similar, and if idle_primed != None and
# we're running in regular mode then they always go together. But,
# in *guest* mode, they can happen independently, even when
# idle_primed=True:
Expand All @@ -2066,8 +2064,8 @@ def unrolled_run(runner, async_fn, args, host_uses_signal_set_wakeup_fd=False):
# before we got here.
#
# So we need to check both.
if idle_primed and not runner.runq and not events:
if idle_prime_type is IdlePrimedTypes.WAITING_FOR_IDLE:
if idle_primed is not None and not runner.runq and not events:
if idle_primed is IdlePrimedTypes.WAITING_FOR_IDLE:
while runner.waiting_for_idle:
key, task = runner.waiting_for_idle.peekitem(0)
if key[:2] == (cushion, tiebreaker):
Expand All @@ -2076,7 +2074,7 @@ def unrolled_run(runner, async_fn, args, host_uses_signal_set_wakeup_fd=False):
else:
break
else:
assert idle_prime_type is IdlePrimedTypes.AUTOJUMP_CLOCK
assert idle_primed is IdlePrimedTypes.AUTOJUMP_CLOCK
runner.clock._autojump()

# Process all runnable tasks, but only the ones that are already
Expand Down

0 comments on commit 6b4341c

Please sign in to comment.