ExecutionContext.cs 7.6 KB

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