ExecutionContext.cs 4.4 KB

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