Kill the MOONLIGHT define in corlib.
[mono.git] / mcs / class / corlib / System.Threading / ExecutionContext.cs
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.
28 //
29
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;
35
36 namespace System.Threading {
37         [Serializable]
38         public sealed class ExecutionContext : ISerializable
39 #if NET_4_0
40                 , IDisposable
41 #endif
42         {
43                 private SecurityContext _sc;
44                 private LogicalCallContext _lcc;
45                 private bool _suppressFlow;
46                 private bool _capture;
47
48                 internal ExecutionContext ()
49                 {
50                 }
51
52                 internal ExecutionContext (ExecutionContext ec)
53                 {
54                         if (ec._sc != null)
55                                 _sc = new SecurityContext (ec._sc);
56                         _suppressFlow = ec._suppressFlow;
57                         _capture = true;
58                 }
59                 
60                 [MonoTODO]
61                 internal ExecutionContext (SerializationInfo info, StreamingContext context)
62                 {
63                         throw new NotImplementedException ();
64                 }
65
66                 public static ExecutionContext Capture ()
67                 {
68                         return Capture (true);
69                 }
70                 
71                 internal static ExecutionContext Capture (bool captureSyncContext)
72                 {
73                         ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
74                         if (ec.FlowSuppressed)
75                                 return null;
76
77                         ExecutionContext capture = new ExecutionContext (ec);
78                         if (SecurityManager.SecurityEnabled)
79                                 capture.SecurityContext = SecurityContext.Capture ();
80 #if !MONOTOUCH
81                         capture.LogicalCallContext = CallContext.CreateLogicalCallContext (false);
82 #endif
83                         return capture;
84                 }
85                 
86                 public ExecutionContext CreateCopy ()
87                 {
88                         if (!_capture)
89                                 throw new InvalidOperationException ();
90
91                         return new ExecutionContext (this);
92                 }
93                 
94 #if NET_4_0
95                 public void Dispose ()
96                 {
97                         if (_sc != null)
98                                 _sc.Dispose ();
99                 }
100 #endif
101
102                 [MonoTODO]
103                 [ReflectionPermission (SecurityAction.Demand, MemberAccess = true)]
104                 public void GetObjectData (SerializationInfo info, StreamingContext context)
105                 {
106                         if (info == null)
107                                 throw new ArgumentNullException ("info");
108                         throw new NotImplementedException ();
109                 }
110                 
111                 // internal stuff
112
113                 internal LogicalCallContext LogicalCallContext {
114                         get {
115                                 if (_lcc == null)
116                                         return new LogicalCallContext ();
117                                 return _lcc;
118                         }
119                         set {
120                                 _lcc = value;
121                         }
122                 }
123
124                 internal SecurityContext SecurityContext {
125                         get {
126                                 if (_sc == null)
127                                         _sc = new SecurityContext ();
128                                 return _sc;
129                         }
130                         set { _sc = value; }
131                 }
132                 internal bool FlowSuppressed {
133                         get { return _suppressFlow; }
134                         set { _suppressFlow = value; }
135                 }
136
137                 // Note: Previous to version 2.0 only the CompressedStack and (sometimes!) the WindowsIdentity
138                 // were propagated to new threads. This is why ExecutionContext is internal in before NET_2_0.
139                 // It also means that all newer context classes should be here (i.e. inside the #if NET_2_0).
140
141                 public static bool IsFlowSuppressed ()
142                 {
143                         return Thread.CurrentThread.ExecutionContext.FlowSuppressed;
144                 }
145
146                 public static void RestoreFlow ()
147                 {
148                         ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
149                         if (!ec.FlowSuppressed)
150                                 throw new InvalidOperationException ();
151
152                         ec.FlowSuppressed = false;
153                 }
154
155                 [MonoTODO ("only the SecurityContext is considered")]
156                 [SecurityPermission (SecurityAction.LinkDemand, Infrastructure = true)]
157                 public static void Run (ExecutionContext executionContext, ContextCallback callback, object state)
158                 {
159                         if (executionContext == null) {
160                                 throw new InvalidOperationException (Locale.GetText (
161                                         "Null ExecutionContext"));
162                         }
163
164                         // FIXME: supporting more than one context should be done with executionContextSwitcher
165                         // and will requires a rewrite of this method
166                         var callContextCallBack = new ContextCallback (new Action<object> ((ostate) => {
167                                 var originalCallContext = CallContext.CreateLogicalCallContext (true);
168                                 try {
169                                         CallContext.SetCurrentCallContext (executionContext.LogicalCallContext);
170                                         callback (ostate);
171                                 } finally {
172                                         CallContext.SetCurrentCallContext (originalCallContext);
173                                 }
174                         }));
175                         SecurityContext.Run (executionContext.SecurityContext, callContextCallBack, state);
176                 }
177
178                 public static AsyncFlowControl SuppressFlow ()
179                 {
180                         Thread t = Thread.CurrentThread;
181                         t.ExecutionContext.FlowSuppressed = true;
182                         return new AsyncFlowControl (t, AsyncFlowControlType.Execution);
183                 }
184         }
185 }