mirror of
https://github.com/mirror/processhacker
synced 2026-06-08 16:03:24 +00:00
b02427417c
git-svn-id: svn://svn.code.sf.net/p/processhacker/code@5614 21ef857c-d57f-4fe0-8362-d861dc6d29cd
61 lines
1.3 KiB
C#
61 lines
1.3 KiB
C#
using System;
|
|
using System.Threading;
|
|
|
|
namespace ProcessHacker.Common.Threading
|
|
{
|
|
public class SemaphorePair : IDisposable
|
|
{
|
|
private int _count;
|
|
private Semaphore _readSemaphore;
|
|
private Semaphore _writeSemaphore;
|
|
|
|
public SemaphorePair(int count)
|
|
{
|
|
_count = count;
|
|
_readSemaphore = new Semaphore(0, count);
|
|
_writeSemaphore = new Semaphore(count, count);
|
|
}
|
|
|
|
public int Count
|
|
{
|
|
get { return _count; }
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_readSemaphore.Close();
|
|
_writeSemaphore.Close();
|
|
}
|
|
|
|
public void ReleaseRead()
|
|
{
|
|
_readSemaphore.Release();
|
|
}
|
|
|
|
public void ReleaseWrite()
|
|
{
|
|
_writeSemaphore.Release();
|
|
}
|
|
|
|
public void WaitRead()
|
|
{
|
|
_readSemaphore.WaitOne();
|
|
}
|
|
|
|
public bool WaitRead(int timeout)
|
|
{
|
|
return _readSemaphore.WaitOne(timeout, false);
|
|
}
|
|
|
|
public void WaitWrite()
|
|
{
|
|
_writeSemaphore.WaitOne();
|
|
}
|
|
|
|
public bool WaitWrite(int timeout)
|
|
{
|
|
return _writeSemaphore.WaitOne(timeout, false);
|
|
}
|
|
}
|
|
}
|