part of the code formatter fix and minor bug fixes

This commit is contained in:
Stan44 2025-05-11 22:48:33 -05:00
parent 01d2cbf4b3
commit 5352e907b9
6 changed files with 41 additions and 9 deletions

View File

@ -14,7 +14,6 @@ namespace AdvChkSys.Resources
{
// Example: Track allocated chunks (for diagnostics, pooling, or resource limits)
private static readonly ConcurrentDictionary<IChunk, DateTime> _allocatedChunks = new();
private static int _allocatedChunkCount;
private static readonly object _lock = new();
private static readonly HashSet<IChunk> _activeChunks = new();
@ -26,8 +25,8 @@ namespace AdvChkSys.Resources
{
lock (_lock)
{
_allocatedChunkCount++;
_activeChunks.Add(chunk);
_allocatedChunks[chunk] = DateTime.UtcNow;
}
}
@ -39,8 +38,8 @@ namespace AdvChkSys.Resources
{
lock (_lock)
{
_allocatedChunkCount--;
_activeChunks.Remove(chunk);
_allocatedChunks.TryRemove(chunk, out _);
}
}
@ -55,6 +54,10 @@ namespace AdvChkSys.Resources
public static void Clear()
{
_allocatedChunks.Clear();
lock (_lock)
{
_activeChunks.Clear();
}
}
/// <summary>

View File

@ -614,9 +614,6 @@ namespace AdvChkSys.Spatial
// Maximum number of chunks before splitting
private const int MAX_CHUNKS = 8;
// Minimum size of a node
private const int MIN_SIZE = 1;
/// <summary>
/// Initializes a new instance of the QuadtreeNode class.
/// </summary>

View File

@ -145,9 +145,15 @@ namespace AdvChkSys.Threading
{
try
{
// Check for cancellation before processing
cancellationToken.ThrowIfCancellationRequested();
// Process the chunk
await processor(chunk).ConfigureAwait(false);
// Check for cancellation after processing but before updating dependencies
cancellationToken.ThrowIfCancellationRequested();
// Mark as completed
completed[chunk] = true;
@ -155,6 +161,9 @@ namespace AdvChkSys.Threading
var dependents = graph.GetDependents(chunk);
foreach (var dependent in dependents)
{
// Check for cancellation during dependency processing
cancellationToken.ThrowIfCancellationRequested();
// Check if all dependencies are completed
var dependencies = graph.GetDependencies(dependent);
if (dependencies.All(d => completed.ContainsKey(d)))

View File

@ -36,7 +36,7 @@ namespace AdvChkSys.Threading
/// <param name="action">The action to perform</param>
public static void TrackOperation(string operationName, Action action)
{
var operationId = Guid.NewGuid();
_ = Guid.NewGuid();
var startTime = _stopwatch.ElapsedMilliseconds;
try

View File

@ -40,6 +40,10 @@ namespace AdvChkSys.Threading
int batchSize = Math.Max(1, (int)Math.Sqrt(coordinates.Length));
int batchCount = (coordinates.Length + batchSize - 1) / batchSize;
// Use the maxDegreeOfParallelism parameter to limit concurrent tasks
int parallelism = maxDegreeOfParallelism ?? ChunkThreadingConfiguration.DefaultMaxDegreeOfParallelism;
var semaphore = new SemaphoreSlim(parallelism, parallelism);
for (int i = 0; i < batchCount; i++)
{
if (cancellationToken.IsCancellationRequested)
@ -56,7 +60,23 @@ namespace AdvChkSys.Threading
var chunk = manager.GetChunk(x, y);
if (chunk != null)
{
tasks.Add(processor(chunk));
// Wait for a slot in the semaphore before starting a new task
await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
// Create a task that releases the semaphore when done
var task = Task.Run(async () =>
{
try
{
await processor(chunk).ConfigureAwait(false);
}
finally
{
semaphore.Release();
}
}, cancellationToken);
tasks.Add(task);
}
}
@ -65,6 +85,9 @@ namespace AdvChkSys.Threading
await Task.WhenAll(tasks).ConfigureAwait(false);
}
}
// Clean up the semaphore
semaphore.Dispose();
}
/// <summary>

View File

@ -115,7 +115,7 @@ namespace AdvChkSys.Util
{
lock (_lock)
{
foreach (var (key, value) in _lruList)
foreach (var (_, value) in _lruList)
yield return value;
}
}