From df6bd0e394d6d46e8d8cb31cfb3a6d4409ab1eb9 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Sun, 28 Jul 2019 20:25:52 -0700 Subject: [PATCH] thread safe drawlayermanager --- encompass-cs/DrawLayerManager.cs | 49 +++++++++++++++++++------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/encompass-cs/DrawLayerManager.cs b/encompass-cs/DrawLayerManager.cs index d6082dc..e60fe4e 100644 --- a/encompass-cs/DrawLayerManager.cs +++ b/encompass-cs/DrawLayerManager.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; @@ -6,32 +7,37 @@ namespace Encompass { internal class DrawLayerManager { + private readonly object layerOrderLock = new object(); + private readonly SortedList layerOrder = new SortedList(); - private readonly Dictionary> layerIndexToComponentIDs = new Dictionary>(); - private readonly Dictionary> layerIndexToGeneralRenderers = new Dictionary>(); + private readonly ConcurrentDictionary> layerIndexToComponentIDs = new ConcurrentDictionary>(); + private readonly ConcurrentDictionary> layerIndexToGeneralRenderers = new ConcurrentDictionary>(); - private readonly Dictionary componentIDToLayerIndex = new Dictionary(); + private readonly ConcurrentDictionary componentIDToLayerIndex = new ConcurrentDictionary(); - public IEnumerable LayerOrder { get { return layerOrder.Values; } } + public IEnumerable LayerOrder { get { lock(layerOrderLock) { return layerOrder.Values; } } } public void RegisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer) { if (layerIndexToGeneralRenderers.ContainsKey(layer)) { var set = layerIndexToGeneralRenderers[layer]; - set.Add(renderer); + set[renderer] = 0; } else { - var set = new HashSet(); - layerIndexToGeneralRenderers.Add(layer, set); - set.Add(renderer); + var set = new ConcurrentDictionary(); + layerIndexToGeneralRenderers[layer] = set; + set[renderer] = 0; } - if (!layerOrder.ContainsKey(layer)) + lock (layerOrderLock) { - layerOrder.Add(layer, layer); + if (!layerOrder.ContainsKey(layer)) + { + layerOrder.Add(layer, layer); + } } } @@ -39,7 +45,7 @@ namespace Encompass { if (layerIndexToGeneralRenderers.ContainsKey(layer)) { - layerIndexToGeneralRenderers[layer].Remove(renderer); + layerIndexToGeneralRenderers[layer].TryRemove(renderer, out _); } } @@ -54,20 +60,23 @@ namespace Encompass if (layerIndexToComponentIDs.ContainsKey(layer)) { var set = layerIndexToComponentIDs[layer]; - set.Add(id); + set[id] = 0; } else { - var set = new HashSet(); - layerIndexToComponentIDs.Add(layer, set); - set.Add(id); + var set = new ConcurrentDictionary(); + layerIndexToComponentIDs[layer] = set; + set[id] = 0; } componentIDToLayerIndex[id] = layer; - if (!layerOrder.ContainsKey(layer)) + lock (layerOrderLock) { - layerOrder.Add(layer, layer); + if (!layerOrder.ContainsKey(layer)) + { + layerOrder.Add(layer, layer); + } } } @@ -76,7 +85,7 @@ namespace Encompass if (componentIDToLayerIndex.ContainsKey(id)) { var layer = componentIDToLayerIndex[id]; - layerIndexToComponentIDs[layer].Remove(id); + layerIndexToComponentIDs[layer].TryRemove(id, out _); } } @@ -89,14 +98,14 @@ namespace Encompass public IEnumerable ComponentIDsByLayer(int layer) { return layerIndexToComponentIDs.ContainsKey(layer) ? - layerIndexToComponentIDs[layer] : + layerIndexToComponentIDs[layer].Keys : Enumerable.Empty(); } public IEnumerable GeneralRenderersByLayer(int layer) { return layerIndexToGeneralRenderers.ContainsKey(layer) ? - layerIndexToGeneralRenderers[layer] : + layerIndexToGeneralRenderers[layer].Keys : Enumerable.Empty(); } }