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