From 402b25a436a6d08da5caf84b78891d066ade6483 Mon Sep 17 00:00:00 2001 From: ikpil Date: Fri, 12 Apr 2024 00:08:06 +0900 Subject: [PATCH] fixed an issue where await deadlock could occur in various platform environments. --- .../Builder/TileNavMeshBuilder.cs | 3 +-- src/DotRecast.Recast/RcBuilder.cs | 15 +++++++-------- .../TestTiledNavMeshBuilder.cs | 3 +-- test/DotRecast.Recast.Test/RecastTileMeshTest.cs | 3 +-- 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/DotRecast.Recast.Toolset/Builder/TileNavMeshBuilder.cs b/src/DotRecast.Recast.Toolset/Builder/TileNavMeshBuilder.cs index 836877f..44a4945 100644 --- a/src/DotRecast.Recast.Toolset/Builder/TileNavMeshBuilder.cs +++ b/src/DotRecast.Recast.Toolset/Builder/TileNavMeshBuilder.cs @@ -97,8 +97,7 @@ namespace DotRecast.Recast.Toolset.Builder filterLowHangingObstacles, filterLedgeSpans, filterWalkableLowHeightSpans, SampleAreaModifications.SAMPLE_AREAMOD_WALKABLE, true); RcBuilder rcBuilder = new RcBuilder(); - var task = rcBuilder.BuildTilesAsync(geom, cfg, Environment.ProcessorCount + 1, Task.Factory); - return task.Result; + return rcBuilder.BuildTiles(geom, cfg, Environment.ProcessorCount + 1, Task.Factory); } public DtNavMesh BuildNavMesh(IInputGeomProvider geom, List meshData, float cellSize, int tileSize, int vertsPerPoly) diff --git a/src/DotRecast.Recast/RcBuilder.cs b/src/DotRecast.Recast/RcBuilder.cs index 129cf2f..9123b92 100644 --- a/src/DotRecast.Recast/RcBuilder.cs +++ b/src/DotRecast.Recast/RcBuilder.cs @@ -47,7 +47,7 @@ namespace DotRecast.Recast _progressListener = progressListener; } - public Task> BuildTilesAsync(IInputGeomProvider geom, RcConfig cfg, + public List BuildTiles(IInputGeomProvider geom, RcConfig cfg, int threads = 0, TaskFactory taskFactory = null, CancellationToken cancellation = default) { RcVec3f bmin = geom.GetMeshBoundsMin(); @@ -56,11 +56,10 @@ namespace DotRecast.Recast if (1 < threads) { - return BuildMultiThreadAsync(geom, cfg, bmin, bmax, tw, th, threads, taskFactory ?? Task.Factory, cancellation); + return BuildMultiThread(geom, cfg, bmin, bmax, tw, th, threads, taskFactory ?? Task.Factory, cancellation); } - var results = BuildSingleThread(geom, cfg, bmin, bmax, tw, th); - return Task.FromResult(results); + return BuildSingleThread(geom, cfg, bmin, bmax, tw, th); } private List BuildSingleThread(IInputGeomProvider geom, RcConfig cfg, RcVec3f bmin, RcVec3f bmax, int tw, int th) @@ -80,7 +79,7 @@ namespace DotRecast.Recast return results; } - private async Task> BuildMultiThreadAsync(IInputGeomProvider geom, RcConfig cfg, RcVec3f bmin, RcVec3f bmax, int tw, int th, + private List BuildMultiThread(IInputGeomProvider geom, RcConfig cfg, RcVec3f bmin, RcVec3f bmax, int tw, int th, int threads, TaskFactory taskFactory, CancellationToken cancellation) { var results = new ConcurrentQueue(); @@ -107,12 +106,12 @@ namespace DotRecast.Recast { Console.WriteLine(e); } - }, null); + }, null, cancellation); limits.Add(task); if (threads <= limits.Count) { - await Task.WhenAll(limits); + Task.WaitAll(limits.ToArray()); limits.Clear(); } } @@ -120,7 +119,7 @@ namespace DotRecast.Recast if (0 < limits.Count) { - await Task.WhenAll(limits); + Task.WaitAll(limits.ToArray()); limits.Clear(); } diff --git a/test/DotRecast.Detour.Test/TestTiledNavMeshBuilder.cs b/test/DotRecast.Detour.Test/TestTiledNavMeshBuilder.cs index 3e29804..835c247 100644 --- a/test/DotRecast.Detour.Test/TestTiledNavMeshBuilder.cs +++ b/test/DotRecast.Detour.Test/TestTiledNavMeshBuilder.cs @@ -78,8 +78,7 @@ public class TestTiledNavMeshBuilder true, true, true, SampleAreaModifications.SAMPLE_AREAMOD_GROUND, true); RcBuilder rcBuilder = new RcBuilder(); - var task = rcBuilder.BuildTilesAsync(geom, cfg); - List rcResult = task.Result; + List rcResult = rcBuilder.BuildTiles(geom, cfg); // Add tiles to nav mesh diff --git a/test/DotRecast.Recast.Test/RecastTileMeshTest.cs b/test/DotRecast.Recast.Test/RecastTileMeshTest.cs index c8d3a96..2abb9df 100644 --- a/test/DotRecast.Recast.Test/RecastTileMeshTest.cs +++ b/test/DotRecast.Recast.Test/RecastTileMeshTest.cs @@ -137,8 +137,7 @@ public class RecastTileMeshTest private void Build(IInputGeomProvider geom, RcBuilder builder, RcConfig cfg, int threads, bool validate) { CancellationTokenSource cts = new CancellationTokenSource(); - var task = builder.BuildTilesAsync(geom, cfg, threads, Task.Factory, cts.Token); - List tiles = task.Result; + List tiles = builder.BuildTiles(geom, cfg, threads, Task.Factory, cts.Token); if (validate) { RcBuilderResult rcResult = GetTile(tiles, 7, 8);