2 // System.Threading.ExecutionContext.cs
5 // Lluis Sanchez (lluis@novell.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
7 // Marek Safar (marek.safar@gmail.com)
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 // Copyright (C) 2014 Xamarin Inc (http://www.xamarin.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
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.Generic;
39 namespace System.Threading {
41 public sealed partial class ExecutionContext : ISerializable
46 internal struct Switcher
48 readonly ExecutionContext ec;
49 readonly LogicalCallContext _lcc;
50 readonly bool _suppressFlow;
51 readonly bool _capture;
52 readonly Dictionary<string, object> local_data;
53 readonly bool copy_on_write;
55 public Switcher (ExecutionContext ec)
59 this._suppressFlow = ec._suppressFlow;
60 this._capture = ec._capture;
61 this.local_data = ec.local_data;
62 this.copy_on_write = ec.CopyOnWrite;
71 public void Restore (ExecutionContext ec)
74 ec._suppressFlow = this._suppressFlow;
75 ec._capture = this._capture;
76 ec.local_data = this.local_data;
77 ec.CopyOnWrite = this.copy_on_write;
82 private SecurityContext _sc;
84 private LogicalCallContext _lcc;
85 private bool _suppressFlow;
86 private bool _capture;
87 Dictionary<string, object> local_data;
89 internal ExecutionContext ()
93 private ExecutionContext (ExecutionContext ec)
97 _suppressFlow = ec._suppressFlow;
101 void CloneData (ExecutionContext ec)
105 _sc = new SecurityContext (ec._sc);
108 _lcc = (LogicalCallContext) ec._lcc.Clone ();
112 internal ExecutionContext (SerializationInfo info, StreamingContext context)
114 throw new NotImplementedException ();
117 public static ExecutionContext Capture ()
119 return Capture (true, false);
122 internal static ExecutionContext Capture (bool captureSyncContext, bool nullOnEmpty)
124 var thread = Thread.CurrentThread;
125 if (nullOnEmpty && !thread.HasExecutionContext)
128 var ec = thread.ExecutionContext;
129 if (ec.FlowSuppressed)
136 && (ec._lcc == null || !ec._lcc.HasInfo))
139 ExecutionContext capture = new ExecutionContext (ec);
141 if (SecurityManager.SecurityEnabled)
142 capture.SecurityContext = SecurityContext.Capture ();
147 public ExecutionContext CreateCopy ()
150 throw new InvalidOperationException ();
152 return new ExecutionContext (this);
156 public void Dispose ()
166 [ReflectionPermission (SecurityAction.Demand, MemberAccess = true)]
167 public void GetObjectData (SerializationInfo info, StreamingContext context)
170 throw new ArgumentNullException ("info");
171 throw new NotImplementedException ();
176 internal LogicalCallContext LogicalCallContext {
179 _lcc = new LogicalCallContext ();
187 internal Dictionary<string, object> DataStore {
189 if (local_data == null)
190 local_data = new Dictionary<string, object> ();
199 internal SecurityContext SecurityContext {
202 _sc = new SecurityContext ();
209 internal bool FlowSuppressed {
210 get { return _suppressFlow; }
211 set { _suppressFlow = value; }
214 internal bool CopyOnWrite { get; set; }
216 public static bool IsFlowSuppressed ()
218 return Current.FlowSuppressed;
221 public static void RestoreFlow ()
223 ExecutionContext ec = Current;
224 if (!ec.FlowSuppressed)
225 throw new InvalidOperationException ();
227 ec.FlowSuppressed = false;
230 [SecurityPermission (SecurityAction.LinkDemand, Infrastructure = true)]
231 public static void Run (ExecutionContext executionContext, ContextCallback callback, object state)
233 if (executionContext == null) {
234 throw new InvalidOperationException (Locale.GetText (
235 "Null ExecutionContext"));
240 Thread.CurrentThread.ExecutionContext = executionContext;
243 Thread.CurrentThread.ExecutionContext = prev;
247 public static AsyncFlowControl SuppressFlow ()
249 Thread t = Thread.CurrentThread;
250 t.ExecutionContext.FlowSuppressed = true;
251 return new AsyncFlowControl (t, AsyncFlowControlType.Execution);
254 internal static LogicalCallContext CreateLogicalCallContext (bool createEmpty)
256 var lcc = Current._lcc;
259 lcc = new LogicalCallContext ();
264 return (LogicalCallContext) lcc.Clone ();
267 internal void FreeNamedDataSlot (string name)
270 _lcc.FreeNamedDataSlot (name);
272 if (local_data != null)
273 local_data.Remove (name);
276 internal static ExecutionContext Current {
278 return Thread.CurrentThread.ExecutionContext;
282 internal static ExecutionContext GetCurrentWritable ()
284 var current = Thread.CurrentThread.ExecutionContext;
285 if (current.CopyOnWrite) {
286 current.CopyOnWrite = false;
287 current.CloneData (current);