Commits

Anonymous committed 9c3a565 Draft

renamed Semaphore.Attempt to Semaphore.Acquire to match new ITimedSync interface

  • Participants
  • Parent commits 507b684

Comments (0)

Files changed (2)

Cineraria.Concurrency/ISync.cs

 namespace Cineraria.Concurrency
 {
     /// <summary>
-    /// A semaphore.
+    /// A synchronisation utility.
     /// </summary>
     public interface ISync
     {
         /// </summary>
         void Release();
     }
+
+    /// <summary>
+    /// A synchronisation utility that accepts a timeout if required.
+    /// </summary>
+    public interface ITimedSync : ISync
+    {
+        /// <summary>
+        /// Waits a maximum of <paramref name="milliseconds"/> to acquire a
+        /// logical token.
+        /// </summary>
+        /// <param name="milliseconds">
+        /// The maximum number of milliseconds to wait.
+        /// </param>
+        /// <returns>
+        /// true if a permission was acquired, false otherwise.
+        /// </returns>
+        bool Acquire(int milliseconds);
+    }
 }

Cineraria.Concurrency/Semaphore.cs

     /// <summary>
     /// A semaphore.
     /// </summary>
-    public class Semaphore : ISync
+    public class Semaphore : ITimedSync
     {
         /// <summary>
         /// Object used for lock() blocks.
         private int _waitingThreads;
 
         /// <summary>
-        /// Initializes a new instance of the <see cref="Semaphore"/> class with 0 logical tokens.
+        /// Initializes a new instance of the <see cref="Semaphore"/> class
+        /// with 0 logical tokens.
         /// </summary>
         public Semaphore()
             : this(0)
         }
 
         /// <summary>
-        /// Initializes a new instance of the <see cref="Semaphore"/> class with the specified number
-        /// of logical tokens.
+        /// Initializes a new instance of the <see cref="Semaphore"/> class
+        /// with the specified number of logical tokens.
         /// </summary>
-        /// <param name="tokens">The number of logical tokens to start with.</param>
+        /// <param name="tokens">
+        /// The number of logical tokens to start with.
+        /// </param>
         public Semaphore(int tokens)
         {
             this._tokenCount = tokens;
         /// <summary>
         /// Gets the object used for lock() blocks.
         /// </summary>
-        /// <remarks>See http://stackoverflow.com/questions/251391/why-is-lockthis-bad </remarks>
+        /// <remarks>
+        /// See http://stackoverflow.com/questions/251391/why-is-lockthis-bad
+        /// </remarks>
         protected object SyncRoot
         {
             get { return this._syncRoot; }
         /// </summary>
         public void Acquire()
         {
-            this.Attempt(-1);
+            this.Acquire(-1);
         }
 
         /// <summary>
         /// <exception cref="ArgumentOutOfRangeException">
         /// <paramref name="milliseconds"/> must be positive or -1.
         /// </exception>
-        public bool Attempt(int milliseconds)
+        public bool Acquire(int milliseconds)
         {
             if (milliseconds < -1 || milliseconds == 0)
             {
                 }
                 else
                 {
-                     timeLeft = (int)(endTime - DateTime.Now).TotalMilliseconds;
+                     timeLeft = (int)
+                         (endTime - DateTime.Now).TotalMilliseconds;
                 }
 
                 this._waitingThreads++;
                         }
                         else if (milliseconds != -1)
                         {
-                            timeLeft = (int)(endTime - DateTime.Now).TotalMilliseconds;
+                            timeLeft = (int)
+                                (endTime - DateTime.Now).TotalMilliseconds;
                         }
                     }
 
         /// <summary>
         /// Releases the specified number of logical tokens.
         /// </summary>
-        /// <param name="tokens">The number of logical tokens to release.</param>
-        /// <exception cref="ArgumentOutOfRangeException">tokens must be a positive number.</exception>
+        /// <param name="tokens">
+        /// The number of logical tokens to release.
+        /// </param>
+        /// <exception cref="ArgumentOutOfRangeException">
+        /// tokens must be a positive number.
+        /// </exception>
         public virtual void Release(int tokens)
         {
             if (tokens <= 0)