Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[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 //  Marek Safar (marek.safar@gmail.com)
8 //
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 // Copyright (C) 2014 Xamarin Inc (http://www.xamarin.com)
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
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.Generic;
38
39 namespace System.Threading {
40         [Serializable]
41         public sealed class ExecutionContext : ISerializable
42 #if NET_4_0
43                 , IDisposable
44 #endif
45         {
46 #if !MOBILE
47                 private SecurityContext _sc;
48 #endif
49                 private LogicalCallContext _lcc;
50                 private bool _suppressFlow;
51                 private bool _capture;
52                 Dictionary<string, object> local_data;
53
54                 internal ExecutionContext ()
55                 {
56                 }
57
58                 private ExecutionContext (ExecutionContext ec)
59                 {
60 #if !MOBILE
61                         if (ec._sc != null)
62                                 _sc = new SecurityContext (ec._sc);
63 #endif
64                         if (ec._lcc != null)
65                                 _lcc = (LogicalCallContext) ec._lcc.Clone ();
66
67                         _suppressFlow = ec._suppressFlow;
68                         _capture = true;
69                 }
70                 
71                 [MonoTODO]
72                 internal ExecutionContext (SerializationInfo info, StreamingContext context)
73                 {
74                         throw new NotImplementedException ();
75                 }
76
77                 public static ExecutionContext Capture ()
78                 {
79                         return Capture (true, false);
80                 }
81                 
82                 internal static ExecutionContext Capture (bool captureSyncContext, bool nullOnEmpty)
83                 {
84                         ExecutionContext ec = Current;
85                         if (ec.FlowSuppressed)
86                                 return null;
87
88                         if (nullOnEmpty
89 #if !MOBILE
90                          && ec._sc == null
91 #endif
92                                 && (ec._lcc == null || !ec._lcc.HasInfo))
93                                 return null;
94
95                         ExecutionContext capture = new ExecutionContext (ec);
96 #if !MOBILE
97                         if (SecurityManager.SecurityEnabled)
98                                 capture.SecurityContext = SecurityContext.Capture ();
99 #endif
100                         return capture;
101                 }
102                 
103                 public ExecutionContext CreateCopy ()
104                 {
105                         if (!_capture)
106                                 throw new InvalidOperationException ();
107
108                         return new ExecutionContext (this);
109                 }
110                 
111 #if NET_4_0
112                 public void Dispose ()
113                 {
114 #if !MOBILE
115                         if (_sc != null)
116                                 _sc.Dispose ();
117 #endif
118                 }
119 #endif
120
121                 [MonoTODO]
122                 [ReflectionPermission (SecurityAction.Demand, MemberAccess = true)]
123                 public void GetObjectData (SerializationInfo info, StreamingContext context)
124                 {
125                         if (info == null)
126                                 throw new ArgumentNullException ("info");
127                         throw new NotImplementedException ();
128                 }
129                 
130                 // internal stuff
131
132                 internal LogicalCallContext LogicalCallContext {
133                         get {
134                                 if (_lcc == null)
135                                         _lcc = new LogicalCallContext ();
136                                 return _lcc;
137                         }
138                         set {
139                                 _lcc = value;
140                         }
141                 }
142
143                 internal Dictionary<string, object> DataStore {
144                         get {
145                                 if (local_data == null)
146                                         local_data = new Dictionary<string, object> ();
147                                 return local_data;
148                         }
149                         set {
150                                 local_data = value;
151                         }
152                 }
153
154 #if !MOBILE
155                 internal SecurityContext SecurityContext {
156                         get {
157                                 if (_sc == null)
158                                         _sc = new SecurityContext ();
159                                 return _sc;
160                         }
161                         set { _sc = value; }
162                 }
163 #endif
164
165                 internal bool FlowSuppressed {
166                         get { return _suppressFlow; }
167                         set { _suppressFlow = value; }
168                 }
169
170                 public static bool IsFlowSuppressed ()
171                 {
172                         return Current.FlowSuppressed;
173                 }
174
175                 public static void RestoreFlow ()
176                 {
177                         ExecutionContext ec = Current;
178                         if (!ec.FlowSuppressed)
179                                 throw new InvalidOperationException ();
180
181                         ec.FlowSuppressed = false;
182                 }
183
184                 [SecurityPermission (SecurityAction.LinkDemand, Infrastructure = true)]
185                 public static void Run (ExecutionContext executionContext, ContextCallback callback, object state)
186                 {
187                         if (executionContext == null) {
188                                 throw new InvalidOperationException (Locale.GetText (
189                                         "Null ExecutionContext"));
190                         }
191
192                         var prev = Current;
193                         try {
194                                 Thread.CurrentThread.ExecutionContext = executionContext;
195                                 callback (state);
196                         } finally {
197                                 Thread.CurrentThread.ExecutionContext = prev;
198                         }
199                 }
200
201                 public static AsyncFlowControl SuppressFlow ()
202                 {
203                         Thread t = Thread.CurrentThread;
204                         t.ExecutionContext.FlowSuppressed = true;
205                         return new AsyncFlowControl (t, AsyncFlowControlType.Execution);
206                 }
207
208                 internal static LogicalCallContext CreateLogicalCallContext (bool createEmpty)
209                 {
210                         var lcc = Current._lcc;
211                         if (lcc == null) {
212                                 if (createEmpty)
213                                         lcc = new LogicalCallContext ();
214
215                                 return lcc;
216                         }
217
218                         return (LogicalCallContext) lcc.Clone ();
219                 }
220
221                 internal void FreeNamedDataSlot (string name)
222                 {
223                         if (_lcc != null)
224                                 _lcc.FreeNamedDataSlot (name);
225
226                         if (local_data != null)
227                                 local_data.Remove (name);
228                 }
229
230                 internal static ExecutionContext Current {
231                         get {
232                                 return Thread.CurrentThread.ExecutionContext;
233                         }
234                 }
235         }
236 }