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 [HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true,
36 ExternalThreading = true)]
37 public class ThreadLocal<T> : IDisposable
39 readonly Func<T> initializer;
40 LocalDataStoreSlot localStore;
41 Exception cachedException;
47 public Func<T> Getter;
50 public ThreadLocal () : this (LazyInitializer.GetDefaultCtorValue<T>)
54 public ThreadLocal (Func<T> initializer)
56 if (initializer == null)
57 throw new ArgumentNullException ("initializer");
59 localStore = Thread.AllocateDataSlot ();
60 this.initializer = initializer;
63 public void Dispose ()
68 protected virtual void Dispose (bool dispManagedRes)
73 public bool IsValueCreated {
76 return IsInitializedThreadLocal ();
83 return GetValueThreadLocal ();
88 DataSlotWrapper w = GetWrapper ();
90 w.Getter = () => value;
94 public override string ToString ()
96 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 = initializer;
138 wrapper.Getter = delegate {
139 wrapper.Creating = true;
141 T val = valSelector ();
142 wrapper.Creating = false;
144 wrapper.Getter = () => val;
146 } catch (Exception e) {