TxSemaphore(ref, capacity) wraps an existing TxRef (Effect's
makeUnsafe); make is the effect.
Attributes#
| Attribute | Type | Description |
|---|---|---|
capacity | int | |
ref | TxRef[int] | The free permits. |
Functions#
__init__#
__init__(permits, capacity) -> None| Parameter | Type | Description |
|---|---|---|
permits | TxRef[int] | |
capacity | int |
def __init__(self, permits: TxRef[int], capacity: int) -> None:
self._permits = permits
self._capacity = capacitymake#
make(permits) -> Effect[TxSemaphore]A semaphore with permits free permits, also its capacity. Dies
if permits is negative.
| Parameter | Type | Description |
|---|---|---|
permits | int |
Returns Effect[TxSemaphore]
@staticmethod
def make(permits: int) -> Effect[TxSemaphore]:
"""A semaphore with ``permits`` free permits, also its capacity. Dies
if ``permits`` is negative."""
if permits < 0:
return die(ValueError("Permits must be non-negative"))
return TxRef.make(permits).map(lambda ref: TxSemaphore(ref, permits))available#
available() -> Effect[int]Returns Effect[int]
def available(self) -> Effect[int]:
return self._permits.get()acquire#
acquire() -> Effect[None]Take one permit, waiting until one is free.
Returns Effect[None]
def acquire(self) -> Effect[None]:
"""Take one permit, waiting until one is free."""
return self._acquire(1)acquire_n#
acquire_n(n) -> Effect[None]Take n permits at once, waiting until that many are free.
Dies if n is not positive.
| Parameter | Type | Description |
|---|---|---|
n | int |
Returns Effect[None]
def acquire_n(self, n: int) -> Effect[None]:
"""Take ``n`` permits at once, waiting until that many are free.
Dies if ``n`` is not positive."""
return _positive(n).and_then(self._acquire(n))try_acquire#
try_acquire() -> Effect[bool]Take one permit if one is free now.
Returns Effect[bool]
def try_acquire(self) -> Effect[bool]:
"""Take one permit if one is free now."""
return self._try_acquire(1)try_acquire_n#
try_acquire_n(n) -> Effect[bool]Take n permits if that many are free now. Dies if n is not
positive.
| Parameter | Type | Description |
|---|---|---|
n | int |
Returns Effect[bool]
def try_acquire_n(self, n: int) -> Effect[bool]:
"""Take ``n`` permits if that many are free now. Dies if ``n`` is not
positive."""
return _positive(n).and_then(self._try_acquire(n))release#
release() -> Effect[None]Give back one permit, up to the capacity.
Returns Effect[None]
def release(self) -> Effect[None]:
"""Give back one permit, up to the capacity."""
return self._release(1)release_n#
release_n(n) -> Effect[None]Give back n permits, up to the capacity. Dies if n is not
positive.
| Parameter | Type | Description |
|---|---|---|
n | int |
Returns Effect[None]
def release_n(self, n: int) -> Effect[None]:
"""Give back ``n`` permits, up to the capacity. Dies if ``n`` is not
positive."""
return _positive(n).and_then(self._release(n))with_permit#
with_permit[A, E, R](eff) -> Effect[A, E, R]Run eff holding one permit, giving it back however it ends.
Only the wait for the permit is interruptible.
| Parameter | Type | Description |
|---|---|---|
eff | Effect[A, E, R] |
Returns Effect[A, E, R]
def with_permit[A, E, R](self, eff: Effect[A, E, R]) -> Effect[A, E, R]:
"""Run ``eff`` holding one permit, giving it back however it ends.
Only the wait for the permit is interruptible."""
require_effect(eff, "eff")
return self._with_acquired(self.acquire(), eff, self.release())with_permits#
with_permits[A, E, R](eff, n) -> Effect[A, E, R]with_permit for n permits.
| Parameter | Type | Description |
|---|---|---|
eff | Effect[A, E, R] | |
n | int |
Returns Effect[A, E, R]
def with_permits[A, E, R](self, eff: Effect[A, E, R], n: int) -> Effect[A, E, R]:
"""``with_permit`` for ``n`` permits."""
require_effect(eff, "eff")
return self._with_acquired(self.acquire_n(n), eff, self.release_n(n))with_permit_scoped#
with_permit_scoped() -> Effect[None, Never, Scope]Take one permit until the current scope closes. Only the wait for the permit is interruptible.
Returns Effect[None, Never, Scope]
def with_permit_scoped(self) -> Effect[None, Never, Scope]:
"""Take one permit until the current scope closes. Only the wait for
the permit is interruptible."""
release = self.release()
return uninterruptible_mask(
lambda restore: tx_restoring(restore, self.acquire()).and_then(
add_finalizer(lambda _: release)
)
)