Merge pull request #1659 from alexanderkyte/stringbuilder-referencesource
[mono.git] / mcs / class / corlib / System.Threading / ExecutionContext.cs
1 // 
2 // System.Threading.ExecutionContext.cs
3 //
4 // Authors:
5 //      Lluis Sanchez (lluis@novell.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //  Marek Safar (marek.safar@gmail.com)
8 //
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 // Copyright (C) 2014 Xamarin Inc (http://www.xamarin.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Runtime.InteropServices;
33 using System.Runtime.Serialization;
34 using System.Security;
35 using System.Security.Permissions;
36 using System.Runtime.Remoting.Messaging;
37 using System.Collections;
38 using System.Collections.Generic;
39
40 namespace System.Threading {
41         [Serializable]
42         public sealed partial class ExecutionContext : ISerializable
43            , IDisposable
44         {
45                 internal struct Switcher
46                 {
47                         readonly ExecutionContext ec;
48                         readonly LogicalCallContext _lcc;
49                         readonly bool _suppressFlow;
50                         readonly bool _capture;
51                         readonly Dictionary<string, object> local_data;
52                         readonly bool copy_on_write;
53
54                         public Switcher (ExecutionContext ec)
55                         {
56                                 this.ec = ec;
57                                 this._lcc = ec._lcc;
58                                 this._suppressFlow = ec._suppressFlow;
59                                 this._capture = ec._capture;
60                                 this.local_data = ec.local_data;
61                                 this.copy_on_write = ec.CopyOnWrite;
62                         }
63
64                         public bool IsEmpty {
65                                 get {
66                                         return ec == null;
67                                 }
68                         }
69
70                         public void Restore (ExecutionContext ec)
71                         {
72                                 ec._lcc = this._lcc;
73                                 ec._suppressFlow = this._suppressFlow;
74                                 ec._capture = this._capture;
75                                 ec.local_data = this.local_data;
76                                 ec.CopyOnWrite = this.copy_on_write;
77                         }
78                 }
79
80 #if !MOBILE
81                 private SecurityContext _sc;
82 #endif
83                 private LogicalCallContext _lcc;
84                 private bool _suppressFlow;
85                 private bool _capture;
86                 Dictionary<string, object> local_data;
87
88                 internal ExecutionContext ()
89                 {
90                 }
91
92                 private ExecutionContext (ExecutionContext ec)
93                 {
94                         CloneData (ec);
95
96                         _suppressFlow = ec._suppressFlow;
97                         _capture = true;
98                 }
99
100                 void CloneData (ExecutionContext ec)
101                 {
102 #if !MOBILE
103                         if (ec._sc != null)
104                                 _sc = new SecurityContext (ec._sc);
105 #endif
106                         if (ec._lcc != null)
107                                 _lcc = (LogicalCallContext) ec._lcc.Clone ();
108                 }
109                 
110                 [MonoTODO]
111                 internal ExecutionContext (SerializationInfo info, StreamingContext context)
112                 {
113                         throw new NotImplementedException ();
114                 }
115
116                 public static ExecutionContext Capture ()
117                 {
118                         return Capture (true, false);
119                 }
120                 
121                 internal static ExecutionContext Capture (bool captureSyncContext, bool nullOnEmpty)
122                 {
123                         var thread = Thread.CurrentThread;
124                         if (nullOnEmpty && !thread.HasExecutionContext)
125                                 return null;
126
127                         var ec = thread.ExecutionContext;
128                         if (ec.FlowSuppressed)
129                                 return null;
130
131                         if (nullOnEmpty
132 #if !MOBILE
133                          && ec._sc == null
134 #endif
135                                 && (ec._lcc == null || !ec._lcc.HasInfo))
136                                 return null;
137
138                         ExecutionContext capture = new ExecutionContext (ec);
139 #if !MOBILE
140                         if (SecurityManager.SecurityEnabled)
141                                 capture.SecurityContext = SecurityContext.Capture ();
142 #endif
143                         return capture;
144                 }
145                 
146                 public ExecutionContext CreateCopy ()
147                 {
148                         if (!_capture)
149                                 throw new InvalidOperationException ();
150
151                         return new ExecutionContext (this);
152                 }
153                 
154                 public void Dispose ()
155                 {
156 #if !MOBILE
157                         if (_sc != null)
158                                 _sc.Dispose ();
159 #endif
160                 }
161
162                 [MonoTODO]
163                 [ReflectionPermission (SecurityAction.Demand, MemberAccess = true)]
164                 public void GetObjectData (SerializationInfo info, StreamingContext context)
165                 {
166                         if (info == null)
167                                 throw new ArgumentNullException ("info");
168                         throw new NotImplementedException ();
169                 }
170                 
171                 // internal stuff
172
173                 internal LogicalCallContext LogicalCallContext {
174                         get {
175                                 if (_lcc == null)
176                                         _lcc = new LogicalCallContext ();
177                                 return _lcc;
178                         }
179                         set {
180                                 _lcc = value;
181                         }
182                 }
183
184                 internal Dictionary<string, object> DataStore {
185                         get {
186                                 if (local_data == null)
187                                         local_data = new Dictionary<string, object> ();
188                                 return local_data;
189                         }
190                         set {
191                                 local_data = value;
192                         }
193                 }
194
195 #if !MOBILE
196                 internal SecurityContext SecurityContext {
197                         get {
198                                 if (_sc == null)
199                                         _sc = new SecurityContext ();
200                                 return _sc;
201                         }
202                         set { _sc = value; }
203                 }
204 #endif
205
206                 internal bool FlowSuppressed {
207                         get { return _suppressFlow; }
208                         set { _suppressFlow = value; }
209                 }
210
211                 internal bool CopyOnWrite { get; set; }
212
213                 public static bool IsFlowSuppressed ()
214                 {
215                         return Current.FlowSuppressed;
216                 }
217
218                 public static void RestoreFlow ()
219                 {
220                         ExecutionContext ec = Current;
221                         if (!ec.FlowSuppressed)
222                                 throw new InvalidOperationException ();
223
224                         ec.FlowSuppressed = false;
225                 }
226                 
227                 internal static void Run(ExecutionContext executionContext, ContextCallback callback, Object state, bool preserveSyncCtx)
228                 {
229                         Run (executionContext, callback, state);
230                 }
231
232                 [SecurityPermission (SecurityAction.LinkDemand, Infrastructure = true)]
233                 public static void Run (ExecutionContext executionContext, ContextCallback callback, object state)
234                 {
235                         if (executionContext == null) {
236                                 throw new InvalidOperationException (Locale.GetText (
237                                         "Null ExecutionContext"));
238                         }
239
240                         var prev = Current;
241                         try {
242                                 Thread.CurrentThread.ExecutionContext = executionContext;
243                                 callback (state);
244                         } finally {
245                                 Thread.CurrentThread.ExecutionContext = prev;
246                         }
247                 }
248
249                 public static AsyncFlowControl SuppressFlow ()
250                 {
251                         Thread t = Thread.CurrentThread;
252                         t.ExecutionContext.FlowSuppressed = true;
253                         return new AsyncFlowControl (t, AsyncFlowControlType.Execution);
254                 }
255
256                 internal static LogicalCallContext CreateLogicalCallContext (bool createEmpty)
257                 {
258                         var lcc = Current._lcc;
259                         LogicalCallContext ctx = null;
260
261                         if (lcc != null && lcc.HasInfo) {
262                                 ctx = new LogicalCallContext ();
263                                 foreach (DictionaryEntry entry in lcc.Datastore) {
264                                         ctx.SetData ((string)entry.Key, entry.Value);
265                                 }
266                         } else if (createEmpty)
267                                 ctx = new LogicalCallContext ();
268
269                         return ctx;
270                 }
271
272                 internal void FreeNamedDataSlot (string name)
273                 {
274                         if (_lcc != null)
275                                 _lcc.FreeNamedDataSlot (name);
276
277                         if (local_data != null)
278                                 local_data.Remove (name);
279                 }
280
281                 internal static ExecutionContext Current {
282                         get {
283                                 return Thread.CurrentThread.ExecutionContext;
284                         }
285                 }
286
287                 internal static ExecutionContext GetCurrentWritable ()
288                 {
289                         var current = Thread.CurrentThread.ExecutionContext;
290                         if (current.CopyOnWrite) {
291                                 current.CopyOnWrite = false;
292                                 current.CloneData (current);
293                         }
294
295                         return current;
296                 }
297         }
298 }