solve edge case

This commit is contained in:
dennisarfan 2025-08-01 09:51:39 +07:00
parent 741d34a5e0
commit 0472bfe58e
15 changed files with 685 additions and 47 deletions

View file

@ -1,4 +1,3 @@
using System.Buffers;
using System.IO.MemoryMappedFiles;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@ -19,7 +18,7 @@ public class ImageIntegral : IDisposable
private readonly int _width;
private readonly int _height;
private IMemoryOwner<ManualResetEventSlim>? _rowLocks;
private ManualResetEventSlim[]? _rowLocks;
private MemoryMappedFile? _memoryMappedFile;
private readonly Lock _lock = new();
@ -66,7 +65,7 @@ public class ImageIntegral : IDisposable
}
}
_rowLocks?.Memory.Span[row].Wait(cancellationToken);
_rowLocks?[row].Wait(cancellationToken);
}
private void Initialize(CancellationToken cancellationToken)
@ -101,12 +100,11 @@ public class ImageIntegral : IDisposable
// initialize resource gating, all rows is expected to be locked
// if the backed file require to allocate, it should be safe to do this
// asynchronously
var rowLocks = MemoryAllocator.AllocateManaged<ManualResetEventSlim>(_height);
var rowLocksSpan = rowLocks.Memory.Span;
var rowLocks = new ManualResetEventSlim[_height];
for (int i = 0; i < _height; i++)
{
var isOpen = i < header.ProcessedRows;
rowLocksSpan[i] = new ManualResetEventSlim(isOpen);
rowLocks[i] = new ManualResetEventSlim(isOpen);
}
_rowLocks = rowLocks;
@ -222,7 +220,7 @@ public class ImageIntegral : IDisposable
view.DangerousWriteSpan(0, writeBuffer.Span, 0, _width);
writeBuffer.Dispose();
_rowLocks!.Memory.Span[row].Set();
_rowLocks![row].Set();
Interlocked.Increment(ref _processedRows);
using (var view = AcquireHeaderView(MemoryMappedFileAccess.Write))
@ -370,11 +368,8 @@ public class ImageIntegral : IDisposable
if (_rowLocks is { } locks)
{
_rowLocks = null;
var lockSpan = locks.Memory.Span;
for(int i = 0; i < _height; i++)
lockSpan[i].Dispose();
locks.Dispose();
locks[i].Dispose();
}
}