Add unit test for AggregateException.GetBaseException that works on .net but is broke...
[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 //       Rewritten by Paolo Molaro (lupus@ximian.com)
7 // 
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 // 
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // THE SOFTWARE.
27
28 #if NET_4_0
29 using System;
30 using System.Collections.Generic;
31 using System.Runtime.Serialization;
32 using System.Runtime.InteropServices;
33 using System.Security.Permissions;
34
35 namespace System.Threading
36 {
37         [HostProtection (SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
38         [System.Diagnostics.DebuggerDisplay ("IsValueCreated={IsValueCreated}, Value={ValueForDebugDisplay}")]
39         [System.Diagnostics.DebuggerTypeProxy ("System.Threading.SystemThreading_ThreadLocalDebugView`1")]
40         public class ThreadLocal<T> : IDisposable
41         {
42                 struct TlsDatum {
43                         internal sbyte state; /* 0 uninitialized, < 0 initializing, > 0 inited */
44                         internal Exception cachedException; /* this is per-thread */
45                         internal T data;
46                 }
47
48                 Func<T> valueFactory;
49                 /* The tlsdata field is handled magically by the JIT
50                  * It must be a struct and it is always accessed by ldflda: the JIT, instead of
51                  * computing the address inside the instance, will return the address of the variable
52                  * for the current thread (based on tls_offset). This magic wouldn't be needed if C#
53                  * let us declare an icall with a TlsDatum& return type...
54                  * For this same reason, we must check tls_offset for != 0 to make sure it's valid before accessing tlsdata
55                  * The address of the tls var is cached per method at the first IL ldflda instruction, so care must be taken
56                  * not to cause it to be conditionally executed.
57                  */
58                 uint tls_offset;
59                 TlsDatum tlsdata;
60                 
61                 public ThreadLocal ()
62                 {
63                         tls_offset = Thread.AllocTlsData (typeof (TlsDatum));
64                 }
65
66                 public ThreadLocal (Func<T> valueFactory) : this ()
67                 {
68                         if (valueFactory == null)
69                                 throw new ArgumentNullException ("valueFactory");
70                         this.valueFactory = valueFactory;
71                 }
72
73 #if NET_4_5
74                 public ThreadLocal (bool trackAllValues) : this () {
75                         if (trackAllValues)
76                                 throw new NotImplementedException ();
77                 }
78
79                 public ThreadLocal (Func<T> valueFactory, bool trackAllValues) : this (valueFactory) {
80                         if (trackAllValues)
81                                 throw new NotImplementedException ();
82                 }
83 #endif
84
85                 public void Dispose ()
86                 {
87                         Dispose (true);
88                 }
89                 
90                 protected virtual void Dispose (bool disposing)
91                 {
92                         if (tls_offset != 0) {
93                                 uint o = tls_offset;
94                                 tls_offset = 0;
95                                 if (disposing)
96                                         valueFactory = null;
97                                 Thread.DestroyTlsData (o);
98                                 GC.SuppressFinalize (this);
99                         }
100                 }
101
102                 ~ThreadLocal ()
103                 {
104                         Dispose (false);
105                 }
106                 
107                 public bool IsValueCreated {
108                         get {
109                                 if (tls_offset == 0)
110                                         throw new ObjectDisposedException ("ThreadLocal object");
111                                 /* ALERT! magic tlsdata JIT access redirects to TLS value instead of instance field */
112                                 return tlsdata.state > 0;
113                         }
114                 }
115
116                 T GetSlowPath () {
117                         /* ALERT! magic tlsdata JIT access redirects to TLS value instead of instance field */
118                         if (tlsdata.cachedException != null)
119                                 throw tlsdata.cachedException;
120                         if (tlsdata.state < 0)
121                                 throw new InvalidOperationException ("The initialization function attempted to reference Value recursively");
122                         tlsdata.state = -1;
123                         if (valueFactory != null) {
124                                 try {
125                                         tlsdata.data = valueFactory ();
126                                 } catch (Exception ex) {
127                                         tlsdata.cachedException = ex;
128                                         throw ex;
129                                 }
130                         } else {
131                                 tlsdata.data = default (T);
132                         }
133                         tlsdata.state = 1;
134                         return tlsdata.data;
135                 }
136
137                 [System.Diagnostics.DebuggerBrowsableAttribute (System.Diagnostics.DebuggerBrowsableState.Never)]
138                 public T Value {
139                         get {
140                                 if (tls_offset == 0)
141                                         throw new ObjectDisposedException ("ThreadLocal object");
142                                 /* ALERT! magic tlsdata JIT access redirects to TLS value instead of instance field */
143                                 if (tlsdata.state > 0)
144                                         return tlsdata.data;
145                                 return GetSlowPath ();
146                         }
147                         set {
148                                 if (tls_offset == 0)
149                                         throw new ObjectDisposedException ("ThreadLocal object");
150                                 /* ALERT! magic tlsdata JIT access redirects to TLS value instead of instance field */
151                                 tlsdata.state = 1;
152                                 tlsdata.data = value;
153                         }
154                 }
155
156 #if NET_4_5
157                 public IList<T> Values {
158                         get {
159                                 if (tls_offset == 0)
160                                         throw new ObjectDisposedException ("ThreadLocal object");
161                                 throw new NotImplementedException ();
162                         }
163                 }
164 #endif
165
166                 public override string ToString ()
167                 {
168                         return string.Format ("[ThreadLocal: IsValueCreated={0}, Value={1}]", IsValueCreated, Value);
169                 }
170                 
171         }
172 }
173 #endif