ExecutionContext.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. , IDisposable
  41. {
  42. internal struct Switcher
  43. {
  44. readonly ExecutionContext ec;
  45. readonly LogicalCallContext _lcc;
  46. readonly bool _suppressFlow;
  47. readonly bool _capture;
  48. readonly Dictionary<string, object> local_data;
  49. readonly bool copy_on_write;
  50. public Switcher (ExecutionContext ec)
  51. {
  52. this.ec = ec;
  53. this._lcc = ec._lcc;
  54. this._suppressFlow = ec._suppressFlow;
  55. this._capture = ec._capture;
  56. this.local_data = ec.local_data;
  57. this.copy_on_write = ec.CopyOnWrite;
  58. }
  59. public bool IsEmpty {
  60. get {
  61. return ec == null;
  62. }
  63. }
  64. public void Restore (ExecutionContext ec)
  65. {
  66. ec._lcc = this._lcc;
  67. ec._suppressFlow = this._suppressFlow;
  68. ec._capture = this._capture;
  69. ec.local_data = this.local_data;
  70. ec.CopyOnWrite = this.copy_on_write;
  71. }
  72. }
  73. #if !MOBILE
  74. private SecurityContext _sc;
  75. #endif
  76. private LogicalCallContext _lcc;
  77. private bool _suppressFlow;
  78. private bool _capture;
  79. Dictionary<string, object> local_data;
  80. internal ExecutionContext ()
  81. {
  82. }
  83. private ExecutionContext (ExecutionContext ec)
  84. {
  85. CloneData (ec);
  86. _suppressFlow = ec._suppressFlow;
  87. _capture = true;
  88. }
  89. void CloneData (ExecutionContext ec)
  90. {
  91. #if !MOBILE
  92. if (ec._sc != null)
  93. _sc = new SecurityContext (ec._sc);
  94. #endif
  95. if (ec._lcc != null)
  96. _lcc = (LogicalCallContext) ec._lcc.Clone ();
  97. }
  98. [MonoTODO]
  99. internal ExecutionContext (SerializationInfo info, StreamingContext context)
  100. {
  101. throw new NotImplementedException ();
  102. }
  103. public static ExecutionContext Capture ()
  104. {
  105. return Capture (true, false);
  106. }
  107. internal static ExecutionContext Capture (bool captureSyncContext, bool nullOnEmpty)
  108. {
  109. var thread = Thread.CurrentThread;
  110. if (nullOnEmpty && !thread.HasExecutionContext)
  111. return null;
  112. var ec = thread.ExecutionContext;
  113. if (ec.FlowSuppressed)
  114. return null;
  115. if (nullOnEmpty
  116. #if !MOBILE
  117. && ec._sc == null
  118. #endif
  119. && (ec._lcc == null || !ec._lcc.HasInfo))
  120. return null;
  121. ExecutionContext capture = new ExecutionContext (ec);
  122. #if !MOBILE
  123. if (SecurityManager.SecurityEnabled)
  124. capture.SecurityContext = SecurityContext.Capture ();
  125. #endif
  126. return capture;
  127. }
  128. public ExecutionContext CreateCopy ()
  129. {
  130. if (!_capture)
  131. throw new InvalidOperationException ();
  132. return new ExecutionContext (this);
  133. }
  134. public void Dispose ()
  135. {
  136. #if !MOBILE
  137. if (_sc != null)
  138. _sc.Dispose ();
  139. #endif
  140. }
  141. [MonoTODO]
  142. [ReflectionPermission (SecurityAction.Demand, MemberAccess = true)]
  143. public void GetObjectData (SerializationInfo info, StreamingContext context)
  144. {
  145. if (info == null)
  146. throw new ArgumentNullException ("info");
  147. throw new NotImplementedException ();
  148. }
  149. // internal stuff
  150. internal LogicalCallContext LogicalCallContext {
  151. get {
  152. if (_lcc == null)
  153. _lcc = new LogicalCallContext ();
  154. return _lcc;
  155. }
  156. set {
  157. _lcc = value;
  158. }
  159. }
  160. internal Dictionary<string, object> DataStore {
  161. get {
  162. if (local_data == null)
  163. local_data = new Dictionary<string, object> ();
  164. return local_data;
  165. }
  166. set {
  167. local_data = value;
  168. }
  169. }
  170. #if !MOBILE
  171. internal SecurityContext SecurityContext {
  172. get {
  173. if (_sc == null)
  174. _sc = new SecurityContext ();
  175. return _sc;
  176. }
  177. set { _sc = value; }
  178. }
  179. #endif
  180. internal bool FlowSuppressed {
  181. get { return _suppressFlow; }
  182. set { _suppressFlow = value; }
  183. }
  184. internal bool CopyOnWrite { get; set; }
  185. public static bool IsFlowSuppressed ()
  186. {
  187. return Current.FlowSuppressed;
  188. }
  189. public static void RestoreFlow ()
  190. {
  191. ExecutionContext ec = Current;
  192. if (!ec.FlowSuppressed)
  193. throw new InvalidOperationException ();
  194. ec.FlowSuppressed = false;
  195. }
  196. internal static void Run(ExecutionContext executionContext, ContextCallback callback, Object state, bool preserveSyncCtx)
  197. {
  198. Run (executionContext, callback, state);
  199. }
  200. [SecurityPermission (SecurityAction.LinkDemand, Infrastructure = true)]
  201. public static void Run (ExecutionContext executionContext, ContextCallback callback, object state)
  202. {
  203. if (executionContext == null) {
  204. throw new InvalidOperationException (Locale.GetText (
  205. "Null ExecutionContext"));
  206. }
  207. var prev = Current;
  208. try {
  209. Thread.CurrentThread.ExecutionContext = executionContext;
  210. callback (state);
  211. } finally {
  212. Thread.CurrentThread.ExecutionContext = prev;
  213. }
  214. }
  215. public static AsyncFlowControl SuppressFlow ()
  216. {
  217. Thread t = Thread.CurrentThread;
  218. t.ExecutionContext.FlowSuppressed = true;
  219. return new AsyncFlowControl (t, AsyncFlowControlType.Execution);
  220. }
  221. internal static LogicalCallContext CreateLogicalCallContext (bool createEmpty)
  222. {
  223. var lcc = Current._lcc;
  224. if (lcc == null) {
  225. if (createEmpty)
  226. lcc = new LogicalCallContext ();
  227. return lcc;
  228. }
  229. return (LogicalCallContext) lcc.Clone ();
  230. }
  231. internal void FreeNamedDataSlot (string name)
  232. {
  233. if (_lcc != null)
  234. _lcc.FreeNamedDataSlot (name);
  235. if (local_data != null)
  236. local_data.Remove (name);
  237. }
  238. internal static ExecutionContext Current {
  239. get {
  240. return Thread.CurrentThread.ExecutionContext;
  241. }
  242. }
  243. internal static ExecutionContext GetCurrentWritable ()
  244. {
  245. var current = Thread.CurrentThread.ExecutionContext;
  246. if (current.CopyOnWrite) {
  247. current.CopyOnWrite = false;
  248. current.CloneData (current);
  249. }
  250. return current;
  251. }
  252. }
  253. }