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
44 internal struct Switcher
46 readonly ExecutionContext ec;
47 readonly LogicalCallContext _lcc;
48 readonly bool _suppressFlow;
49 readonly bool _capture;
50 readonly Dictionary<string, object> local_data;
51 readonly bool copy_on_write;
53 public Switcher (ExecutionContext ec)
57 this._suppressFlow = ec._suppressFlow;
58 this._capture = ec._capture;
59 this.local_data = ec.local_data;
60 this.copy_on_write = ec.CopyOnWrite;
69 public void Restore (ExecutionContext ec)
72 ec._suppressFlow = this._suppressFlow;
73 ec._capture = this._capture;
74 ec.local_data = this.local_data;
75 ec.CopyOnWrite = this.copy_on_write;
80 private SecurityContext _sc;
82 private LogicalCallContext _lcc;
83 private bool _suppressFlow;
84 private bool _capture;
85 Dictionary<string, object> local_data;
87 internal ExecutionContext ()
91 private ExecutionContext (ExecutionContext ec)
95 _suppressFlow = ec._suppressFlow;
99 void CloneData (ExecutionContext ec)
103 _sc = new SecurityContext (ec._sc);
106 _lcc = (LogicalCallContext) ec._lcc.Clone ();
110 internal ExecutionContext (SerializationInfo info, StreamingContext context)
112 throw new NotImplementedException ();
115 public static ExecutionContext Capture ()
117 return Capture (true, false);
120 internal static ExecutionContext Capture (bool captureSyncContext, bool nullOnEmpty)
122 var thread = Thread.CurrentThread;
123 if (nullOnEmpty && !thread.HasExecutionContext)
126 var ec = thread.ExecutionContext;
127 if (ec.FlowSuppressed)
134 && (ec._lcc == null || !ec._lcc.HasInfo))
137 ExecutionContext capture = new ExecutionContext (ec);
139 if (SecurityManager.SecurityEnabled)
140 capture.SecurityContext = SecurityContext.Capture ();
145 public ExecutionContext CreateCopy ()
148 throw new InvalidOperationException ();
150 return new ExecutionContext (this);
153 public void Dispose ()
162 [ReflectionPermission (SecurityAction.Demand, MemberAccess = true)]
163 public void GetObjectData (SerializationInfo info, StreamingContext context)
166 throw new ArgumentNullException ("info");
167 throw new NotImplementedException ();
172 internal LogicalCallContext LogicalCallContext {
175 _lcc = new LogicalCallContext ();
183 internal Dictionary<string, object> DataStore {
185 if (local_data == null)
186 local_data = new Dictionary<string, object> ();
195 internal SecurityContext SecurityContext {
198 _sc = new SecurityContext ();
205 internal bool FlowSuppressed {
206 get { return _suppressFlow; }
207 set { _suppressFlow = value; }
210 internal bool CopyOnWrite { get; set; }
212 public static bool IsFlowSuppressed ()
214 return Current.FlowSuppressed;
217 public static void RestoreFlow ()
219 ExecutionContext ec = Current;
220 if (!ec.FlowSuppressed)
221 throw new InvalidOperationException ();
223 ec.FlowSuppressed = false;
226 internal static void Run(ExecutionContext executionContext, ContextCallback callback, Object state, bool preserveSyncCtx)
228 Run (executionContext, callback, state);
231 [SecurityPermission (SecurityAction.LinkDemand, Infrastructure = true)]
232 public static void Run (ExecutionContext executionContext, ContextCallback callback, object state)
234 if (executionContext == null) {
235 throw new InvalidOperationException (Locale.GetText (
236 "Null ExecutionContext"));
241 Thread.CurrentThread.ExecutionContext = executionContext;
244 Thread.CurrentThread.ExecutionContext = prev;
248 public static AsyncFlowControl SuppressFlow ()
250 Thread t = Thread.CurrentThread;
251 t.ExecutionContext.FlowSuppressed = true;
252 return new AsyncFlowControl (t, AsyncFlowControlType.Execution);
255 internal static LogicalCallContext CreateLogicalCallContext (bool createEmpty)
257 var lcc = Current._lcc;
260 lcc = new LogicalCallContext ();
265 return (LogicalCallContext) lcc.Clone ();
268 internal void FreeNamedDataSlot (string name)
271 _lcc.FreeNamedDataSlot (name);
273 if (local_data != null)
274 local_data.Remove (name);
277 internal static ExecutionContext Current {
279 return Thread.CurrentThread.ExecutionContext;
283 internal static ExecutionContext GetCurrentWritable ()
285 var current = Thread.CurrentThread.ExecutionContext;
286 if (current.CopyOnWrite) {
287 current.CopyOnWrite = false;
288 current.CloneData (current);