From 6b4341c0172a091290e52c5a585ad6720f4ba4e4 Mon Sep 17 00:00:00 2001 From: "Nathaniel J. Smith" Date: Tue, 9 Jun 2020 15:43:05 -0700 Subject: [PATCH] Track idle primed state in one variable, instead of two --- trio/_core/_run.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/trio/_core/_run.py b/trio/_core/_run.py index 1441361dd4..8b112051e5 100644 --- a/trio/_core/_run.py +++ b/trio/_core/_run.py @@ -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) @@ -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: @@ -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): @@ -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