Advertisement
uniblab

Improved Semaphore wrapper/helper (k-mode, u-mode, and null)

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