Class Semaphore
- Namespace
- Temporalio.Workflows
- Assembly
- Temporalio.dll
Semaphore is an alternative to Semaphore and SemaphoreSlim built to be deterministic and lightweight. Many of the concepts are similar to SemaphoreSlim.
public class Semaphore
- Inheritance
-
Semaphore
- Inherited Members
Constructors
Semaphore(int)
Initializes a new instance of the Semaphore class.
public Semaphore(int initialCount)
Parameters
initialCount
intThe initial number of permits that will be available for requests.
Properties
CurrentCount
Gets the current number of available permits that can be granted concurrently. This is
decremented on each successful WaitAsync
call and incremented on each
Release
call.
public int CurrentCount { get; }
Property Value
Max
Gets the fixed maximum number of permits that can be granted concurrently. This is
always the initialCount
value from the constructor.
public int Max { get; init; }
Property Value
Methods
Release()
Release a permit for use by another waiter.
public void Release()
Exceptions
- InvalidOperationException
Thrown if this release call would extend the CurrentCount beyond Max which means more
Release
calls were made than successfulWaitAsync
calls.
WaitAsync(int, CancellationToken?)
Wait for a permit to become available or timeout to be reached. If the task returns true, users must call Release() to properly give the permit back when done.
public Task<bool> WaitAsync(int millisecondsTimeout, CancellationToken? cancellationToken = null)
Parameters
millisecondsTimeout
intMilliseconds until timeout. If this is 0, this is a non-blocking task that will return immediately. If this value is -1 (i.e. Infinite), there is no timeout and the result will always be true on success (at which point you might as well use WaitAsync(CancellationToken?)). Otherwise a timer is started for the timeout and canceled if the wait succeeds.
cancellationToken
CancellationToken?Cancellation token that can interrupt this wait. If unset, this defaults to CancellationToken. Upon cancel, awaiting the resulting task will throw a TaskCanceledException exception.
Returns
- Task<bool>
A Task representing the result of the asynchronous operation. The task is true if the wait succeeded and false if it timed out. This task is canceled if the cancellation token is.
WaitAsync(CancellationToken?)
Wait for a permit to become available. If the task succeeds, users must call Release() to properly give the permit back when done.
public Task WaitAsync(CancellationToken? cancellationToken = null)
Parameters
cancellationToken
CancellationToken?Cancellation token that can interrupt this wait. If unset, this defaults to CancellationToken. Upon cancel, awaiting the resulting task will throw a TaskCanceledException exception.
Returns
- Task
A Task representing the result of the asynchronous operation. This task is canceled if the cancellation token is.
WaitAsync(TimeSpan, CancellationToken?)
Wait for a permit to become available or timeout to be reached. If the task returns true, users must call Release() to properly give the permit back when done.
public Task<bool> WaitAsync(TimeSpan timeout, CancellationToken? cancellationToken = null)
Parameters
timeout
TimeSpanTimeSpan until timeout. If this is Zero, this is a non-blocking task that will return immediately. If this value is -1ms (i.e. InfiniteTimeSpan), there is no timeout and the result will always be true on success (at which point you might as well use WaitAsync(CancellationToken?)). Otherwise a timer is started for the timeout and canceled if the wait succeeds.
cancellationToken
CancellationToken?Cancellation token that can interrupt this wait. If unset, this defaults to CancellationToken. Upon cancel, awaiting the resulting task will throw a TaskCanceledException exception.