Commit a90c6ddb authored by Michael Denny's avatar Michael Denny

fix possible concurrency issue also on BinaryConverterCache and EnumTools

parent acd8a6da
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
namespace Syroot.BinaryData
{
......@@ -10,7 +10,7 @@ namespace Syroot.BinaryData
{
// ---- FIELDS -------------------------------------------------------------------------------------------------
private static readonly Dictionary<Type, IBinaryConverter> _cache = new Dictionary<Type, IBinaryConverter>();
private static readonly ConcurrentDictionary<Type, IBinaryConverter> _cache = new ConcurrentDictionary<Type, IBinaryConverter>();
// ---- METHODS (INTERNAL) -------------------------------------------------------------------------------------
......@@ -24,7 +24,7 @@ namespace Syroot.BinaryData
if (!_cache.TryGetValue(type, out IBinaryConverter converter))
{
converter = (IBinaryConverter)Activator.CreateInstance(type);
_cache.Add(type, converter);
_cache.TryAdd(type, converter);
}
return converter;
}
......
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
namespace Syroot.BinaryData
......@@ -11,7 +11,7 @@ namespace Syroot.BinaryData
{
// ---- FIELDS -------------------------------------------------------------------------------------------------
private static Dictionary<Type, bool> _flagEnums = new Dictionary<Type, bool>();
private static ConcurrentDictionary<Type, bool> _flagEnums = new ConcurrentDictionary<Type, bool>();
// ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
......@@ -45,7 +45,7 @@ namespace Syroot.BinaryData
if (!_flagEnums.TryGetValue(type, out bool value))
{
value = type.GetCustomAttributes(typeof(FlagsAttribute), true)?.Any() == true;
_flagEnums.Add(type, value);
_flagEnums.TryAdd(type, value);
}
return value;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment