Commit e04f3507 authored by Ray Koopa's avatar Ray Koopa

Merge branch 'hotfix/concurrency-issue' into 'master'

Fix concurrency issue on TypeData

Closes #42

See merge request !35
parents 6c6f2fb8 a90c6ddb
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.Concurrent;
using System.Collections.Generic;
using System.Reflection;
......@@ -11,7 +12,7 @@ namespace Syroot.BinaryData
{
// ---- FIELDS -------------------------------------------------------------------------------------------------
private static readonly Dictionary<Type, TypeData> _cache = new Dictionary<Type, TypeData>();
private static readonly ConcurrentDictionary<Type, TypeData> _cache = new ConcurrentDictionary<Type, TypeData>();
// ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------
......@@ -86,7 +87,7 @@ namespace Syroot.BinaryData
if (!_cache.TryGetValue(type, out TypeData typeData))
{
typeData = new TypeData(type);
_cache.Add(type, typeData);
_cache.TryAdd(type, typeData);
}
return typeData;
}
......
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