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 class ExecutionContext : ISerializable
47 private SecurityContext _sc;
49 private LogicalCallContext _lcc;
50 private bool _suppressFlow;
51 private bool _capture;
52 Dictionary<string, object> local_data;
54 internal ExecutionContext ()
58 private ExecutionContext (ExecutionContext ec)
62 _sc = new SecurityContext (ec._sc);
65 _lcc = (LogicalCallContext) ec._lcc.Clone ();
67 _suppressFlow = ec._suppressFlow;
72 internal ExecutionContext (SerializationInfo info, StreamingContext context)
74 throw new NotImplementedException ();
77 public static ExecutionContext Capture ()
79 return Capture (true, false);
82 internal static ExecutionContext Capture (bool captureSyncContext, bool nullOnEmpty)
84 ExecutionContext ec = Current;
85 if (ec.FlowSuppressed)
92 && (ec._lcc == null || !ec._lcc.HasInfo))
95 ExecutionContext capture = new ExecutionContext (ec);
97 if (SecurityManager.SecurityEnabled)
98 capture.SecurityContext = SecurityContext.Capture ();
103 public ExecutionContext CreateCopy ()
106 throw new InvalidOperationException ();
108 return new ExecutionContext (this);
112 public void Dispose ()
122 [ReflectionPermission (SecurityAction.Demand, MemberAccess = true)]
123 public void GetObjectData (SerializationInfo info, StreamingContext context)
126 throw new ArgumentNullException ("info");
127 throw new NotImplementedException ();
132 internal LogicalCallContext LogicalCallContext {
135 _lcc = new LogicalCallContext ();
143 internal Dictionary<string, object> DataStore {
145 if (local_data == null)
146 local_data = new Dictionary<string, object> ();
155 internal SecurityContext SecurityContext {
158 _sc = new SecurityContext ();
165 internal bool FlowSuppressed {
166 get { return _suppressFlow; }
167 set { _suppressFlow = value; }
170 public static bool IsFlowSuppressed ()
172 return Current.FlowSuppressed;
175 public static void RestoreFlow ()
177 ExecutionContext ec = Current;
178 if (!ec.FlowSuppressed)
179 throw new InvalidOperationException ();
181 ec.FlowSuppressed = false;
184 [SecurityPermission (SecurityAction.LinkDemand, Infrastructure = true)]
185 public static void Run (ExecutionContext executionContext, ContextCallback callback, object state)
187 if (executionContext == null) {
188 throw new InvalidOperationException (Locale.GetText (
189 "Null ExecutionContext"));
194 Thread.CurrentThread.ExecutionContext = executionContext;
197 Thread.CurrentThread.ExecutionContext = prev;
201 public static AsyncFlowControl SuppressFlow ()
203 Thread t = Thread.CurrentThread;
204 t.ExecutionContext.FlowSuppressed = true;
205 return new AsyncFlowControl (t, AsyncFlowControlType.Execution);
208 internal static LogicalCallContext CreateLogicalCallContext (bool createEmpty)
210 var lcc = Current._lcc;
213 lcc = new LogicalCallContext ();
218 return (LogicalCallContext) lcc.Clone ();
221 internal void FreeNamedDataSlot (string name)
224 _lcc.FreeNamedDataSlot (name);
226 if (local_data != null)
227 local_data.Remove (name);
230 internal static ExecutionContext Current {
232 return Thread.CurrentThread.ExecutionContext;