fixed an issue where await deadlock could occur in various platform environments.

This commit is contained in:
ikpil 2024-04-12 00:08:06 +09:00
parent 7874b4403c
commit 402b25a436
4 changed files with 10 additions and 14 deletions

View File

@ -97,8 +97,7 @@ namespace DotRecast.Recast.Toolset.Builder
filterLowHangingObstacles, filterLedgeSpans, filterWalkableLowHeightSpans, filterLowHangingObstacles, filterLedgeSpans, filterWalkableLowHeightSpans,
SampleAreaModifications.SAMPLE_AREAMOD_WALKABLE, true); SampleAreaModifications.SAMPLE_AREAMOD_WALKABLE, true);
RcBuilder rcBuilder = new RcBuilder(); RcBuilder rcBuilder = new RcBuilder();
var task = rcBuilder.BuildTilesAsync(geom, cfg, Environment.ProcessorCount + 1, Task.Factory); return rcBuilder.BuildTiles(geom, cfg, Environment.ProcessorCount + 1, Task.Factory);
return task.Result;
} }
public DtNavMesh BuildNavMesh(IInputGeomProvider geom, List<DtMeshData> meshData, float cellSize, int tileSize, int vertsPerPoly) public DtNavMesh BuildNavMesh(IInputGeomProvider geom, List<DtMeshData> meshData, float cellSize, int tileSize, int vertsPerPoly)

View File

@ -47,7 +47,7 @@ namespace DotRecast.Recast
_progressListener = progressListener; _progressListener = progressListener;
} }
public Task<List<RcBuilderResult>> BuildTilesAsync(IInputGeomProvider geom, RcConfig cfg, public List<RcBuilderResult> BuildTiles(IInputGeomProvider geom, RcConfig cfg,
int threads = 0, TaskFactory taskFactory = null, CancellationToken cancellation = default) int threads = 0, TaskFactory taskFactory = null, CancellationToken cancellation = default)
{ {
RcVec3f bmin = geom.GetMeshBoundsMin(); RcVec3f bmin = geom.GetMeshBoundsMin();
@ -56,11 +56,10 @@ namespace DotRecast.Recast
if (1 < threads) 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 BuildSingleThread(geom, cfg, bmin, bmax, tw, th);
return Task.FromResult(results);
} }
private List<RcBuilderResult> BuildSingleThread(IInputGeomProvider geom, RcConfig cfg, RcVec3f bmin, RcVec3f bmax, int tw, int th) private List<RcBuilderResult> BuildSingleThread(IInputGeomProvider geom, RcConfig cfg, RcVec3f bmin, RcVec3f bmax, int tw, int th)
@ -80,7 +79,7 @@ namespace DotRecast.Recast
return results; return results;
} }
private async Task<List<RcBuilderResult>> BuildMultiThreadAsync(IInputGeomProvider geom, RcConfig cfg, RcVec3f bmin, RcVec3f bmax, int tw, int th, private List<RcBuilderResult> BuildMultiThread(IInputGeomProvider geom, RcConfig cfg, RcVec3f bmin, RcVec3f bmax, int tw, int th,
int threads, TaskFactory taskFactory, CancellationToken cancellation) int threads, TaskFactory taskFactory, CancellationToken cancellation)
{ {
var results = new ConcurrentQueue<RcBuilderResult>(); var results = new ConcurrentQueue<RcBuilderResult>();
@ -107,12 +106,12 @@ namespace DotRecast.Recast
{ {
Console.WriteLine(e); Console.WriteLine(e);
} }
}, null); }, null, cancellation);
limits.Add(task); limits.Add(task);
if (threads <= limits.Count) if (threads <= limits.Count)
{ {
await Task.WhenAll(limits); Task.WaitAll(limits.ToArray());
limits.Clear(); limits.Clear();
} }
} }
@ -120,7 +119,7 @@ namespace DotRecast.Recast
if (0 < limits.Count) if (0 < limits.Count)
{ {
await Task.WhenAll(limits); Task.WaitAll(limits.ToArray());
limits.Clear(); limits.Clear();
} }

View File

@ -78,8 +78,7 @@ public class TestTiledNavMeshBuilder
true, true, true, true, true, true,
SampleAreaModifications.SAMPLE_AREAMOD_GROUND, true); SampleAreaModifications.SAMPLE_AREAMOD_GROUND, true);
RcBuilder rcBuilder = new RcBuilder(); RcBuilder rcBuilder = new RcBuilder();
var task = rcBuilder.BuildTilesAsync(geom, cfg); List<RcBuilderResult> rcResult = rcBuilder.BuildTiles(geom, cfg);
List<RcBuilderResult> rcResult = task.Result;
// Add tiles to nav mesh // Add tiles to nav mesh

View File

@ -137,8 +137,7 @@ public class RecastTileMeshTest
private void Build(IInputGeomProvider geom, RcBuilder builder, RcConfig cfg, int threads, bool validate) private void Build(IInputGeomProvider geom, RcBuilder builder, RcConfig cfg, int threads, bool validate)
{ {
CancellationTokenSource cts = new CancellationTokenSource(); CancellationTokenSource cts = new CancellationTokenSource();
var task = builder.BuildTilesAsync(geom, cfg, threads, Task.Factory, cts.Token); List<RcBuilderResult> tiles = builder.BuildTiles(geom, cfg, threads, Task.Factory, cts.Token);
List<RcBuilderResult> tiles = task.Result;
if (validate) if (validate)
{ {
RcBuilderResult rcResult = GetTile(tiles, 7, 8); RcBuilderResult rcResult = GetTile(tiles, 7, 8);