Reorder methods and add xmldoc for `BlockAllOperations`

This commit is contained in:
Dean Herbert 2021-06-28 16:14:14 +09:00
parent 90f0bc87f5
commit f78cedd0e1
1 changed files with 37 additions and 28 deletions

View File

@ -76,10 +76,26 @@ public RealmWriteUsage GetForWrite()
return new RealmWriteUsage(createContext(), writeComplete);
}
private void writeComplete()
/// <summary>
/// Flush any active contexts and block any further writes.
/// </summary>
/// <remarks>
/// This should be used in places we need to ensure no ongoing reads/writes are occurring with realm.
/// ie. to move the realm backing file to a new location.
/// </remarks>
/// <returns>An <see cref="IDisposable"/> which should be disposed to end the blocking section.</returns>
/// <exception cref="InvalidOperationException">Thrown if this context is already disposed.</exception>
public IDisposable BlockAllOperations()
{
Monitor.Exit(writeLock);
pending_writes.Value--;
if (IsDisposed)
throw new InvalidOperationException(@"Attempted to block operations after already disposed.");
blockingLock.Wait();
flushContexts();
return new InvokeOnDisposal<RealmContextFactory>(this, endBlockingSection);
static void endBlockingSection(RealmContextFactory factory) => factory.blockingLock.Release();
}
protected override void Update()
@ -113,6 +129,12 @@ private Realm createContext()
}
}
private void writeComplete()
{
Monitor.Exit(writeLock);
pending_writes.Value--;
}
private void onMigration(Migration migration, ulong lastSchemaVersion)
{
switch (lastSchemaVersion)
@ -124,31 +146,6 @@ private void onMigration(Migration migration, ulong lastSchemaVersion)
}
}
protected override void Dispose(bool isDisposing)
{
if (!IsDisposed)
{
// intentionally block all operations indefinitely. this ensures that nothing can start consuming a new context after disposal.
BlockAllOperations();
blockingLock?.Dispose();
}
base.Dispose(isDisposing);
}
public IDisposable BlockAllOperations()
{
if (IsDisposed)
throw new InvalidOperationException(@"Attempted to block operations after already disposed.");
blockingLock.Wait();
flushContexts();
return new InvokeOnDisposal<RealmContextFactory>(this, endBlockingSection);
static void endBlockingSection(RealmContextFactory factory) => factory.blockingLock.Release();
}
private void flushContexts()
{
var previousContext = context;
@ -161,6 +158,18 @@ private void flushContexts()
previousContext?.Dispose();
}
protected override void Dispose(bool isDisposing)
{
if (!IsDisposed)
{
// intentionally block all operations indefinitely. this ensures that nothing can start consuming a new context after disposal.
BlockAllOperations();
blockingLock?.Dispose();
}
base.Dispose(isDisposing);
}
/// <summary>
/// A usage of realm from an arbitrary thread.
/// </summary>