Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / corlib / System.Threading / ThreadLocal.cs
1 // 
2 // ThreadLocal.cs
3 //  
4 // Author:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 // 
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
8 // 
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:
15 // 
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 // 
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
25 // THE SOFTWARE.
26
27 #if NET_4_0
28 using System;
29 using System.Runtime.Serialization;
30 using System.Runtime.InteropServices;
31 using System.Security.Permissions;
32
33 namespace System.Threading
34 {
35         [HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, 
36                                  ExternalThreading = true)]
37         public class ThreadLocal<T> : IDisposable
38         {
39                 readonly Func<T> initializer;
40                 LocalDataStoreSlot localStore;
41                 Exception cachedException;
42                 
43                 class DataSlotWrapper
44                 {
45                         public bool Creating;
46                         public bool Init;
47                         public Func<T> Getter;
48                 }
49                 
50                 public ThreadLocal () : this (LazyInitializer.GetDefaultCtorValue<T>)
51                 {
52                 }
53
54                 public ThreadLocal (Func<T> initializer)
55                 {
56                         if (initializer == null)
57                                 throw new ArgumentNullException ("initializer");
58                         
59                         localStore = Thread.AllocateDataSlot ();
60                         this.initializer = initializer;
61                 }
62                 
63                 public void Dispose ()
64                 {
65                         Dispose (true);
66                 }
67                 
68                 protected virtual void Dispose (bool dispManagedRes)
69                 {
70                         
71                 }
72                 
73                 public bool IsValueCreated {
74                         get {
75                                 ThrowIfNeeded ();
76                                 return IsInitializedThreadLocal ();
77                         }
78                 }
79                 
80                 public T Value {
81                         get {
82                                 ThrowIfNeeded ();
83                                 return GetValueThreadLocal ();
84                         }
85                         set {
86                                 ThrowIfNeeded ();
87
88                                 DataSlotWrapper w = GetWrapper ();
89                                 w.Init = true;
90                                 w.Getter = () => value;
91                         }
92                 }
93                 
94                 public override string ToString ()
95                 {
96                         return string.Format ("[ThreadLocal: IsValueCreated={0}, Value={1}]", IsValueCreated, Value);
97                 }
98
99                 
100                 T GetValueThreadLocal ()
101                 {
102                         DataSlotWrapper myWrapper = GetWrapper ();
103                         if (myWrapper.Creating)
104                                 throw new InvalidOperationException ("The initialization function attempted to reference Value recursively");
105
106                         return myWrapper.Getter ();
107                 }
108                 
109                 bool IsInitializedThreadLocal ()
110                 {
111                         DataSlotWrapper myWrapper = GetWrapper ();
112
113                         return myWrapper.Init;
114                 }
115
116                 DataSlotWrapper GetWrapper ()
117                 {
118                         DataSlotWrapper myWrapper = (DataSlotWrapper)Thread.GetData (localStore);
119                         if (myWrapper == null) {
120                                 myWrapper = DataSlotCreator ();
121                                 Thread.SetData (localStore, myWrapper);
122                         }
123
124                         return myWrapper;
125                 }
126
127                 void ThrowIfNeeded ()
128                 {
129                         if (cachedException != null)
130                                 throw cachedException;
131                 }
132
133                 DataSlotWrapper DataSlotCreator ()
134                 {
135                         DataSlotWrapper wrapper = new DataSlotWrapper ();
136                         Func<T> valSelector = initializer;
137         
138                         wrapper.Getter = delegate {
139                                 wrapper.Creating = true;
140                                 try {
141                                         T val = valSelector ();
142                                         wrapper.Creating = false;
143                                         wrapper.Init = true;
144                                         wrapper.Getter = () => val;
145                                         return val;
146                                 } catch (Exception e) {
147                                         cachedException = e;
148                                         throw e;
149                                 }
150                         };
151                         
152                         return wrapper;
153                 }
154         }
155 }
156 #endif