diff options
author | Scott Ferguson <scott.ferguson@unity3d.com> | 2020-01-24 17:57:48 +0300 |
---|---|---|
committer | Scott Ferguson <scott.ferguson@unity3d.com> | 2020-01-24 17:57:48 +0300 |
commit | df63b279ada3d41e1e95e77c3bc2b4139d178f4b (patch) | |
tree | 0062814dd41471a08587cdb08ba3102244d39678 /Mono.Cecil/GenericInstanceType.cs | |
parent | 93aeae7f8f67e0d9899c41d252817bf12ec54086 (diff) |
Improves thread safety of lazy initializations
The Interlocked.Exchange calls will ensure that the same object is
returned from each call even under a race. That's not necessarily
required, but it should also make sure that no write re-ordering issues
occur on platforms that allow it. And it also makes the possibility
of thread safety issues explicit.
Diffstat (limited to 'Mono.Cecil/GenericInstanceType.cs')
-rw-r--r-- | Mono.Cecil/GenericInstanceType.cs | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/Mono.Cecil/GenericInstanceType.cs b/Mono.Cecil/GenericInstanceType.cs index 8885850..745acd8 100644 --- a/Mono.Cecil/GenericInstanceType.cs +++ b/Mono.Cecil/GenericInstanceType.cs @@ -10,7 +10,7 @@ using System; using System.Text; - +using System.Threading; using Mono.Collections.Generic; using MD = Mono.Cecil.Metadata; @@ -26,7 +26,12 @@ namespace Mono.Cecil { } public Collection<TypeReference> GenericArguments { - get { return arguments ?? (arguments = new Collection<TypeReference> ()); } + get { + if (arguments == null) + Interlocked.CompareExchange (ref arguments, new Collection<TypeReference> (), null); + + return arguments; + } } public override TypeReference DeclaringType { |