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 ();
84 return GetValueThreadLocal ();
89 DataSlotWrapper w = GetWrapper ();
91 w.Getter = () => value;
95 public override string ToString ()
97 return string.Format ("[ThreadLocal: IsValueCreated={0}, Value={1}]", IsValueCreated, Value);
100 T GetValueThreadLocal ()
102 DataSlotWrapper myWrapper = GetWrapper ();
103 if (myWrapper.Creating)
104 throw new InvalidOperationException ("The initialization function attempted to reference Value recursively");
106 return myWrapper.Getter ();
109 bool IsInitializedThreadLocal ()
111 DataSlotWrapper myWrapper = GetWrapper ();
113 return myWrapper.Init;
116 DataSlotWrapper GetWrapper ()
118 DataSlotWrapper myWrapper = (DataSlotWrapper)Thread.GetData (localStore);
119 if (myWrapper == null) {
120 myWrapper = DataSlotCreator ();
121 Thread.SetData (localStore, myWrapper);
127 void ThrowIfNeeded ()
129 if (cachedException != null)
130 throw cachedException;
133 DataSlotWrapper DataSlotCreator ()
135 DataSlotWrapper wrapper = new DataSlotWrapper ();
136 Func<T> valSelector = valueFactory;
138 wrapper.Getter = delegate {
139 wrapper.Creating = true;
141 T val = valSelector ();
142 wrapper.Creating = false;
144 wrapper.Getter = () => val;
146 } catch (Exception e) {