1
0

Lazy.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // Lazy.cs
  3. //
  4. // Authors:
  5. // Zoltan Varga ([email protected])
  6. // Marek Safar ([email protected])
  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. using System;
  30. using System.Runtime.Serialization;
  31. using System.Runtime.InteropServices;
  32. using System.Security.Permissions;
  33. using System.Threading;
  34. using System.Diagnostics;
  35. namespace OpenSim.Framework
  36. {
  37. public enum LazyThreadSafetyMode
  38. {
  39. None,
  40. PublicationOnly,
  41. ExecutionAndPublication
  42. }
  43. [SerializableAttribute]
  44. [ComVisibleAttribute(false)]
  45. public class Lazy<T>
  46. {
  47. T value;
  48. bool inited;
  49. LazyThreadSafetyMode mode;
  50. Func<T> factory;
  51. object monitor;
  52. Exception exception;
  53. public Lazy()
  54. : this(LazyThreadSafetyMode.ExecutionAndPublication)
  55. {
  56. }
  57. public Lazy(Func<T> valueFactory)
  58. : this(valueFactory, LazyThreadSafetyMode.ExecutionAndPublication)
  59. {
  60. }
  61. public Lazy(bool isThreadSafe)
  62. : this(() => Activator.CreateInstance<T>(), isThreadSafe ? LazyThreadSafetyMode.ExecutionAndPublication : LazyThreadSafetyMode.None)
  63. {
  64. }
  65. public Lazy(Func<T> valueFactory, bool isThreadSafe)
  66. : this(valueFactory, isThreadSafe ? LazyThreadSafetyMode.ExecutionAndPublication : LazyThreadSafetyMode.None)
  67. {
  68. }
  69. public Lazy(LazyThreadSafetyMode mode)
  70. : this(() => Activator.CreateInstance<T>(), mode)
  71. {
  72. }
  73. public Lazy(Func<T> valueFactory, LazyThreadSafetyMode mode)
  74. {
  75. if (valueFactory == null)
  76. throw new ArgumentNullException("valueFactory");
  77. this.factory = valueFactory;
  78. if (mode != LazyThreadSafetyMode.None)
  79. monitor = new object();
  80. this.mode = mode;
  81. }
  82. // Don't trigger expensive initialization
  83. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  84. public T Value
  85. {
  86. get
  87. {
  88. if (inited)
  89. return value;
  90. if (exception != null)
  91. throw exception;
  92. return InitValue();
  93. }
  94. }
  95. T InitValue()
  96. {
  97. switch (mode)
  98. {
  99. case LazyThreadSafetyMode.None:
  100. {
  101. var init_factory = factory;
  102. if (init_factory == null)
  103. throw exception = new InvalidOperationException("The initialization function tries to access Value on this instance");
  104. try
  105. {
  106. factory = null;
  107. T v = init_factory();
  108. value = v;
  109. Thread.MemoryBarrier();
  110. inited = true;
  111. }
  112. catch (Exception ex)
  113. {
  114. exception = ex;
  115. throw;
  116. }
  117. break;
  118. }
  119. case LazyThreadSafetyMode.PublicationOnly:
  120. {
  121. var init_factory = factory;
  122. T v;
  123. //exceptions are ignored
  124. if (init_factory != null)
  125. v = init_factory();
  126. else
  127. v = default(T);
  128. lock (monitor)
  129. {
  130. if (inited)
  131. return value;
  132. value = v;
  133. Thread.MemoryBarrier();
  134. inited = true;
  135. factory = null;
  136. }
  137. break;
  138. }
  139. case LazyThreadSafetyMode.ExecutionAndPublication:
  140. {
  141. lock (monitor)
  142. {
  143. if (inited)
  144. return value;
  145. if (factory == null)
  146. throw exception = new InvalidOperationException("The initialization function tries to access Value on this instance");
  147. var init_factory = factory;
  148. try
  149. {
  150. factory = null;
  151. T v = init_factory();
  152. value = v;
  153. Thread.MemoryBarrier();
  154. inited = true;
  155. }
  156. catch (Exception ex)
  157. {
  158. exception = ex;
  159. throw;
  160. }
  161. }
  162. break;
  163. }
  164. default:
  165. throw new InvalidOperationException("Invalid LazyThreadSafetyMode " + mode);
  166. }
  167. if (monitor == null)
  168. {
  169. value = factory();
  170. inited = true;
  171. }
  172. else
  173. {
  174. lock (monitor)
  175. {
  176. if (inited)
  177. return value;
  178. if (factory == null)
  179. throw new InvalidOperationException("The initialization function tries to access Value on this instance");
  180. var init_factory = factory;
  181. try
  182. {
  183. factory = null;
  184. T v = init_factory();
  185. value = v;
  186. Thread.MemoryBarrier();
  187. inited = true;
  188. }
  189. catch
  190. {
  191. factory = init_factory;
  192. throw;
  193. }
  194. }
  195. }
  196. return value;
  197. }
  198. public bool IsValueCreated
  199. {
  200. get
  201. {
  202. return inited;
  203. }
  204. }
  205. public override string ToString()
  206. {
  207. if (inited)
  208. return value.ToString();
  209. else
  210. return "Value is not created";
  211. }
  212. }
  213. }