5 // Zoltan Varga (vargaz@gmail.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // Copyright (C) 2009 Novell
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Runtime.Serialization;
34 using System.Runtime.InteropServices;
35 using System.Security.Permissions;
36 using System.Threading;
37 using System.Diagnostics;
41 [SerializableAttribute]
42 [ComVisibleAttribute(false)]
43 [HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
44 [DebuggerDisplay ("ThreadSafetyMode={Mode}, IsValueCreated={IsValueCreated}, IsValueFaulted={IsValueFaulted}, Value={ValueForDebugDisplay}")]
51 LazyThreadSafetyMode mode;
55 : this (LazyThreadSafetyMode.ExecutionAndPublication)
59 public Lazy (Func<T> valueFactory)
60 : this (valueFactory, LazyThreadSafetyMode.ExecutionAndPublication)
64 public Lazy (bool isThreadSafe)
65 : this (Activator.CreateInstance<T>, isThreadSafe ? LazyThreadSafetyMode.ExecutionAndPublication : LazyThreadSafetyMode.None)
69 public Lazy (Func<T> valueFactory, bool isThreadSafe)
70 : this (valueFactory, isThreadSafe ? LazyThreadSafetyMode.ExecutionAndPublication : LazyThreadSafetyMode.None)
74 public Lazy (LazyThreadSafetyMode mode)
75 : this (Activator.CreateInstance<T>, mode)
81 public Lazy (Func<T> valueFactory, LazyThreadSafetyMode mode)
83 if (valueFactory == null)
84 throw new ArgumentNullException ("valueFactory");
85 this.factory = valueFactory;
86 if (mode != LazyThreadSafetyMode.None)
87 monitor = new object ();
91 // Don't trigger expensive initialization
92 [DebuggerBrowsable (DebuggerBrowsableState.Never)]
97 if (exception != null)
106 Func<T> init_factory;
110 case LazyThreadSafetyMode.None:
111 init_factory = factory;
112 if (init_factory == null)
113 throw exception = new InvalidOperationException ("The initialization function tries to access Value on this instance");
118 Thread.MemoryBarrier ();
120 } catch (Exception ex) {
126 case LazyThreadSafetyMode.PublicationOnly:
127 init_factory = factory;
129 //exceptions are ignored
130 if (init_factory != null)
139 Thread.MemoryBarrier ();
145 case LazyThreadSafetyMode.ExecutionAndPublication:
151 throw exception = new InvalidOperationException ("The initialization function tries to access Value on this instance");
153 init_factory = factory;
158 Thread.MemoryBarrier ();
160 } catch (Exception ex) {
168 throw new InvalidOperationException ("Invalid LazyThreadSafetyMode " + mode);
174 public bool IsValueCreated {
180 public override string ToString ()
183 return value.ToString ();
185 return "Value is not created";