mirror of
https://github.com/TalAloni/SMBLibrary.git
synced 2025-05-14 00:17:03 +02:00
Updates to Utilities
This commit is contained in:
parent
6cb61ca63e
commit
5508c749ce
13 changed files with 417 additions and 13 deletions
51
Utilities/Threading/CountdownLatch.cs
Normal file
51
Utilities/Threading/CountdownLatch.cs
Normal file
|
@ -0,0 +1,51 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Utilities
|
||||
{
|
||||
public class CountdownLatch
|
||||
{
|
||||
private int m_count;
|
||||
private EventWaitHandle m_waitHandle = new EventWaitHandle(true, EventResetMode.ManualReset);
|
||||
|
||||
public CountdownLatch()
|
||||
{
|
||||
}
|
||||
|
||||
public void Increment()
|
||||
{
|
||||
int count = Interlocked.Increment(ref m_count);
|
||||
if (count == 1)
|
||||
{
|
||||
m_waitHandle.Reset();
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(int value)
|
||||
{
|
||||
int count = Interlocked.Add(ref m_count, value);
|
||||
if (count == value)
|
||||
{
|
||||
m_waitHandle.Reset();
|
||||
}
|
||||
}
|
||||
|
||||
public void Decrement()
|
||||
{
|
||||
int count = Interlocked.Decrement(ref m_count);
|
||||
if (m_count == 0)
|
||||
{
|
||||
m_waitHandle.Set();
|
||||
}
|
||||
else if (count < 0)
|
||||
{
|
||||
throw new InvalidOperationException("Count must be greater than or equal to 0");
|
||||
}
|
||||
}
|
||||
|
||||
public void WaitUntilZero()
|
||||
{
|
||||
m_waitHandle.WaitOne();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue