OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
Lazy.cs
Go to the documentation of this file.
1 //
2 // Lazy.cs
3 //
4 // Authors:
5 // Zoltan Varga (vargaz@gmail.com)
6 // Marek Safar (marek.safar@gmail.com)
7 //
8 // Copyright (C) 2009 Novell
9 //
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:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
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.
28 //
29 
30 using System;
31 using System.Runtime.Serialization;
32 using System.Runtime.InteropServices;
33 using System.Security.Permissions;
34 using System.Threading;
35 using System.Diagnostics;
36 
37 namespace OpenSim.Framework
38 {
40  {
41  None,
44  }
45 
46  [SerializableAttribute]
47  [ComVisibleAttribute(false)]
48  [HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
49  public class Lazy<T>
50  {
51  T value;
52  bool inited;
54  Func<T> factory;
55  object monitor;
56  Exception exception;
57 
58  public Lazy()
60  {
61  }
62 
63  public Lazy(Func<T> valueFactory)
64  : this(valueFactory, LazyThreadSafetyMode.ExecutionAndPublication)
65  {
66  }
67 
68  public Lazy(bool isThreadSafe)
69  : this(() => Activator.CreateInstance<T>(), isThreadSafe ? LazyThreadSafetyMode.ExecutionAndPublication : LazyThreadSafetyMode.None)
70  {
71  }
72 
73  public Lazy(Func<T> valueFactory, bool isThreadSafe)
74  : this(valueFactory, isThreadSafe ? LazyThreadSafetyMode.ExecutionAndPublication : LazyThreadSafetyMode.None)
75  {
76  }
77 
79  : this(() => Activator.CreateInstance<T>(), mode)
80  {
81  }
82 
83  public Lazy(Func<T> valueFactory, LazyThreadSafetyMode mode)
84  {
85  if (valueFactory == null)
86  throw new ArgumentNullException("valueFactory");
87  this.factory = valueFactory;
88  if (mode != LazyThreadSafetyMode.None)
89  monitor = new object();
90  this.mode = mode;
91  }
92 
93  // Don't trigger expensive initialization
94  [DebuggerBrowsable(DebuggerBrowsableState.Never)]
95  public T Value
96  {
97  get
98  {
99  if (inited)
100  return value;
101  if (exception != null)
102  throw exception;
103 
104  return InitValue();
105  }
106  }
107 
108  T InitValue()
109  {
110  switch (mode)
111  {
112  case LazyThreadSafetyMode.None:
113  {
114  var init_factory = factory;
115  if (init_factory == null)
116  throw exception = new InvalidOperationException("The initialization function tries to access Value on this instance");
117  try
118  {
119  factory = null;
120  T v = init_factory();
121  value = v;
122  Thread.MemoryBarrier();
123  inited = true;
124  }
125  catch (Exception ex)
126  {
127  exception = ex;
128  throw;
129  }
130  break;
131  }
132  case LazyThreadSafetyMode.PublicationOnly:
133  {
134  var init_factory = factory;
135  T v;
136 
137  //exceptions are ignored
138  if (init_factory != null)
139  v = init_factory();
140  else
141  v = default(T);
142 
143  lock (monitor)
144  {
145  if (inited)
146  return value;
147  value = v;
148  Thread.MemoryBarrier();
149  inited = true;
150  factory = null;
151  }
152  break;
153  }
154  case LazyThreadSafetyMode.ExecutionAndPublication:
155  {
156  lock (monitor)
157  {
158  if (inited)
159  return value;
160 
161  if (factory == null)
162  throw exception = new InvalidOperationException("The initialization function tries to access Value on this instance");
163 
164  var init_factory = factory;
165  try
166  {
167  factory = null;
168  T v = init_factory();
169  value = v;
170  Thread.MemoryBarrier();
171  inited = true;
172  }
173  catch (Exception ex)
174  {
175  exception = ex;
176  throw;
177  }
178  }
179  break;
180  }
181  default:
182  throw new InvalidOperationException("Invalid LazyThreadSafetyMode " + mode);
183  }
184 
185  if (monitor == null)
186  {
187  value = factory();
188  inited = true;
189  }
190  else
191  {
192  lock (monitor)
193  {
194  if (inited)
195  return value;
196 
197  if (factory == null)
198  throw new InvalidOperationException("The initialization function tries to access Value on this instance");
199 
200  var init_factory = factory;
201  try
202  {
203  factory = null;
204  T v = init_factory();
205  value = v;
206  Thread.MemoryBarrier();
207  inited = true;
208  }
209  catch
210  {
211  factory = init_factory;
212  throw;
213  }
214  }
215  }
216 
217  return value;
218  }
219 
220  public bool IsValueCreated
221  {
222  get
223  {
224  return inited;
225  }
226  }
227 
228  public override string ToString()
229  {
230  if (inited)
231  return value.ToString();
232  else
233  return "Value is not created";
234  }
235  }
236 }
Lazy(Func< T > valueFactory)
Definition: Lazy.cs:63
LazyThreadSafetyMode
Definition: Lazy.cs:39
Lazy(LazyThreadSafetyMode mode)
Definition: Lazy.cs:78
Lazy(bool isThreadSafe)
Definition: Lazy.cs:68
Lazy(Func< T > valueFactory, LazyThreadSafetyMode mode)
Definition: Lazy.cs:83
override string ToString()
Definition: Lazy.cs:228
Lazy(Func< T > valueFactory, bool isThreadSafe)
Definition: Lazy.cs:73