usim icon indicating copy to clipboard operation
usim copied to clipboard

Add Semaphore/Resource type

Open maxfischer2781 opened this issue 4 years ago • 0 comments

Porting SimPy resources (#51 and #50) made it pretty obvious that uSim's Resources type is a bit awkward when there really is just one type of resources.

resources = Resources(capacity=8)
await resource.increase(capacity=8)
await resource.decrease(capacity=8)
async with resource.borrow(capacity=8):
    ...

Other ways to emulate a Semaphore are Tracked and Queue, which come with other unwanted baggage. IMO the Resources interface is good, but we may want a "just one type" version:

resources = Resource(8)
await resource.increase(8)
await resource.decrease(8)
async with resource.borrow(8):
    ...

The use-case of a raw Semaphore would be easily handled by having proper defaults:

resources = Resource()  # default level of 0
await resource.increase()  # default in/decrease of 1
await resource.decrease()
async with resource.borrow():  # default borrow/claim of 1
    ...

maxfischer2781 avatar Sep 19 '19 14:09 maxfischer2781