ExecutionContext.cs 5.4 KB

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