From a3e22f787ac1f63c84dcdcee7303d75d26c9f38f Mon Sep 17 00:00:00 2001 From: Todd Strader Date: Wed, 21 Apr 2021 14:17:06 -0400 Subject: [PATCH] Move lock methods to base class --- src/cocotb_bus/drivers/__init__.py | 12 ++++++++++++ src/cocotb_bus/drivers/avalon.py | 12 ------------ src/cocotb_bus/drivers/opb.py | 12 ------------ 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/cocotb_bus/drivers/__init__.py b/src/cocotb_bus/drivers/__init__.py index 2e402a9f..e72237f5 100644 --- a/src/cocotb_bus/drivers/__init__.py +++ b/src/cocotb_bus/drivers/__init__.py @@ -81,6 +81,8 @@ def __init__(self): """Constructor for a driver instance.""" self._pending = Event(name="Driver._pending") self._sendQ = deque() + self.busy_event = Event("Driver._busy") + self.busy = False # Sub-classes may already set up logging if not hasattr(self, "log"): @@ -89,6 +91,16 @@ def __init__(self): # Create an independent coroutine which can send stuff self._thread = cocotb.scheduler.add(self._send_thread()) + async def _acquire_lock(self): + if self.busy: + await self.busy_event.wait() + self.busy_event.clear() + self.busy = True + + def _release_lock(self): + self.busy = False + self.busy_event.set() + def kill(self): """Kill the coroutine sending stuff.""" if self._thread: diff --git a/src/cocotb_bus/drivers/avalon.py b/src/cocotb_bus/drivers/avalon.py index a8788c33..eb6b03ee 100644 --- a/src/cocotb_bus/drivers/avalon.py +++ b/src/cocotb_bus/drivers/avalon.py @@ -79,22 +79,10 @@ class AvalonMaster(AvalonMM): def __init__(self, entity, name, clock, **kwargs): AvalonMM.__init__(self, entity, name, clock, **kwargs) self.log.debug("AvalonMaster created") - self.busy_event = Event("%s_busy" % name) - self.busy = False def __len__(self): return 2**len(self.bus.address) - async def _acquire_lock(self): - if self.busy: - await self.busy_event.wait() - self.busy_event.clear() - self.busy = True - - def _release_lock(self): - self.busy = False - self.busy_event.set() - @coroutine async def read(self, address: int, sync: bool = True) -> BinaryValue: """Issue a request to the bus and block until this comes back. diff --git a/src/cocotb_bus/drivers/opb.py b/src/cocotb_bus/drivers/opb.py index cd76c8c9..46979b51 100644 --- a/src/cocotb_bus/drivers/opb.py +++ b/src/cocotb_bus/drivers/opb.py @@ -31,18 +31,6 @@ def __init__(self, entity, name, clock, **kwargs): BusDriver.__init__(self, entity, name, clock, **kwargs) self.bus.select.setimmediatevalue(0) self.log.debug("OPBMaster created") - self.busy_event = Event("%s_busy" % name) - self.busy = False - - async def _acquire_lock(self): - if self.busy: - await self.busy_event.wait() - self.busy_event.clear() - self.busy = True - - def _release_lock(self): - self.busy = False - self.busy_event.set() @cocotb.coroutine async def read(self, address: int, sync: bool = True) -> BinaryValue: