Skip to content

Commit

Permalink
Move lock methods to base class
Browse files Browse the repository at this point in the history
  • Loading branch information
toddstrader authored and ktbarrett committed Apr 26, 2021
1 parent 3c93452 commit a3e22f7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
12 changes: 12 additions & 0 deletions src/cocotb_bus/drivers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand All @@ -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:
Expand Down
12 changes: 0 additions & 12 deletions src/cocotb_bus/drivers/avalon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 0 additions & 12 deletions src/cocotb_bus/drivers/opb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit a3e22f7

Please sign in to comment.