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