Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/cecil.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Ferguson <scott.ferguson@unity3d.com>2020-01-24 17:57:48 +0300
committerScott Ferguson <scott.ferguson@unity3d.com>2020-01-24 17:57:48 +0300
commitdf63b279ada3d41e1e95e77c3bc2b4139d178f4b (patch)
tree0062814dd41471a08587cdb08ba3102244d39678 /Mono.Collections.Generic
parent93aeae7f8f67e0d9899c41d252817bf12ec54086 (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.Collections.Generic')
-rw-r--r--Mono.Collections.Generic/ReadOnlyCollection.cs10
1 files changed, 9 insertions, 1 deletions
diff --git a/Mono.Collections.Generic/ReadOnlyCollection.cs b/Mono.Collections.Generic/ReadOnlyCollection.cs
index fe78414..73ef974 100644
--- a/Mono.Collections.Generic/ReadOnlyCollection.cs
+++ b/Mono.Collections.Generic/ReadOnlyCollection.cs
@@ -11,6 +11,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
+using System.Threading;
namespace Mono.Collections.Generic {
@@ -19,7 +20,14 @@ namespace Mono.Collections.Generic {
static ReadOnlyCollection<T> empty;
public static ReadOnlyCollection<T> Empty {
- get { return empty ?? (empty = new ReadOnlyCollection<T> ()); }
+ get
+ {
+ if (empty != null)
+ return empty;
+
+ Interlocked.CompareExchange (ref empty, new ReadOnlyCollection<T> (), null);
+ return empty;
+ }
}
bool ICollection<T>.IsReadOnly {