2 // System.Threading.ExecutionContext.cs
5 // Lluis Sanchez (lluis@novell.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Runtime.InteropServices;
31 using System.Runtime.Serialization;
32 using System.Security;
33 using System.Security.Permissions;
34 using System.Runtime.Remoting.Messaging;
36 namespace System.Threading {
38 public sealed class ExecutionContext : ISerializable
44 private SecurityContext _sc;
46 private LogicalCallContext _lcc;
47 private bool _suppressFlow;
48 private bool _capture;
50 internal ExecutionContext ()
54 internal ExecutionContext (ExecutionContext ec)
58 _sc = new SecurityContext (ec._sc);
60 _suppressFlow = ec._suppressFlow;
65 internal ExecutionContext (SerializationInfo info, StreamingContext context)
67 throw new NotImplementedException ();
70 public static ExecutionContext Capture ()
72 return Capture (true);
75 internal static ExecutionContext Capture (bool captureSyncContext)
77 ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
78 if (ec.FlowSuppressed)
81 ExecutionContext capture = new ExecutionContext (ec);
83 if (SecurityManager.SecurityEnabled)
84 capture.SecurityContext = SecurityContext.Capture ();
87 capture.LogicalCallContext = CallContext.CreateLogicalCallContext (false);
92 public ExecutionContext CreateCopy ()
95 throw new InvalidOperationException ();
97 return new ExecutionContext (this);
101 public void Dispose ()
109 [ReflectionPermission (SecurityAction.Demand, MemberAccess = true)]
110 public void GetObjectData (SerializationInfo info, StreamingContext context)
113 throw new ArgumentNullException ("info");
114 throw new NotImplementedException ();
119 internal LogicalCallContext LogicalCallContext {
122 return new LogicalCallContext ();
131 internal SecurityContext SecurityContext {
134 _sc = new SecurityContext ();
140 internal bool FlowSuppressed {
141 get { return _suppressFlow; }
142 set { _suppressFlow = value; }
145 // Note: Previous to version 2.0 only the CompressedStack and (sometimes!) the WindowsIdentity
146 // were propagated to new threads. This is why ExecutionContext is internal in before NET_2_0.
147 // It also means that all newer context classes should be here (i.e. inside the #if NET_2_0).
149 public static bool IsFlowSuppressed ()
151 return Thread.CurrentThread.ExecutionContext.FlowSuppressed;
154 public static void RestoreFlow ()
156 ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
157 if (!ec.FlowSuppressed)
158 throw new InvalidOperationException ();
160 ec.FlowSuppressed = false;
164 [MonoTODO ("only the SecurityContext is considered")]
165 [SecurityPermission (SecurityAction.LinkDemand, Infrastructure = true)]
166 public static void Run (ExecutionContext executionContext, ContextCallback callback, object state)
168 if (executionContext == null) {
169 throw new InvalidOperationException (Locale.GetText (
170 "Null ExecutionContext"));
173 // FIXME: supporting more than one context should be done with executionContextSwitcher
174 // and will requires a rewrite of this method
175 var callContextCallBack = new ContextCallback (new Action<object> ((ostate) => {
176 var originalCallContext = CallContext.CreateLogicalCallContext (true);
178 CallContext.SetCurrentCallContext (executionContext.LogicalCallContext);
181 CallContext.SetCurrentCallContext (originalCallContext);
184 SecurityContext.Run (executionContext.SecurityContext, callContextCallBack, state);
187 public static AsyncFlowControl SuppressFlow ()
189 Thread t = Thread.CurrentThread;
190 t.ExecutionContext.FlowSuppressed = true;
191 return new AsyncFlowControl (t, AsyncFlowControlType.Execution);