mscorlib(4.0.0.0) API with additions
LazyInitializer.cs
2 
3 namespace System.Threading
4 {
6  [__DynamicallyInvokable]
7  [HostProtection(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
8  public static class LazyInitializer
9  {
16  [__DynamicallyInvokable]
17  public static T EnsureInitialized<T>(ref T target) where T : class
18  {
19  if (Volatile.Read(ref target) != null)
20  {
21  return target;
22  }
23  return EnsureInitializedCore(ref target, LazyHelpers<T>.s_activatorFactorySelector);
24  }
25 
34  [__DynamicallyInvokable]
35  public static T EnsureInitialized<T>(ref T target, Func<T> valueFactory) where T : class
36  {
37  if (Volatile.Read(ref target) != null)
38  {
39  return target;
40  }
41  return EnsureInitializedCore(ref target, valueFactory);
42  }
43 
44  private static T EnsureInitializedCore<T>(ref T target, Func<T> valueFactory) where T : class
45  {
46  T val = valueFactory();
47  if (val == null)
48  {
49  throw new InvalidOperationException(Environment.GetResourceString("Lazy_StaticInit_InvalidOperation"));
50  }
51  Interlocked.CompareExchange(ref target, val, null);
52  return target;
53  }
54 
63  [__DynamicallyInvokable]
64  public static T EnsureInitialized<T>(ref T target, ref bool initialized, ref object syncLock)
65  {
66  if (Volatile.Read(ref initialized))
67  {
68  return target;
69  }
70  return EnsureInitializedCore(ref target, ref initialized, ref syncLock, LazyHelpers<T>.s_activatorFactorySelector);
71  }
72 
82  [__DynamicallyInvokable]
83  public static T EnsureInitialized<T>(ref T target, ref bool initialized, ref object syncLock, Func<T> valueFactory)
84  {
85  if (Volatile.Read(ref initialized))
86  {
87  return target;
88  }
89  return EnsureInitializedCore(ref target, ref initialized, ref syncLock, valueFactory);
90  }
91 
92  private static T EnsureInitializedCore<T>(ref T target, ref bool initialized, ref object syncLock, Func<T> valueFactory)
93  {
94  object obj = syncLock;
95  if (obj == null)
96  {
97  object obj2 = new object();
98  obj = Interlocked.CompareExchange(ref syncLock, obj2, null);
99  if (obj == null)
100  {
101  obj = obj2;
102  }
103  }
104  lock (obj)
105  {
106  if (!Volatile.Read(ref initialized))
107  {
108  target = valueFactory();
109  Volatile.Write(ref initialized, value: true);
110  }
111  }
112  return target;
113  }
114  }
115 }
static T EnsureInitialized< T >(ref T target)
Initializes a target reference type with the type's default constructor if it hasn't already been ini...
Definition: __Canon.cs:3
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Provides lazy initialization routines.
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
Contains methods for performing volatile memory operations.
Definition: Volatile.cs:8
static bool Read(ref bool location)
Reads the value of the specified field. On systems that require it, inserts a memory barrier that pre...
Definition: Volatile.cs:15
The exception that is thrown when a method call is invalid for the object's current state.
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10