ExecutionContext.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // System.Threading.ExecutionContext.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. // Marek Safar ([email protected])
  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. using System.Runtime.InteropServices;
  32. using System.Runtime.Serialization;
  33. using System.Security;
  34. using System.Security.Permissions;
  35. using System.Runtime.Remoting.Messaging;
  36. using System.Collections.Generic;
  37. namespace System.Threading {
  38. [Serializable]
  39. public sealed partial class ExecutionContext : ISerializable
  40. #if NET_4_0
  41. , IDisposable
  42. #endif
  43. {
  44. #if !MOBILE
  45. private SecurityContext _sc;
  46. #endif
  47. private LogicalCallContext _lcc;
  48. private bool _suppressFlow;
  49. private bool _capture;
  50. Dictionary<string, object> local_data;
  51. internal ExecutionContext ()
  52. {
  53. }
  54. private ExecutionContext (ExecutionContext ec)
  55. {
  56. #if !MOBILE
  57. if (ec._sc != null)
  58. _sc = new SecurityContext (ec._sc);
  59. #endif
  60. if (ec._lcc != null)
  61. _lcc = (LogicalCallContext) ec._lcc.Clone ();
  62. _suppressFlow = ec._suppressFlow;
  63. _capture = true;
  64. }
  65. [MonoTODO]
  66. internal ExecutionContext (SerializationInfo info, StreamingContext context)
  67. {
  68. throw new NotImplementedException ();
  69. }
  70. public static ExecutionContext Capture ()
  71. {
  72. return Capture (true, false);
  73. }
  74. internal static ExecutionContext Capture (bool captureSyncContext, bool nullOnEmpty)
  75. {
  76. ExecutionContext ec = Current;
  77. if (ec.FlowSuppressed)
  78. return null;
  79. if (nullOnEmpty
  80. #if !MOBILE
  81. && ec._sc == null
  82. #endif
  83. && (ec._lcc == null || !ec._lcc.HasInfo))
  84. return null;
  85. ExecutionContext capture = new ExecutionContext (ec);
  86. #if !MOBILE
  87. if (SecurityManager.SecurityEnabled)
  88. capture.SecurityContext = SecurityContext.Capture ();
  89. #endif
  90. return capture;
  91. }
  92. public ExecutionContext CreateCopy ()
  93. {
  94. if (!_capture)
  95. throw new InvalidOperationException ();
  96. return new ExecutionContext (this);
  97. }
  98. #if NET_4_0
  99. public void Dispose ()
  100. {
  101. #if !MOBILE
  102. if (_sc != null)
  103. _sc.Dispose ();
  104. #endif
  105. }
  106. #endif
  107. [MonoTODO]
  108. [ReflectionPermission (SecurityAction.Demand, MemberAccess = true)]
  109. public void GetObjectData (SerializationInfo info, StreamingContext context)
  110. {
  111. if (info == null)
  112. throw new ArgumentNullException ("info");
  113. throw new NotImplementedException ();
  114. }
  115. // internal stuff
  116. internal LogicalCallContext LogicalCallContext {
  117. get {
  118. if (_lcc == null)
  119. _lcc = new LogicalCallContext ();
  120. return _lcc;
  121. }
  122. set {
  123. _lcc = value;
  124. }
  125. }
  126. internal Dictionary<string, object> DataStore {
  127. get {
  128. if (local_data == null)
  129. local_data = new Dictionary<string, object> ();
  130. return local_data;
  131. }
  132. set {
  133. local_data = value;
  134. }
  135. }
  136. #if !MOBILE
  137. internal SecurityContext SecurityContext {
  138. get {
  139. if (_sc == null)
  140. _sc = new SecurityContext ();
  141. return _sc;
  142. }
  143. set { _sc = value; }
  144. }
  145. #endif
  146. internal bool FlowSuppressed {
  147. get { return _suppressFlow; }
  148. set { _suppressFlow = value; }
  149. }
  150. public static bool IsFlowSuppressed ()
  151. {
  152. return Current.FlowSuppressed;
  153. }
  154. public static void RestoreFlow ()
  155. {
  156. ExecutionContext ec = Current;
  157. if (!ec.FlowSuppressed)
  158. throw new InvalidOperationException ();
  159. ec.FlowSuppressed = false;
  160. }
  161. [SecurityPermission (SecurityAction.LinkDemand, Infrastructure = true)]
  162. public static void Run (ExecutionContext executionContext, ContextCallback callback, object state)
  163. {
  164. if (executionContext == null) {
  165. throw new InvalidOperationException (Locale.GetText (
  166. "Null ExecutionContext"));
  167. }
  168. var prev = Current;
  169. try {
  170. Thread.CurrentThread.ExecutionContext = executionContext;
  171. callback (state);
  172. } finally {
  173. Thread.CurrentThread.ExecutionContext = prev;
  174. }
  175. }
  176. public static AsyncFlowControl SuppressFlow ()
  177. {
  178. Thread t = Thread.CurrentThread;
  179. t.ExecutionContext.FlowSuppressed = true;
  180. return new AsyncFlowControl (t, AsyncFlowControlType.Execution);
  181. }
  182. internal static LogicalCallContext CreateLogicalCallContext (bool createEmpty)
  183. {
  184. var lcc = Current._lcc;
  185. if (lcc == null) {
  186. if (createEmpty)
  187. lcc = new LogicalCallContext ();
  188. return lcc;
  189. }
  190. return (LogicalCallContext) lcc.Clone ();
  191. }
  192. internal void FreeNamedDataSlot (string name)
  193. {
  194. if (_lcc != null)
  195. _lcc.FreeNamedDataSlot (name);
  196. if (local_data != null)
  197. local_data.Remove (name);
  198. }
  199. internal static ExecutionContext Current {
  200. get {
  201. return Thread.CurrentThread.ExecutionContext;
  202. }
  203. }
  204. }
  205. }