From b3d63cf2efaed07bfa66d5d145d9e84e130a2e55 Mon Sep 17 00:00:00 2001 From: nopjmp Date: Sat, 26 Oct 2019 16:56:09 -0500 Subject: [PATCH] Queue multiple textures if we have room Additionally, fix a potential bug where a thread is not running and does not update the State to Progress, so we want to count them as active. --- LibreMetaverse/TexturePipeline.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/LibreMetaverse/TexturePipeline.cs b/LibreMetaverse/TexturePipeline.cs index cd5dab37..581877b2 100644 --- a/LibreMetaverse/TexturePipeline.cs +++ b/LibreMetaverse/TexturePipeline.cs @@ -28,6 +28,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading; using OpenMetaverse.Packets; using OpenMetaverse.Assets; @@ -466,21 +467,20 @@ namespace OpenMetaverse while (_Running) { // find free slots - int pending = 0; int active = 0; - TaskInfo nextTask = null; + var pendingTasks = new Queue(); lock (_Transfers) { - foreach (KeyValuePair request in _Transfers) + foreach (var request in _Transfers) { switch (request.Value.State) { case TextureRequestState.Pending: - nextTask = request.Value; - ++pending; + pendingTasks.Enqueue(request.Value); break; + case TextureRequestState.Started: case TextureRequestState.Progress: ++active; break; @@ -488,14 +488,15 @@ namespace OpenMetaverse } } - if (pending > 0 && active <= maxTextureRequests) + // NOTE: use TryDequeue once we can use .NET Standard 2.1 + while (active <= maxTextureRequests && pendingTasks.Any()) { + var nextTask = pendingTasks.Dequeue(); nextTask.State = TextureRequestState.Started; - //Logger.DebugLog(String.Format("Sending Worker thread new download request {0}", slot)); ThreadPool.QueueUserWorkItem(TextureRequestDoWork, nextTask); - continue; + ++active; } // Queue was empty or all download slots are inuse, let's give up some CPU time