ExecutionContext.cs 7.2 KB

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