5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 using System.Runtime.Serialization;
30 using System.Runtime.InteropServices;
31 using System.Security.Permissions;
33 namespace System.Threading
35 [HostProtection (SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
36 [System.Diagnostics.DebuggerDisplay ("IsValueCreated={IsValueCreated}, Value={ValueForDebugDisplay}")]
37 [System.Diagnostics.DebuggerTypeProxy ("System.Threading.SystemThreading_ThreadLocalDebugView`1")]
38 public class ThreadLocal<T> : IDisposable
40 readonly Func<T> valueFactory;
41 LocalDataStoreSlot localStore;
42 Exception cachedException;
48 public Func<T> Getter;
51 public ThreadLocal () : this (LazyInitializer.GetDefaultValueFactory<T>)
55 public ThreadLocal (Func<T> valueFactory)
57 if (valueFactory == null)
58 throw new ArgumentNullException ("valueFactory");
60 localStore = Thread.AllocateDataSlot ();
61 this.valueFactory = valueFactory;
64 public void Dispose ()
69 protected virtual void Dispose (bool disposing)
74 public bool IsValueCreated {
77 return IsInitializedThreadLocal ();
81 [System.Diagnostics.DebuggerBrowsableAttribute (System.Diagnostics.DebuggerBrowsableState.Never)]
85 return GetValueThreadLocal ();
90 DataSlotWrapper w = GetWrapper ();
92 w.Getter = () => value;
96 public override string ToString ()
98 return string.Format ("[ThreadLocal: IsValueCreated={0}, Value={1}]", IsValueCreated, Value);
101 T GetValueThreadLocal ()
103 DataSlotWrapper myWrapper = GetWrapper ();
104 if (myWrapper.Creating)
105 throw new InvalidOperationException ("The initialization function attempted to reference Value recursively");
107 return myWrapper.Getter ();
110 bool IsInitializedThreadLocal ()
112 DataSlotWrapper myWrapper = GetWrapper ();
114 return myWrapper.Init;
117 DataSlotWrapper GetWrapper ()
119 DataSlotWrapper myWrapper = (DataSlotWrapper)Thread.GetData (localStore);
120 if (myWrapper == null) {
121 myWrapper = DataSlotCreator ();
122 Thread.SetData (localStore, myWrapper);
128 void ThrowIfNeeded ()
130 if (cachedException != null)
131 throw cachedException;
134 DataSlotWrapper DataSlotCreator ()
136 DataSlotWrapper wrapper = new DataSlotWrapper ();
137 Func<T> valSelector = valueFactory;
139 wrapper.Getter = delegate {
140 wrapper.Creating = true;
142 T val = valSelector ();
143 wrapper.Creating = false;
145 wrapper.Getter = () => val;
147 } catch (Exception e) {