ExecutionContext.cs 4.4 KB

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