uniblab

Semaphore helper/wrapper

Mar 24th, 2020
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | None | 0 0
  1. namespace Icod.Wod {
  2.  
  3.     public sealed class Semaphore : System.IDisposable {
  4.  
  5.         #region fields
  6.         private readonly System.Threading.Semaphore mySemaphore;
  7.         private readonly System.Threading.SemaphoreSlim mySemaphoreSlim;
  8.  
  9.         private readonly System.Action myWait;
  10.         private readonly System.Func<System.TimeSpan, System.Boolean> myWaitTimeSpan;
  11.         private readonly System.Func<System.Int32, System.Boolean> myWaitMilliseconds;
  12.         private readonly System.Func<System.Int32> myRelease;
  13.         #endregion fields
  14.  
  15.  
  16.         #region .ctor
  17.         public Semaphore( System.Int32 initialCount, System.Int32 maximumCount ) : this( initialCount, maximumCount, null ) {
  18.         }
  19.         public Semaphore( System.Int32 initialCount, System.Int32 maximumCount, System.String name ) {
  20.             if ( System.String.IsNullOrEmpty( name ) ) {
  21.                 mySemaphoreSlim = new System.Threading.SemaphoreSlim( initialCount, maximumCount );
  22.                 myRelease = () => mySemaphoreSlim.Release();
  23.                 myWait = () => mySemaphoreSlim.Wait();
  24.                 myWaitTimeSpan = timeout => mySemaphoreSlim.Wait( timeout );
  25.                 myWaitMilliseconds = millisecondsTimeout => mySemaphoreSlim.Wait( millisecondsTimeout );
  26.             } else {
  27.                 mySemaphore = new System.Threading.Semaphore( initialCount, maximumCount, name );
  28.                 myRelease = () => mySemaphore.Release();
  29.                 myWait = () => mySemaphore.WaitOne();
  30.                 myWaitTimeSpan = timeout => mySemaphore.WaitOne( timeout );
  31.                 myWaitMilliseconds = millisecondsTimeout => mySemaphore.WaitOne( millisecondsTimeout );
  32.             }
  33.         }
  34.  
  35.         ~Semaphore() {
  36.             this.Dispose( false );
  37.         }
  38.         #endregion .ctor
  39.  
  40.  
  41.         #region methods
  42.         public void Dispose() {
  43.             this.Dispose( true );
  44.             System.GC.SuppressFinalize( this );
  45.         }
  46.         protected void Dispose( System.Boolean disposing ) {
  47.             if ( disposing ) {
  48.                 if ( null != mySemaphore ) {
  49.                     mySemaphore.Dispose();
  50.                 }
  51.                 if ( null != mySemaphoreSlim ) {
  52.                     mySemaphoreSlim.Dispose();
  53.                 }
  54.             }
  55.         }
  56.  
  57.         public System.Int32 Release() {
  58.             return myRelease();
  59.         }
  60.         public void Wait() {
  61.             myWait();
  62.         }
  63.         public System.Boolean Wait( System.TimeSpan timeout ) {
  64.             return myWaitTimeSpan( timeout );
  65.         }
  66.         public System.Boolean Wait( System.Int32 millisecondsTimeout ) {
  67.             return myWaitMilliseconds( millisecondsTimeout );
  68.         }
  69.         #endregion methods
  70.  
  71.     }
  72.  
  73. }
Add Comment
Please, Sign In to add comment