Skip to content

Commit 7a9059f

Browse files
committed
Add some locks around static dictionaries.
It's possible this introduced some multithreading issues. Resolves #113.
1 parent da5d641 commit 7a9059f

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

Assets/FullSerializer/Source/Reflection/fsMetaType.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ public class fsMetaType {
1515

1616
public static fsMetaType Get(fsConfig config, Type type) {
1717
Dictionary<Type, fsMetaType> metaTypes;
18-
if (_configMetaTypes.TryGetValue(config, out metaTypes) == false)
19-
metaTypes = _configMetaTypes[config] = new Dictionary<Type, fsMetaType>();
18+
lock (typeof(fsMetaType)) {
19+
if (_configMetaTypes.TryGetValue(config, out metaTypes) == false)
20+
metaTypes = _configMetaTypes[config] = new Dictionary<Type, fsMetaType>();
21+
}
2022

2123
fsMetaType metaType;
2224
if (metaTypes.TryGetValue(type, out metaType) == false) {
@@ -32,7 +34,9 @@ public static fsMetaType Get(fsConfig config, Type type) {
3234
/// serialization mode.
3335
/// </summary>
3436
public static void ClearCache() {
35-
_configMetaTypes = new Dictionary<fsConfig, Dictionary<Type, fsMetaType>>();
37+
lock (typeof(fsMetaType)) {
38+
_configMetaTypes = new Dictionary<fsConfig, Dictionary<Type, fsMetaType>>();
39+
}
3640
}
3741

3842
private fsMetaType(fsConfig config, Type reflectedType) {

Assets/FullSerializer/Source/Reflection/fsTypeCache.cs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,37 @@ public static class fsTypeCache {
2424
private static List<Assembly> _assembliesByIndex;
2525

2626
static fsTypeCache() {
27-
// Setup assembly references so searching and the like resolves correctly.
28-
_assembliesByName = new Dictionary<string, Assembly>();
29-
_assembliesByIndex = new List<Assembly>();
27+
lock (typeof(fsTypeCache)) {
28+
// Setup assembly references so searching and the like resolves correctly.
29+
_assembliesByName = new Dictionary<string, Assembly>();
30+
_assembliesByIndex = new List<Assembly>();
3031

3132
#if (!UNITY_EDITOR && UNITY_METRO && !ENABLE_IL2CPP) // no AppDomain on WinRT
32-
var assembly = typeof(object).GetTypeInfo().Assembly;
33-
_assembliesByName[assembly.FullName] = assembly;
34-
_assembliesByIndex.Add(assembly);
35-
#else
36-
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) {
33+
var assembly = typeof(object).GetTypeInfo().Assembly;
3734
_assembliesByName[assembly.FullName] = assembly;
3835
_assembliesByIndex.Add(assembly);
39-
}
36+
#else
37+
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) {
38+
_assembliesByName[assembly.FullName] = assembly;
39+
_assembliesByIndex.Add(assembly);
40+
}
4041
#endif
41-
42-
_cachedTypes = new Dictionary<string, Type>();
42+
_cachedTypes = new Dictionary<string, Type>();
4343

4444
#if !(UNITY_WP8 || UNITY_METRO) // AssemblyLoad events are not supported on these platforms
45-
AppDomain.CurrentDomain.AssemblyLoad += OnAssemblyLoaded;
45+
AppDomain.CurrentDomain.AssemblyLoad += OnAssemblyLoaded;
4646
#endif
47+
}
4748
}
4849

4950
#if !(UNITY_WP8 || UNITY_METRO) // AssemblyLoad events are not supported on these platforms
5051
private static void OnAssemblyLoaded(object sender, AssemblyLoadEventArgs args) {
51-
_assembliesByName[args.LoadedAssembly.FullName] = args.LoadedAssembly;
52-
_assembliesByIndex.Add(args.LoadedAssembly);
52+
lock (typeof(fsTypeCache)) {
53+
_assembliesByName[args.LoadedAssembly.FullName] = args.LoadedAssembly;
54+
_assembliesByIndex.Add(args.LoadedAssembly);
5355

54-
_cachedTypes = new Dictionary<string, Type>();
56+
_cachedTypes = new Dictionary<string, Type>();
57+
}
5558
}
5659
#endif
5760

@@ -150,17 +153,19 @@ public static Type GetType(string name, string assemblyHint) {
150153
return null;
151154
}
152155

153-
Type type;
154-
if (_cachedTypes.TryGetValue(name, out type) == false) {
155-
// if both the direct and indirect type lookups fail, then throw an exception
156-
if (TryDirectTypeLookup(assemblyHint, name, out type) == false &&
157-
TryIndirectTypeLookup(name, out type) == false) {
156+
lock (typeof(fsTypeCache)) {
157+
Type type;
158+
if (_cachedTypes.TryGetValue(name, out type) == false) {
159+
// if both the direct and indirect type lookups fail, then throw an exception
160+
if (TryDirectTypeLookup(assemblyHint, name, out type) == false &&
161+
TryIndirectTypeLookup(name, out type) == false) {
162+
}
163+
164+
_cachedTypes[name] = type;
158165
}
159166

160-
_cachedTypes[name] = type;
167+
return type;
161168
}
162-
163-
return type;
164169
}
165170
}
166171
}

0 commit comments

Comments
 (0)