Skip to content

Commit

Permalink
Merge pull request #411 from nwestfall/asynclock_release
Browse files Browse the repository at this point in the history
Implement Disposable.Create in "AsyncLock" to prevent multiple disposes
  • Loading branch information
PureWeen authored Oct 1, 2018
2 parents 54ba92e + d6c95d6 commit 72321b9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/Akavache.Sqlite3/AsyncLock.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Reactive.Disposables;

namespace Akavache.Sqlite3.Internal
{
Expand All @@ -20,11 +21,11 @@ public AsyncLock()
var wait = m_semaphore.WaitAsync(ct);

// Happy path. We synchronously acquired the lock.
if (wait.IsCompleted && !wait.IsFaulted)
if (wait.IsCompleted && !wait.IsFaulted && !wait.IsCanceled)
return m_releaser;

return wait
.ContinueWith((_, state) => (IDisposable)state,
.ContinueWith((task, state) => task.IsCanceled ? null : (IDisposable)state,
m_releaser.Result, ct,
TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
}
Expand Down
18 changes: 15 additions & 3 deletions src/Akavache.Sqlite3/OperationQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public IDisposable Start()
break;
}

//Verify lock was acquired
if(@lock == null)
break;

using (@lock)
{
// NB: We special-case the first item because we want to
Expand Down Expand Up @@ -141,10 +145,14 @@ public IDisposable Start()
}
catch (OperationCanceledException) { }

using (flushLock.LockAsync().Result)
try
{
FlushInternal();
using(flushLock.LockAsync().Result)
{
FlushInternal();
}
}
catch(OperationCanceledException) { }

start = null;
}));
Expand Down Expand Up @@ -240,7 +248,11 @@ public AsyncSubject<Unit> Vacuum()
var lockTask = flushLock.LockAsync(shouldQuit.Token);
operationQueue.Add(OperationQueueItem.CreateUnit(OperationType.DoNothing));

@lock = await lockTask;
try
{
@lock = await lockTask;
}
catch(OperationCanceledException) { }

var deleteOp = OperationQueueItem.CreateUnit(OperationType.DeleteExpiredSqliteOperation);
operationQueue.Add(deleteOp);
Expand Down

0 comments on commit 72321b9

Please sign in to comment.