From 303596f99c95f826b9e61b34d8de5946fa272880 Mon Sep 17 00:00:00 2001 From: Panagiotis Kanavos Date: Tue, 14 Feb 2012 12:29:25 +0200 Subject: [PATCH] Replace manual reset event with asynch reset event for pausing the up/download agent --- trunk/Pithos.Core/Agents/AsyncManualResetEvent.cs | 55 +++++++++++++++++++++ trunk/Pithos.Core/Agents/NetworkAgent.cs | 10 ++-- 2 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 trunk/Pithos.Core/Agents/AsyncManualResetEvent.cs diff --git a/trunk/Pithos.Core/Agents/AsyncManualResetEvent.cs b/trunk/Pithos.Core/Agents/AsyncManualResetEvent.cs new file mode 100644 index 0000000..00ec851 --- /dev/null +++ b/trunk/Pithos.Core/Agents/AsyncManualResetEvent.cs @@ -0,0 +1,55 @@ +// ----------------------------------------------------------------------- +// +// TODO: Update copyright text. +// +// ----------------------------------------------------------------------- + +using System.Threading; +using System.Threading.Tasks; + +namespace Pithos.Core.Agents +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + + /// + /// TODO: Update summary. + /// + public class AsyncManualResetEvent + { + private volatile TaskCompletionSource _tcs; + + public AsyncManualResetEvent() + { + _tcs = new TaskCompletionSource(); + } + + public AsyncManualResetEvent(bool signalled) + { + _tcs = new TaskCompletionSource(); + if (signalled) + _tcs.SetResult(true); + } + public Task WaitAsync() { return _tcs.Task; } + + public void Wait() + { + _tcs.Task.Wait(); + } + + public void Set() { _tcs.TrySetResult(true); } + + public void Reset() + { + while (true) + { + var tcs = _tcs; + if (!tcs.Task.IsCompleted || + Interlocked.CompareExchange(ref _tcs, new TaskCompletionSource(), tcs) == tcs) + return; + } + } + } +} diff --git a/trunk/Pithos.Core/Agents/NetworkAgent.cs b/trunk/Pithos.Core/Agents/NetworkAgent.cs index e3f2923..5151c4f 100644 --- a/trunk/Pithos.Core/Agents/NetworkAgent.cs +++ b/trunk/Pithos.Core/Agents/NetworkAgent.cs @@ -65,7 +65,6 @@ namespace Pithos.Core.Agents readonly ConcurrentDictionary _deletedFiles=new ConcurrentDictionary(); - 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 _tcs; - private ConcurrentDictionary _lastSeen=new ConcurrentDictionary(); + private readonly AsyncManualResetEvent _pauseAgent = new AsyncManualResetEvent(true); + + private ConcurrentDictionary _lastSeen = new ConcurrentDictionary(); 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; -- 1.7.10.4