part of the code formatter fix and minor bug fixes
This commit is contained in:
parent
01d2cbf4b3
commit
5352e907b9
@ -14,7 +14,6 @@ namespace AdvChkSys.Resources
|
|||||||
{
|
{
|
||||||
// Example: Track allocated chunks (for diagnostics, pooling, or resource limits)
|
// Example: Track allocated chunks (for diagnostics, pooling, or resource limits)
|
||||||
private static readonly ConcurrentDictionary<IChunk, DateTime> _allocatedChunks = new();
|
private static readonly ConcurrentDictionary<IChunk, DateTime> _allocatedChunks = new();
|
||||||
private static int _allocatedChunkCount;
|
|
||||||
private static readonly object _lock = new();
|
private static readonly object _lock = new();
|
||||||
private static readonly HashSet<IChunk> _activeChunks = new();
|
private static readonly HashSet<IChunk> _activeChunks = new();
|
||||||
|
|
||||||
@ -26,8 +25,8 @@ namespace AdvChkSys.Resources
|
|||||||
{
|
{
|
||||||
lock (_lock)
|
lock (_lock)
|
||||||
{
|
{
|
||||||
_allocatedChunkCount++;
|
|
||||||
_activeChunks.Add(chunk);
|
_activeChunks.Add(chunk);
|
||||||
|
_allocatedChunks[chunk] = DateTime.UtcNow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,8 +38,8 @@ namespace AdvChkSys.Resources
|
|||||||
{
|
{
|
||||||
lock (_lock)
|
lock (_lock)
|
||||||
{
|
{
|
||||||
_allocatedChunkCount--;
|
|
||||||
_activeChunks.Remove(chunk);
|
_activeChunks.Remove(chunk);
|
||||||
|
_allocatedChunks.TryRemove(chunk, out _);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,6 +54,10 @@ namespace AdvChkSys.Resources
|
|||||||
public static void Clear()
|
public static void Clear()
|
||||||
{
|
{
|
||||||
_allocatedChunks.Clear();
|
_allocatedChunks.Clear();
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
_activeChunks.Clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -614,9 +614,6 @@ namespace AdvChkSys.Spatial
|
|||||||
// Maximum number of chunks before splitting
|
// Maximum number of chunks before splitting
|
||||||
private const int MAX_CHUNKS = 8;
|
private const int MAX_CHUNKS = 8;
|
||||||
|
|
||||||
// Minimum size of a node
|
|
||||||
private const int MIN_SIZE = 1;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the QuadtreeNode class.
|
/// Initializes a new instance of the QuadtreeNode class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -145,9 +145,15 @@ namespace AdvChkSys.Threading
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
// Check for cancellation before processing
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
// Process the chunk
|
// Process the chunk
|
||||||
await processor(chunk).ConfigureAwait(false);
|
await processor(chunk).ConfigureAwait(false);
|
||||||
|
|
||||||
|
// Check for cancellation after processing but before updating dependencies
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
// Mark as completed
|
// Mark as completed
|
||||||
completed[chunk] = true;
|
completed[chunk] = true;
|
||||||
|
|
||||||
@ -155,6 +161,9 @@ namespace AdvChkSys.Threading
|
|||||||
var dependents = graph.GetDependents(chunk);
|
var dependents = graph.GetDependents(chunk);
|
||||||
foreach (var dependent in dependents)
|
foreach (var dependent in dependents)
|
||||||
{
|
{
|
||||||
|
// Check for cancellation during dependency processing
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
// Check if all dependencies are completed
|
// Check if all dependencies are completed
|
||||||
var dependencies = graph.GetDependencies(dependent);
|
var dependencies = graph.GetDependencies(dependent);
|
||||||
if (dependencies.All(d => completed.ContainsKey(d)))
|
if (dependencies.All(d => completed.ContainsKey(d)))
|
||||||
|
|||||||
@ -36,7 +36,7 @@ namespace AdvChkSys.Threading
|
|||||||
/// <param name="action">The action to perform</param>
|
/// <param name="action">The action to perform</param>
|
||||||
public static void TrackOperation(string operationName, Action action)
|
public static void TrackOperation(string operationName, Action action)
|
||||||
{
|
{
|
||||||
var operationId = Guid.NewGuid();
|
_ = Guid.NewGuid();
|
||||||
var startTime = _stopwatch.ElapsedMilliseconds;
|
var startTime = _stopwatch.ElapsedMilliseconds;
|
||||||
|
|
||||||
try
|
try
|
||||||
|
|||||||
@ -40,6 +40,10 @@ namespace AdvChkSys.Threading
|
|||||||
int batchSize = Math.Max(1, (int)Math.Sqrt(coordinates.Length));
|
int batchSize = Math.Max(1, (int)Math.Sqrt(coordinates.Length));
|
||||||
int batchCount = (coordinates.Length + batchSize - 1) / batchSize;
|
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++)
|
for (int i = 0; i < batchCount; i++)
|
||||||
{
|
{
|
||||||
if (cancellationToken.IsCancellationRequested)
|
if (cancellationToken.IsCancellationRequested)
|
||||||
@ -56,7 +60,23 @@ namespace AdvChkSys.Threading
|
|||||||
var chunk = manager.GetChunk(x, y);
|
var chunk = manager.GetChunk(x, y);
|
||||||
if (chunk != null)
|
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);
|
await Task.WhenAll(tasks).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean up the semaphore
|
||||||
|
semaphore.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -115,7 +115,7 @@ namespace AdvChkSys.Util
|
|||||||
{
|
{
|
||||||
lock (_lock)
|
lock (_lock)
|
||||||
{
|
{
|
||||||
foreach (var (key, value) in _lruList)
|
foreach (var (_, value) in _lruList)
|
||||||
yield return value;
|
yield return value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user