Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move lock methods to base class #23

Merged
merged 1 commit into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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