Thread.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. //
  2. // System.Threading.Thread.cs
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. //
  7. // (C) Ximian, Inc. http://www.ximian.com
  8. // Copyright (C) 2004-2006 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.Remoting.Contexts;
  30. using System.Runtime.Serialization;
  31. using System.Runtime.Serialization.Formatters.Binary;
  32. using System.Security.Permissions;
  33. using System.Security.Principal;
  34. using System.Globalization;
  35. using System.Runtime.CompilerServices;
  36. using System.Runtime.InteropServices;
  37. using System.IO;
  38. using System.Collections.Generic;
  39. using System.Reflection;
  40. using System.Security;
  41. using System.Runtime.ConstrainedExecution;
  42. namespace System.Threading {
  43. [StructLayout (LayoutKind.Sequential)]
  44. sealed class InternalThread : CriticalFinalizerObject {
  45. #pragma warning disable 169, 414, 649
  46. #region Sync with metadata/object-internals.h
  47. int lock_thread_id;
  48. // stores a thread handle
  49. internal IntPtr system_thread_handle;
  50. /* Note this is an opaque object (an array), not a CultureInfo */
  51. private object cached_culture_info;
  52. /* accessed only from unmanaged code */
  53. private IntPtr name;
  54. private int name_len;
  55. private ThreadState state;
  56. private object abort_exc;
  57. private int abort_state_handle;
  58. /* thread_id is only accessed from unmanaged code */
  59. internal Int64 thread_id;
  60. /* start_notify is used by the runtime to signal that Start()
  61. * is ok to return
  62. */
  63. private IntPtr start_notify;
  64. private IntPtr stack_ptr;
  65. private UIntPtr static_data; /* GC-tracked */
  66. private IntPtr runtime_thread_info;
  67. /* current System.Runtime.Remoting.Contexts.Context instance
  68. keep as an object to avoid triggering its class constructor when not needed */
  69. private object current_appcontext;
  70. private object pending_exception;
  71. private object root_domain_thread;
  72. internal byte[] _serialized_principal;
  73. internal int _serialized_principal_version;
  74. private IntPtr appdomain_refs;
  75. private int interruption_requested;
  76. private IntPtr synch_cs;
  77. internal bool threadpool_thread;
  78. private bool thread_interrupt_requested;
  79. /* These are used from managed code */
  80. internal int stack_size;
  81. internal byte apartment_state;
  82. internal volatile int critical_region_level;
  83. internal int managed_id;
  84. private int small_id;
  85. private IntPtr manage_callback;
  86. private IntPtr interrupt_on_stop;
  87. private IntPtr flags;
  88. private IntPtr thread_pinning_ref;
  89. private IntPtr async_invoke_method;
  90. /*
  91. * These fields are used to avoid having to increment corlib versions
  92. * when a new field is added to the unmanaged MonoThread structure.
  93. */
  94. private IntPtr unused1;
  95. private IntPtr unused2;
  96. #endregion
  97. #pragma warning restore 169, 414, 649
  98. // Closes the system thread handle
  99. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  100. private extern void Thread_free_internal(IntPtr handle);
  101. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
  102. ~InternalThread() {
  103. Thread_free_internal(system_thread_handle);
  104. }
  105. }
  106. [StructLayout (LayoutKind.Sequential)]
  107. public sealed partial class Thread {
  108. #pragma warning disable 414
  109. #region Sync with metadata/object-internals.h
  110. private InternalThread internal_thread;
  111. object m_ThreadStartArg;
  112. #endregion
  113. #pragma warning restore 414
  114. IPrincipal principal;
  115. int principal_version;
  116. bool current_culture_set;
  117. bool current_ui_culture_set;
  118. CultureInfo current_culture;
  119. CultureInfo current_ui_culture;
  120. // the name of current_thread is
  121. // important because they are used by the runtime.
  122. [ThreadStatic]
  123. static Thread current_thread;
  124. static internal CultureInfo default_culture;
  125. static internal CultureInfo default_ui_culture;
  126. // can be both a ThreadStart and a ParameterizedThreadStart
  127. private MulticastDelegate m_Delegate;
  128. private ExecutionContext m_ExecutionContext; // this call context follows the logical thread
  129. private bool m_ExecutionContextBelongsToOuterScope;
  130. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  131. private extern void ConstructInternalThread ();
  132. private InternalThread Internal {
  133. get {
  134. if (internal_thread == null)
  135. ConstructInternalThread ();
  136. return internal_thread;
  137. }
  138. }
  139. public static Context CurrentContext {
  140. [SecurityPermission (SecurityAction.LinkDemand, Infrastructure=true)]
  141. get {
  142. return(AppDomain.InternalGetContext ());
  143. }
  144. }
  145. /*
  146. * These two methods return an array in the target
  147. * domain with the same content as the argument. If
  148. * the argument is already in the target domain, then
  149. * the argument is returned, otherwise a copy.
  150. */
  151. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  152. private extern static byte[] ByteArrayToRootDomain (byte[] arr);
  153. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  154. private extern static byte[] ByteArrayToCurrentDomain (byte[] arr);
  155. static void DeserializePrincipal (Thread th)
  156. {
  157. MemoryStream ms = new MemoryStream (ByteArrayToCurrentDomain (th.Internal._serialized_principal));
  158. int type = ms.ReadByte ();
  159. if (type == 0) {
  160. BinaryFormatter bf = new BinaryFormatter ();
  161. th.principal = (IPrincipal) bf.Deserialize (ms);
  162. th.principal_version = th.Internal._serialized_principal_version;
  163. } else if (type == 1) {
  164. BinaryReader reader = new BinaryReader (ms);
  165. string name = reader.ReadString ();
  166. string auth_type = reader.ReadString ();
  167. int n_roles = reader.ReadInt32 ();
  168. string [] roles = null;
  169. if (n_roles >= 0) {
  170. roles = new string [n_roles];
  171. for (int i = 0; i < n_roles; i++)
  172. roles [i] = reader.ReadString ();
  173. }
  174. th.principal = new GenericPrincipal (new GenericIdentity (name, auth_type), roles);
  175. } else if (type == 2 || type == 3) {
  176. string [] roles = type == 2 ? null : new string [0];
  177. th.principal = new GenericPrincipal (new GenericIdentity ("", ""), roles);
  178. }
  179. }
  180. static void SerializePrincipal (Thread th, IPrincipal value)
  181. {
  182. MemoryStream ms = new MemoryStream ();
  183. bool done = false;
  184. if (value.GetType () == typeof (GenericPrincipal)) {
  185. GenericPrincipal gp = (GenericPrincipal) value;
  186. if (gp.Identity != null && gp.Identity.GetType () == typeof (GenericIdentity)) {
  187. GenericIdentity id = (GenericIdentity) gp.Identity;
  188. if (id.Name == "" && id.AuthenticationType == "") {
  189. if (gp.Roles == null) {
  190. ms.WriteByte (2);
  191. done = true;
  192. } else if (gp.Roles.Length == 0) {
  193. ms.WriteByte (3);
  194. done = true;
  195. }
  196. } else {
  197. ms.WriteByte (1);
  198. BinaryWriter br = new BinaryWriter (ms);
  199. br.Write (gp.Identity.Name);
  200. br.Write (gp.Identity.AuthenticationType);
  201. string [] roles = gp.Roles;
  202. if (roles == null) {
  203. br.Write ((int) (-1));
  204. } else {
  205. br.Write (roles.Length);
  206. foreach (string s in roles) {
  207. br.Write (s);
  208. }
  209. }
  210. br.Flush ();
  211. done = true;
  212. }
  213. }
  214. }
  215. if (!done) {
  216. ms.WriteByte (0);
  217. BinaryFormatter bf = new BinaryFormatter ();
  218. try {
  219. bf.Serialize (ms, value);
  220. } catch {}
  221. }
  222. th.Internal._serialized_principal = ByteArrayToRootDomain (ms.ToArray ());
  223. }
  224. public static IPrincipal CurrentPrincipal {
  225. get {
  226. Thread th = CurrentThread;
  227. if (th.principal_version != th.Internal._serialized_principal_version)
  228. th.principal = null;
  229. if (th.principal != null)
  230. return th.principal;
  231. if (th.Internal._serialized_principal != null) {
  232. try {
  233. DeserializePrincipal (th);
  234. return th.principal;
  235. } catch {}
  236. }
  237. th.principal = GetDomain ().DefaultPrincipal;
  238. th.principal_version = th.Internal._serialized_principal_version;
  239. return th.principal;
  240. }
  241. [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
  242. set {
  243. Thread th = CurrentThread;
  244. if (value != GetDomain ().DefaultPrincipal) {
  245. ++th.Internal._serialized_principal_version;
  246. try {
  247. SerializePrincipal (th, value);
  248. } catch (Exception) {
  249. th.Internal._serialized_principal = null;
  250. }
  251. th.principal_version = th.Internal._serialized_principal_version;
  252. } else {
  253. th.Internal._serialized_principal = null;
  254. }
  255. th.principal = value;
  256. }
  257. }
  258. // Looks up the object associated with the current thread
  259. // this is called by the JIT directly, too
  260. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  261. private extern static InternalThread CurrentInternalThread_internal();
  262. public static Thread CurrentThread {
  263. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
  264. get {
  265. if (current_thread == null)
  266. current_thread = new Thread (CurrentInternalThread_internal ());
  267. return current_thread;
  268. }
  269. }
  270. internal static int CurrentThreadId {
  271. get {
  272. return (int)(CurrentThread.internal_thread.thread_id);
  273. }
  274. }
  275. public static AppDomain GetDomain() {
  276. return AppDomain.CurrentDomain;
  277. }
  278. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  279. public extern static int GetDomainID();
  280. // Returns the system thread handle
  281. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  282. private extern IntPtr Thread_internal (MulticastDelegate start);
  283. private Thread (InternalThread it) {
  284. internal_thread = it;
  285. }
  286. // part of ".NETPortable,Version=v4.0,Profile=Profile3" i.e. FX4 and SL4
  287. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
  288. ~Thread ()
  289. {
  290. }
  291. [Obsolete ("Deprecated in favor of GetApartmentState, SetApartmentState and TrySetApartmentState.")]
  292. public ApartmentState ApartmentState {
  293. get {
  294. if ((ThreadState & ThreadState.Stopped) != 0)
  295. throw new ThreadStateException ("Thread is dead; state can not be accessed.");
  296. return (ApartmentState)Internal.apartment_state;
  297. }
  298. set {
  299. TrySetApartmentState (value);
  300. }
  301. }
  302. //[MethodImplAttribute (MethodImplOptions.InternalCall)]
  303. //private static extern int current_lcid ();
  304. public CultureInfo CurrentCulture {
  305. get {
  306. CultureInfo culture = current_culture;
  307. if (current_culture_set && culture != null)
  308. return culture;
  309. if (default_culture != null)
  310. return default_culture;
  311. current_culture = culture = CultureInfo.ConstructCurrentCulture ();
  312. return culture;
  313. }
  314. [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
  315. set {
  316. if (value == null)
  317. throw new ArgumentNullException ("value");
  318. value.CheckNeutral ();
  319. current_culture = value;
  320. current_culture_set = true;
  321. }
  322. }
  323. public CultureInfo CurrentUICulture {
  324. get {
  325. CultureInfo culture = current_ui_culture;
  326. if (current_ui_culture_set && culture != null)
  327. return culture;
  328. if (default_ui_culture != null)
  329. return default_ui_culture;
  330. current_ui_culture = culture = CultureInfo.ConstructCurrentUICulture ();
  331. return culture;
  332. }
  333. set {
  334. if (value == null)
  335. throw new ArgumentNullException ("value");
  336. current_ui_culture = value;
  337. current_ui_culture_set = true;
  338. }
  339. }
  340. public bool IsThreadPoolThread {
  341. get {
  342. return IsThreadPoolThreadInternal;
  343. }
  344. }
  345. internal bool IsThreadPoolThreadInternal {
  346. get {
  347. return Internal.threadpool_thread;
  348. }
  349. set {
  350. Internal.threadpool_thread = value;
  351. }
  352. }
  353. public bool IsAlive {
  354. get {
  355. ThreadState curstate = GetState (Internal);
  356. if((curstate & ThreadState.Aborted) != 0 ||
  357. (curstate & ThreadState.Stopped) != 0 ||
  358. (curstate & ThreadState.Unstarted) != 0) {
  359. return(false);
  360. } else {
  361. return(true);
  362. }
  363. }
  364. }
  365. public bool IsBackground {
  366. get {
  367. ThreadState thread_state = GetState (Internal);
  368. if ((thread_state & ThreadState.Stopped) != 0)
  369. throw new ThreadStateException ("Thread is dead; state can not be accessed.");
  370. return (thread_state & ThreadState.Background) != 0;
  371. }
  372. set {
  373. if (value) {
  374. SetState (Internal, ThreadState.Background);
  375. } else {
  376. ClrState (Internal, ThreadState.Background);
  377. }
  378. }
  379. }
  380. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  381. private extern static string GetName_internal (InternalThread thread);
  382. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  383. private extern static void SetName_internal (InternalThread thread, String name);
  384. /*
  385. * The thread name must be shared by appdomains, so it is stored in
  386. * unmanaged code.
  387. */
  388. public string Name {
  389. get {
  390. return GetName_internal (Internal);
  391. }
  392. set {
  393. SetName_internal (Internal, value);
  394. }
  395. }
  396. public ThreadState ThreadState {
  397. get {
  398. return GetState (Internal);
  399. }
  400. }
  401. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  402. private extern static void Abort_internal (InternalThread thread, object stateInfo);
  403. [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
  404. public void Abort ()
  405. {
  406. Abort_internal (Internal, null);
  407. }
  408. [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
  409. public void Abort (object stateInfo)
  410. {
  411. Abort_internal (Internal, stateInfo);
  412. }
  413. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  414. extern object GetAbortExceptionState ();
  415. internal object AbortReason {
  416. get {
  417. return GetAbortExceptionState ();
  418. }
  419. }
  420. void ClearAbortReason ()
  421. {
  422. }
  423. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  424. private extern static void SpinWait_nop ();
  425. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
  426. public static void SpinWait (int iterations)
  427. {
  428. if (iterations < 0)
  429. return;
  430. while (iterations-- > 0)
  431. {
  432. SpinWait_nop ();
  433. }
  434. }
  435. void StartInternal (IPrincipal principal, ref StackCrawlMark stackMark)
  436. {
  437. Internal._serialized_principal = CurrentThread.Internal._serialized_principal;
  438. // Thread_internal creates and starts the new thread,
  439. if (Thread_internal(m_Delegate) == IntPtr.Zero)
  440. throw new SystemException ("Thread creation failed.");
  441. m_ThreadStartArg = null;
  442. }
  443. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  444. extern private static void SetState (InternalThread thread, ThreadState set);
  445. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  446. extern private static void ClrState (InternalThread thread, ThreadState clr);
  447. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  448. extern private static ThreadState GetState (InternalThread thread);
  449. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  450. extern public static byte VolatileRead (ref byte address);
  451. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  452. extern public static double VolatileRead (ref double address);
  453. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  454. extern public static short VolatileRead (ref short address);
  455. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  456. extern public static int VolatileRead (ref int address);
  457. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  458. extern public static long VolatileRead (ref long address);
  459. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  460. extern public static IntPtr VolatileRead (ref IntPtr address);
  461. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  462. extern public static object VolatileRead (ref object address);
  463. [CLSCompliant(false)]
  464. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  465. extern public static sbyte VolatileRead (ref sbyte address);
  466. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  467. extern public static float VolatileRead (ref float address);
  468. [CLSCompliant (false)]
  469. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  470. extern public static ushort VolatileRead (ref ushort address);
  471. [CLSCompliant (false)]
  472. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  473. extern public static uint VolatileRead (ref uint address);
  474. [CLSCompliant (false)]
  475. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  476. extern public static ulong VolatileRead (ref ulong address);
  477. [CLSCompliant (false)]
  478. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  479. extern public static UIntPtr VolatileRead (ref UIntPtr address);
  480. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  481. extern public static void VolatileWrite (ref byte address, byte value);
  482. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  483. extern public static void VolatileWrite (ref double address, double value);
  484. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  485. extern public static void VolatileWrite (ref short address, short value);
  486. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  487. extern public static void VolatileWrite (ref int address, int value);
  488. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  489. extern public static void VolatileWrite (ref long address, long value);
  490. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  491. extern public static void VolatileWrite (ref IntPtr address, IntPtr value);
  492. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  493. extern public static void VolatileWrite (ref object address, object value);
  494. [CLSCompliant(false)]
  495. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  496. extern public static void VolatileWrite (ref sbyte address, sbyte value);
  497. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  498. extern public static void VolatileWrite (ref float address, float value);
  499. [CLSCompliant (false)]
  500. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  501. extern public static void VolatileWrite (ref ushort address, ushort value);
  502. [CLSCompliant (false)]
  503. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  504. extern public static void VolatileWrite (ref uint address, uint value);
  505. [CLSCompliant (false)]
  506. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  507. extern public static void VolatileWrite (ref ulong address, ulong value);
  508. [CLSCompliant (false)]
  509. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  510. extern public static void VolatileWrite (ref UIntPtr address, UIntPtr value);
  511. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  512. extern static int SystemMaxStackStize ();
  513. static int GetProcessDefaultStackSize (int maxStackSize)
  514. {
  515. if (maxStackSize == 0)
  516. return 0;
  517. if (maxStackSize < 131072) // make sure stack is at least 128k big
  518. return 131072;
  519. int page_size = Environment.GetPageSize ();
  520. if ((maxStackSize % page_size) != 0) // round up to a divisible of page size
  521. maxStackSize = (maxStackSize / (page_size - 1)) * page_size;
  522. /* Respect the max stack size imposed by the system*/
  523. return Math.Min (maxStackSize, SystemMaxStackStize ());
  524. }
  525. void SetStart (MulticastDelegate start, int maxStackSize)
  526. {
  527. m_Delegate = start;
  528. Internal.stack_size = maxStackSize;
  529. }
  530. public int ManagedThreadId {
  531. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
  532. get {
  533. return Internal.managed_id;
  534. }
  535. }
  536. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
  537. public static void BeginCriticalRegion ()
  538. {
  539. CurrentThread.Internal.critical_region_level++;
  540. }
  541. [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
  542. public static void EndCriticalRegion ()
  543. {
  544. CurrentThread.Internal.critical_region_level--;
  545. }
  546. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  547. public static void BeginThreadAffinity ()
  548. {
  549. // Managed and native threads are currently bound together.
  550. }
  551. [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
  552. public static void EndThreadAffinity ()
  553. {
  554. // Managed and native threads are currently bound together.
  555. }
  556. public ApartmentState GetApartmentState ()
  557. {
  558. return (ApartmentState)Internal.apartment_state;
  559. }
  560. public void SetApartmentState (ApartmentState state)
  561. {
  562. if (!TrySetApartmentState (state))
  563. throw new InvalidOperationException ("Failed to set the specified COM apartment state.");
  564. }
  565. public bool TrySetApartmentState (ApartmentState state)
  566. {
  567. if ((ThreadState & ThreadState.Unstarted) == 0)
  568. throw new ThreadStateException ("Thread was in an invalid state for the operation being executed.");
  569. if ((ApartmentState)Internal.apartment_state != ApartmentState.Unknown &&
  570. (ApartmentState)Internal.apartment_state != state)
  571. return false;
  572. Internal.apartment_state = (byte)state;
  573. return true;
  574. }
  575. [ComVisible (false)]
  576. public override int GetHashCode ()
  577. {
  578. return ManagedThreadId;
  579. }
  580. internal CultureInfo GetCurrentUICultureNoAppX ()
  581. {
  582. return CultureInfo.CurrentUICulture;
  583. }
  584. }
  585. }