update save methods

This commit is contained in:
Luke Pulverenti
2016-11-19 03:40:13 -05:00
parent ffb1ec76a7
commit 52227ce00d
9 changed files with 117 additions and 60 deletions

View File

@@ -12,7 +12,7 @@ namespace Emby.Server.Implementations.Data
public abstract class BaseSqliteRepository : IDisposable
{
protected string DbFilePath { get; set; }
protected SemaphoreSlim WriteLock = new SemaphoreSlim(1, 1);
protected ReaderWriterLockSlim WriteLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
protected ILogger Logger { get; private set; }
protected BaseSqliteRepository(ILogger logger)
@@ -130,9 +130,10 @@ namespace Emby.Server.Implementations.Data
{
lock (_disposeLock)
{
WriteLock.Wait();
CloseConnection();
using (WriteLock.Write())
{
CloseConnection();
}
}
}
catch (Exception ex)
@@ -178,4 +179,51 @@ namespace Emby.Server.Implementations.Data
}));
}
}
public static class ReaderWriterLockSlimExtensions
{
private sealed class ReadLockToken : IDisposable
{
private ReaderWriterLockSlim _sync;
public ReadLockToken(ReaderWriterLockSlim sync)
{
_sync = sync;
sync.EnterReadLock();
}
public void Dispose()
{
if (_sync != null)
{
_sync.ExitReadLock();
_sync = null;
}
}
}
private sealed class WriteLockToken : IDisposable
{
private ReaderWriterLockSlim _sync;
public WriteLockToken(ReaderWriterLockSlim sync)
{
_sync = sync;
sync.EnterWriteLock();
}
public void Dispose()
{
if (_sync != null)
{
_sync.ExitWriteLock();
_sync = null;
}
}
}
public static IDisposable Read(this ReaderWriterLockSlim obj)
{
return new ReadLockToken(obj);
}
public static IDisposable Write(this ReaderWriterLockSlim obj)
{
return new WriteLockToken(obj);
}
}
}