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;
38 using System.Collections.Generic;
40 namespace System.Threading {
42 public sealed partial class ExecutionContext : ISerializable
45 internal struct Switcher
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;
54 public Switcher (ExecutionContext ec)
58 this._suppressFlow = ec._suppressFlow;
59 this._capture = ec._capture;
60 this.local_data = ec.local_data;
61 this.copy_on_write = ec.CopyOnWrite;
70 public void Restore (ExecutionContext ec)
73 ec._suppressFlow = this._suppressFlow;
74 ec._capture = this._capture;
75 ec.local_data = this.local_data;
76 ec.CopyOnWrite = this.copy_on_write;
81 private SecurityContext _sc;
83 private LogicalCallContext _lcc;
84 private bool _suppressFlow;
85 private bool _capture;
86 Dictionary<string, object> local_data;
88 internal ExecutionContext ()
92 private ExecutionContext (ExecutionContext ec)
96 _suppressFlow = ec._suppressFlow;
100 void CloneData (ExecutionContext ec)
104 _sc = new SecurityContext (ec._sc);
107 _lcc = (LogicalCallContext) ec._lcc.Clone ();
111 internal ExecutionContext (SerializationInfo info, StreamingContext context)
113 throw new NotImplementedException ();
116 public static ExecutionContext Capture ()
118 return Capture (true, false);
121 internal static ExecutionContext Capture (bool captureSyncContext, bool nullOnEmpty)
123 var thread = Thread.CurrentThread;
124 if (nullOnEmpty && !thread.HasExecutionContext)
127 var ec = thread.ExecutionContext;
128 if (ec.FlowSuppressed)
135 && (ec._lcc == null || !ec._lcc.HasInfo))
138 ExecutionContext capture = new ExecutionContext (ec);
140 if (SecurityManager.SecurityEnabled)
141 capture.SecurityContext = SecurityContext.Capture ();
146 public ExecutionContext CreateCopy ()
149 throw new InvalidOperationException ();
151 return new ExecutionContext (this);
154 public void Dispose ()
163 [ReflectionPermission (SecurityAction.Demand, MemberAccess = true)]
164 public void GetObjectData (SerializationInfo info, StreamingContext context)
167 throw new ArgumentNullException ("info");
168 throw new NotImplementedException ();
173 internal LogicalCallContext LogicalCallContext {
176 _lcc = new LogicalCallContext ();
184 internal Dictionary<string, object> DataStore {
186 if (local_data == null)
187 local_data = new Dictionary<string, object> ();
196 internal SecurityContext SecurityContext {
199 _sc = new SecurityContext ();
206 internal bool FlowSuppressed {
207 get { return _suppressFlow; }
208 set { _suppressFlow = value; }
211 internal bool CopyOnWrite { get; set; }
213 public static bool IsFlowSuppressed ()
215 return Current.FlowSuppressed;
218 public static void RestoreFlow ()
220 ExecutionContext ec = Current;
221 if (!ec.FlowSuppressed)
222 throw new InvalidOperationException ();
224 ec.FlowSuppressed = false;
227 internal static void Run(ExecutionContext executionContext, ContextCallback callback, Object state, bool preserveSyncCtx)
229 Run (executionContext, callback, state);
232 [SecurityPermission (SecurityAction.LinkDemand, Infrastructure = true)]
233 public static void Run (ExecutionContext executionContext, ContextCallback callback, object state)
235 if (executionContext == null) {
236 throw new InvalidOperationException (Locale.GetText (
237 "Null ExecutionContext"));
242 Thread.CurrentThread.ExecutionContext = executionContext;
245 Thread.CurrentThread.ExecutionContext = prev;
249 public static AsyncFlowControl SuppressFlow ()
251 Thread t = Thread.CurrentThread;
252 t.ExecutionContext.FlowSuppressed = true;
253 return new AsyncFlowControl (t, AsyncFlowControlType.Execution);
256 internal static LogicalCallContext CreateLogicalCallContext (bool createEmpty)
258 var lcc = Current._lcc;
259 LogicalCallContext ctx = null;
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);
266 } else if (createEmpty)
267 ctx = new LogicalCallContext ();
272 internal void FreeNamedDataSlot (string name)
275 _lcc.FreeNamedDataSlot (name);
277 if (local_data != null)
278 local_data.Remove (name);
281 internal static ExecutionContext Current {
283 return Thread.CurrentThread.ExecutionContext;
287 internal static ExecutionContext GetCurrentWritable ()
289 var current = Thread.CurrentThread.ExecutionContext;
290 if (current.CopyOnWrite) {
291 current.CopyOnWrite = false;
292 current.CloneData (current);