Replace manual reset event with asynch reset event for pausing the up/download agent
authorPanagiotis Kanavos <pkanavos@gmail.com>
Tue, 14 Feb 2012 10:29:25 +0000 (12:29 +0200)
committerPanagiotis Kanavos <pkanavos@gmail.com>
Tue, 14 Feb 2012 10:30:31 +0000 (12:30 +0200)
trunk/Pithos.Core/Agents/AsyncManualResetEvent.cs [new file with mode: 0644]
trunk/Pithos.Core/Agents/NetworkAgent.cs

diff --git a/trunk/Pithos.Core/Agents/AsyncManualResetEvent.cs b/trunk/Pithos.Core/Agents/AsyncManualResetEvent.cs
new file mode 100644 (file)
index 0000000..00ec851
--- /dev/null
@@ -0,0 +1,55 @@
+// -----------------------------------------------------------------------\r
+// <copyright file="AsyncManualResetEvent.cs" company="Microsoft">\r
+// TODO: Update copyright text.\r
+// </copyright>\r
+// -----------------------------------------------------------------------\r
+\r
+using System.Threading;\r
+using System.Threading.Tasks;\r
+\r
+namespace Pithos.Core.Agents\r
+{\r
+    using System;\r
+    using System.Collections.Generic;\r
+    using System.Linq;\r
+    using System.Text;\r
+\r
+    /// <summary>\r
+    /// TODO: Update summary.\r
+    /// </summary>\r
+    public class AsyncManualResetEvent\r
+    {\r
+        private volatile TaskCompletionSource<bool> _tcs;\r
+\r
+        public AsyncManualResetEvent()\r
+        {\r
+            _tcs = new TaskCompletionSource<bool>();\r
+        }\r
+\r
+        public AsyncManualResetEvent(bool signalled)\r
+        {            \r
+            _tcs = new TaskCompletionSource<bool>();\r
+            if (signalled)\r
+                _tcs.SetResult(true);\r
+        }\r
+        public Task WaitAsync() { return _tcs.Task; }\r
+\r
+        public void Wait()\r
+        {            \r
+            _tcs.Task.Wait();\r
+        }\r
+\r
+        public void Set() { _tcs.TrySetResult(true); }\r
+\r
+        public void Reset()\r
+        {\r
+            while (true)\r
+            {\r
+                var tcs = _tcs;\r
+                if (!tcs.Task.IsCompleted ||\r
+                    Interlocked.CompareExchange(ref _tcs, new TaskCompletionSource<bool>(), tcs) == tcs)\r
+                    return;\r
+            }\r
+        }\r
+    }\r
+}\r
index e3f2923..5151c4f 100644 (file)
@@ -65,7 +65,6 @@ namespace Pithos.Core.Agents
         readonly ConcurrentDictionary<string,DateTime> _deletedFiles=new ConcurrentDictionary<string, DateTime>();
 
 
-        private readonly ManualResetEventSlim _pauseAgent = new ManualResetEventSlim(true);
 
         [System.ComponentModel.Composition.Import]
         public IStatusKeeper StatusKeeper { get; set; }
@@ -80,8 +79,11 @@ namespace Pithos.Core.Agents
         public IPithosSettings Settings { get; set; }
 
         private bool _firstPoll = true;
+        
         private TaskCompletionSource<bool> _tcs;
-        private ConcurrentDictionary<string,DateTime> _lastSeen=new ConcurrentDictionary<string, DateTime>();
+        private readonly AsyncManualResetEvent _pauseAgent = new AsyncManualResetEvent(true);
+
+        private ConcurrentDictionary<string, DateTime> _lastSeen = new ConcurrentDictionary<string, DateTime>();
 
         public void Start()
         {
@@ -522,7 +524,7 @@ namespace Pithos.Core.Agents
 
             using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push(accountInfo.UserName))
             {
-                _pauseAgent.Wait();
+                await _pauseAgent.WaitAsync();
 
                 Log.Info("Scheduled");
                 var client = new CloudFilesClient(accountInfo)
@@ -537,7 +539,7 @@ namespace Pithos.Core.Agents
 
                 try
                 {
-                    _pauseAgent.Wait();
+                    await _pauseAgent.WaitAsync();
                     //Get the poll time now. We may miss some deletions but it's better to keep a file that was deleted
                     //than delete a file that was created while we were executing the poll                    
                     var pollTime = DateTime.Now;