Add context manager for currently scheduled task.
@@ -33,6 +33,7 @@ FIXME: We have a problem here. If a coroutine schedules a read-only but we
also have pending writes we have to schedule the ReadWrite callback before
the ReadOnly (and this is invalid, at least in Modelsim).
"""
+from contextlib import contextmanager
import os
import sys
import logging
@@ -243,6 +244,8 @@ class Scheduler:
self._test = None
self._main_thread = threading.current_thread()
+ self._current_task = None
+
self._is_reacting = False
self._write_coro_inst = None
@@ -741,6 +744,16 @@ class Scheduler:
.format(type(result), result)
)
+ @contextmanager
+ def _task_context(self, task):
+ """Context manager for the currently running task."""
+ old_task = self._current_task
+ self._current_task = task
+ try:
+ yield
+ finally:
+ self._current_task = old_task
+
def schedule(self, coroutine, trigger=None):
"""Schedule a coroutine by calling the send method.
@@ -749,6 +762,7 @@ class Scheduler:
trigger (cocotb.triggers.Trigger): The trigger that caused this
coroutine to be scheduled.
"""
+ with self._task_context(coroutine):
if trigger is None:
send_outcome = outcomes.Value(None)
else:
