Class ReadWriteSpinSemaphore
Very lightweight semaphore-like synchronization object based on System.Threading.SpinWait. It optimized for very-short write operations and concurrent read operations (short enough). It may spent lot of CPU cycles if you don't follow this rule. To achieve this internal state maintained which may be (-1 - for write operation in progress, 0 - for no operations, positive - for read operations). When positive it is equal to number of active read operations. WaitWrite() should be used for write operations and it will await when Eco.Shared.Concurrent.ReadWriteSpinSemaphore.state is 0 to switch state to -1. When write operation finished ReleaseWrite() should be used to allow other write and read operations. WaitRead() should be used for read operations and it will await until Eco.Shared.Concurrent.ReadWriteSpinSemaphore.state is greater or equal to 0 (no write operations in progress) and then increases state value. When read operation finished ReleaseRead() should be used to allow write operations. WARNING! Because this is a struct you should never ever copy it and always as an class field only. Read more in System.Threading.SpinLock documentation (https://docs.microsoft.com/en-us/dotnet/api/System.Threading.SpinLock?view=net-7.0).
Inheritance
Namespace: Eco.Shared.Concurrent
Assembly: Eco.Shared.dll
Syntax
public sealed class ReadWriteSpinSemaphore : ValueType
Methods
ReleaseRead()
Releases active Read operation. All Read operations should be released before any Write operation may start.
Declaration
public void ReleaseRead()
ReleaseWrite()
Releases active Write operation. No other operation may happen until Write released.
Declaration
public void ReleaseWrite()
WaitRead()
Waits for Read operation availability and modifies state to prevent concurrent write operations.
Declaration
public void WaitRead()
WaitWrite()
Waits for Write operation availability and modifies state to prevent other write and read operations.
Declaration
public void WaitWrite()