Lazy.cs 7.3 KB

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