use MOONLIGHT symbol
[mcs.git] / class / corlib / System.Threading / ExecutionContext.cs
blob684caf0a2e4fc928c72e11a6fe4ae096e2563c71
1 //
2 // System.Threading.ExecutionContext.cs
3 //
4 // Authors:
5 // Lluis Sanchez (lluis@novell.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
7 //
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
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:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
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;
35 namespace System.Threading {
37 [Serializable]
38 public sealed class ExecutionContext : ISerializable {
39 #if !MOONLIGHT
40 private SecurityContext _sc;
41 #endif
42 private bool _suppressFlow;
43 private bool _capture;
45 internal ExecutionContext ()
49 internal ExecutionContext (ExecutionContext ec)
51 #if !MOONLIGHT
52 if (ec._sc != null)
53 _sc = new SecurityContext (ec._sc);
54 #endif
55 _suppressFlow = ec._suppressFlow;
56 _capture = true;
59 [MonoTODO]
60 internal ExecutionContext (SerializationInfo info, StreamingContext context)
62 throw new NotImplementedException ();
65 public static ExecutionContext Capture ()
67 ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
68 if (ec.FlowSuppressed)
69 return null;
71 ExecutionContext capture = new ExecutionContext (ec);
72 #if !MOONLIGHT
73 if (SecurityManager.SecurityEnabled)
74 capture.SecurityContext = SecurityContext.Capture ();
75 #endif
76 return capture;
79 public ExecutionContext CreateCopy ()
81 if (!_capture)
82 throw new InvalidOperationException ();
84 return new ExecutionContext (this);
87 [MonoTODO]
88 [ReflectionPermission (SecurityAction.Demand, MemberAccess = true)]
89 public void GetObjectData (SerializationInfo info, StreamingContext context)
91 if (info == null)
92 throw new ArgumentNullException ("info");
93 throw new NotImplementedException ();
96 // internal stuff
97 #if !MOONLIGHT
98 internal SecurityContext SecurityContext {
99 get {
100 if (_sc == null)
101 _sc = new SecurityContext ();
102 return _sc;
104 set { _sc = value; }
106 #endif
107 internal bool FlowSuppressed {
108 get { return _suppressFlow; }
109 set { _suppressFlow = value; }
112 // Note: Previous to version 2.0 only the CompressedStack and (sometimes!) the WindowsIdentity
113 // were propagated to new threads. This is why ExecutionContext is internal in before NET_2_0.
114 // It also means that all newer context classes should be here (i.e. inside the #if NET_2_0).
116 public static bool IsFlowSuppressed ()
118 return Thread.CurrentThread.ExecutionContext.FlowSuppressed;
121 public static void RestoreFlow ()
123 ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
124 if (!ec.FlowSuppressed)
125 throw new InvalidOperationException ();
127 ec.FlowSuppressed = false;
130 #if !MOONLIGHT
131 [MonoTODO ("only the SecurityContext is considered")]
132 [SecurityPermission (SecurityAction.LinkDemand, Infrastructure = true)]
133 public static void Run (ExecutionContext executionContext, ContextCallback callback, object state)
135 if (executionContext == null) {
136 throw new InvalidOperationException (Locale.GetText (
137 "Null ExecutionContext"));
140 // FIXME: supporting more than one context (the SecurityContext)
141 // will requires a rewrite of this method
143 SecurityContext.Run (executionContext.SecurityContext, callback, state);
146 public static AsyncFlowControl SuppressFlow ()
148 Thread t = Thread.CurrentThread;
149 t.ExecutionContext.FlowSuppressed = true;
150 return new AsyncFlowControl (t, AsyncFlowControlType.Execution);
152 #endif