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