Thread.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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-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.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.IO;
  37. using System.Collections;
  38. using System.Security;
  39. #if NET_2_0
  40. using System.Runtime.ConstrainedExecution;
  41. using System.Runtime.InteropServices;
  42. #endif
  43. namespace System.Threading
  44. {
  45. public sealed class Thread
  46. {
  47. #region Sync with metadata/object-internals.h
  48. int lock_thread_id;
  49. // stores a thread handle
  50. private IntPtr system_thread_handle;
  51. private IntPtr culture_info;
  52. private IntPtr ui_culture_info;
  53. private bool threadpool_thread;
  54. /* accessed only from unmanaged code */
  55. private IntPtr name;
  56. private int name_len;
  57. private ThreadState state = ThreadState.Unstarted;
  58. private object abort_exc;
  59. internal object abort_state;
  60. /* thread_id is only accessed from unmanaged code */
  61. private int thread_id;
  62. /* start_notify is used by the runtime to signal that Start()
  63. * is ok to return
  64. */
  65. private IntPtr start_notify;
  66. private IntPtr stack_ptr;
  67. private IntPtr static_data;
  68. private IntPtr jit_data;
  69. private IntPtr lock_data;
  70. Context current_appcontext;
  71. int stack_size;
  72. object start_obj;
  73. private IntPtr appdomain_refs;
  74. private bool interruption_requested;
  75. private IntPtr suspend_event;
  76. private IntPtr suspended_event;
  77. private IntPtr resume_event;
  78. private object synch_lock = new Object();
  79. private IntPtr serialized_culture_info;
  80. private int serialized_culture_info_len;
  81. private IntPtr serialized_ui_culture_info;
  82. private int serialized_ui_culture_info_len;
  83. /*
  84. * These fields are used to avoid having to increment corlib versions
  85. * when a new field is added to the unmanaged MonoThread structure.
  86. */
  87. private IntPtr unused1;
  88. private IntPtr unused2;
  89. private IntPtr unused3;
  90. private IntPtr unused4;
  91. private IntPtr unused5;
  92. private IntPtr unused6;
  93. private IntPtr unused7;
  94. private IntPtr unused8;
  95. #endregion
  96. [ThreadStatic]
  97. static Hashtable slothash;
  98. // can be both a ThreadSart and a ParameterizedThreadStart
  99. private MulticastDelegate threadstart;
  100. private string thread_name=null;
  101. private IPrincipal _principal;
  102. private ExecutionContext _ec;
  103. public static Context CurrentContext {
  104. [SecurityPermission (SecurityAction.LinkDemand, Infrastructure=true)]
  105. get {
  106. return(AppDomain.InternalGetContext ());
  107. }
  108. }
  109. public static IPrincipal CurrentPrincipal {
  110. get {
  111. IPrincipal p = null;
  112. Thread th = CurrentThread;
  113. lock (th) {
  114. p = th._principal;
  115. if (p == null) {
  116. p = GetDomain ().DefaultPrincipal;
  117. th._principal = p;
  118. }
  119. }
  120. return p;
  121. }
  122. [SecurityPermission (SecurityAction.Demand, ControlPrincipal = true)]
  123. set {
  124. CurrentThread._principal = value;
  125. }
  126. }
  127. // Looks up the object associated with the current thread
  128. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  129. private extern static Thread CurrentThread_internal();
  130. public static Thread CurrentThread {
  131. #if NET_2_0
  132. [ReliabilityContract (Consistency.WillNotCorruptState, CER.MayFail)]
  133. #endif
  134. get {
  135. return(CurrentThread_internal());
  136. }
  137. }
  138. internal static int CurrentThreadId {
  139. get {
  140. return CurrentThread.thread_id;
  141. }
  142. }
  143. // Stores a hash keyed by strings of LocalDataStoreSlot objects
  144. static Hashtable datastorehash;
  145. private static object datastore_lock = new object ();
  146. private static void InitDataStoreHash () {
  147. lock (datastore_lock) {
  148. if (datastorehash == null) {
  149. datastorehash = Hashtable.Synchronized(new Hashtable());
  150. }
  151. }
  152. }
  153. public static LocalDataStoreSlot AllocateNamedDataSlot(string name) {
  154. lock (datastore_lock) {
  155. if (datastorehash == null)
  156. InitDataStoreHash ();
  157. LocalDataStoreSlot slot = (LocalDataStoreSlot)datastorehash[name];
  158. if(slot!=null) {
  159. // This exception isnt documented (of
  160. // course) but .net throws it
  161. throw new ArgumentException("Named data slot already added");
  162. }
  163. slot = new LocalDataStoreSlot();
  164. datastorehash.Add(name, slot);
  165. return(slot);
  166. }
  167. }
  168. public static void FreeNamedDataSlot(string name) {
  169. lock (datastore_lock) {
  170. if (datastorehash == null)
  171. InitDataStoreHash ();
  172. LocalDataStoreSlot slot=(LocalDataStoreSlot)datastorehash[name];
  173. if(slot!=null) {
  174. datastorehash.Remove(slot);
  175. }
  176. }
  177. }
  178. private static Hashtable SlotHash {
  179. get {
  180. if (slothash == null)
  181. slothash = new Hashtable ();
  182. return slothash;
  183. }
  184. }
  185. public static LocalDataStoreSlot AllocateDataSlot() {
  186. return new LocalDataStoreSlot();
  187. }
  188. public static object GetData(LocalDataStoreSlot slot) {
  189. return SlotHash [slot];
  190. }
  191. public static void SetData(LocalDataStoreSlot slot,
  192. object data) {
  193. SlotHash [slot] = data;
  194. }
  195. public static AppDomain GetDomain() {
  196. return AppDomain.CurrentDomain;
  197. }
  198. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  199. public extern static int GetDomainID();
  200. public static LocalDataStoreSlot GetNamedDataSlot(string name) {
  201. lock (datastore_lock) {
  202. if (datastorehash == null)
  203. InitDataStoreHash ();
  204. LocalDataStoreSlot slot=(LocalDataStoreSlot)datastorehash[name];
  205. if(slot==null) {
  206. slot=AllocateNamedDataSlot(name);
  207. }
  208. return(slot);
  209. }
  210. }
  211. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  212. private extern static void ResetAbort_internal();
  213. [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
  214. public static void ResetAbort ()
  215. {
  216. ResetAbort_internal ();
  217. }
  218. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  219. private extern static void Sleep_internal(int ms);
  220. public static void Sleep(int millisecondsTimeout) {
  221. if((millisecondsTimeout<0) && (millisecondsTimeout != Timeout.Infinite)) {
  222. throw new ArgumentException("Negative timeout");
  223. }
  224. Sleep_internal(millisecondsTimeout);
  225. }
  226. public static void Sleep(TimeSpan timeout) {
  227. // LAMESPEC: says to throw ArgumentException too
  228. int ms=Convert.ToInt32(timeout.TotalMilliseconds);
  229. if(ms < 0 || ms > Int32.MaxValue) {
  230. throw new ArgumentOutOfRangeException("Timeout out of range");
  231. }
  232. Sleep_internal(ms);
  233. }
  234. // Returns the system thread handle
  235. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  236. private extern IntPtr Thread_internal (MulticastDelegate start);
  237. public Thread(ThreadStart start) {
  238. if(start==null) {
  239. throw new ArgumentNullException("Null ThreadStart");
  240. }
  241. threadstart=start;
  242. }
  243. [MonoTODO]
  244. #if NET_2_0
  245. [Obsolete ("")]
  246. #endif
  247. public ApartmentState ApartmentState {
  248. get {
  249. return(ApartmentState.Unknown);
  250. }
  251. set {
  252. }
  253. }
  254. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  255. private static extern int current_lcid ();
  256. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  257. private extern CultureInfo GetCachedCurrentCulture ();
  258. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  259. private extern byte[] GetSerializedCurrentCulture ();
  260. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  261. private extern void SetCachedCurrentCulture (CultureInfo culture);
  262. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  263. private extern void SetSerializedCurrentCulture (byte[] culture);
  264. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  265. private extern CultureInfo GetCachedCurrentUICulture ();
  266. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  267. private extern byte[] GetSerializedCurrentUICulture ();
  268. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  269. private extern void SetCachedCurrentUICulture (CultureInfo culture);
  270. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  271. private extern void SetSerializedCurrentUICulture (byte[] culture);
  272. /* If the current_lcid() isn't known by CultureInfo,
  273. * it will throw an exception which may cause
  274. * String.Concat to try and recursively look up the
  275. * CurrentCulture, which will throw an exception, etc.
  276. * Use a boolean to short-circuit this scenario.
  277. */
  278. private static bool in_currentculture=false;
  279. /*
  280. * Thread objects are shared between appdomains, and CurrentCulture
  281. * should always return an object in the calling appdomain. See bug
  282. * http://bugzilla.ximian.com/show_bug.cgi?id=50049 for more info.
  283. * This is hard to implement correctly and efficiently, so the current
  284. * implementation is not perfect: changes made in one appdomain to the
  285. * state of the current cultureinfo object are not visible to other
  286. * appdomains.
  287. */
  288. public CultureInfo CurrentCulture {
  289. get {
  290. if (in_currentculture)
  291. /* Bail out */
  292. return CultureInfo.InvariantCulture;
  293. CultureInfo culture = GetCachedCurrentCulture ();
  294. if (culture != null)
  295. return culture;
  296. byte[] arr = GetSerializedCurrentCulture ();
  297. if (arr == null) {
  298. lock (typeof (Thread)) {
  299. in_currentculture=true;
  300. culture = CultureInfo.ConstructCurrentCulture ();
  301. //
  302. // Don't serialize the culture in this case to avoid
  303. // initializing the serialization infrastructure in the
  304. // common case when the culture is not set explicitly.
  305. //
  306. SetCachedCurrentCulture (culture);
  307. in_currentculture = false;
  308. return culture;
  309. }
  310. }
  311. /*
  312. * No cultureinfo object exists for this domain, so create one
  313. * by deserializing the serialized form.
  314. */
  315. in_currentculture = true;
  316. try {
  317. BinaryFormatter bf = new BinaryFormatter ();
  318. MemoryStream ms = new MemoryStream (arr);
  319. culture = (CultureInfo)bf.Deserialize (ms);
  320. SetCachedCurrentCulture (culture);
  321. }
  322. finally {
  323. in_currentculture = false;
  324. }
  325. return culture;
  326. }
  327. [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
  328. set {
  329. in_currentculture = true;
  330. if (value == null)
  331. throw new ArgumentNullException ("value");
  332. try {
  333. BinaryFormatter bf = new BinaryFormatter();
  334. MemoryStream ms = new MemoryStream ();
  335. bf.Serialize (ms, value);
  336. SetCachedCurrentCulture (value);
  337. SetSerializedCurrentCulture (ms.GetBuffer ());
  338. } finally {
  339. in_currentculture = false;
  340. }
  341. }
  342. }
  343. public CultureInfo CurrentUICulture {
  344. get {
  345. if (in_currentculture)
  346. /* Bail out */
  347. return CultureInfo.InvariantCulture;
  348. CultureInfo culture = GetCachedCurrentUICulture ();
  349. if (culture != null)
  350. return culture;
  351. byte[] arr = GetSerializedCurrentUICulture ();
  352. if (arr == null) {
  353. lock (typeof (Thread)) {
  354. in_currentculture=true;
  355. /* We don't
  356. * distinguish
  357. * between
  358. * System and
  359. * UI cultures
  360. */
  361. culture = CultureInfo.ConstructCurrentUICulture ();
  362. //
  363. // Don't serialize the culture in this case to avoid
  364. // initializing the serialization infrastructure in the
  365. // common case when the culture is not set explicitly.
  366. //
  367. SetCachedCurrentUICulture (culture);
  368. in_currentculture = false;
  369. return culture;
  370. }
  371. }
  372. /*
  373. * No cultureinfo object exists for this domain, so create one
  374. * by deserializing the serialized form.
  375. */
  376. in_currentculture = true;
  377. try {
  378. BinaryFormatter bf = new BinaryFormatter ();
  379. MemoryStream ms = new MemoryStream (arr);
  380. culture = (CultureInfo)bf.Deserialize (ms);
  381. SetCachedCurrentUICulture (culture);
  382. }
  383. finally {
  384. in_currentculture = false;
  385. }
  386. return culture;
  387. }
  388. set {
  389. in_currentculture = true;
  390. if (value == null)
  391. throw new ArgumentNullException ("value");
  392. try {
  393. BinaryFormatter bf = new BinaryFormatter();
  394. MemoryStream ms = new MemoryStream ();
  395. bf.Serialize (ms, value);
  396. SetCachedCurrentUICulture (value);
  397. SetSerializedCurrentUICulture (ms.GetBuffer ());
  398. } finally {
  399. in_currentculture = false;
  400. }
  401. }
  402. }
  403. public bool IsThreadPoolThread {
  404. get {
  405. return IsThreadPoolThreadInternal;
  406. }
  407. }
  408. internal bool IsThreadPoolThreadInternal {
  409. get {
  410. return threadpool_thread;
  411. }
  412. set {
  413. threadpool_thread = value;
  414. }
  415. }
  416. public bool IsAlive {
  417. get {
  418. ThreadState curstate=state;
  419. if((curstate & ThreadState.Aborted) != 0 ||
  420. (curstate & ThreadState.Stopped) != 0 ||
  421. (curstate & ThreadState.Unstarted) != 0) {
  422. return(false);
  423. } else {
  424. return(true);
  425. }
  426. }
  427. }
  428. public bool IsBackground {
  429. get {
  430. if((state & ThreadState.Background) != 0) {
  431. return(true);
  432. } else {
  433. return(false);
  434. }
  435. }
  436. set {
  437. if(value==true) {
  438. set_state(ThreadState.Background);
  439. } else {
  440. clr_state(ThreadState.Background);
  441. }
  442. }
  443. }
  444. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  445. private extern string GetName_internal ();
  446. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  447. private extern void SetName_internal (String name);
  448. /*
  449. * The thread name must be shared by appdomains, so it is stored in
  450. * unmanaged code.
  451. */
  452. public string Name {
  453. get {
  454. return GetName_internal ();
  455. }
  456. set {
  457. lock (synch_lock) {
  458. if(Name!=null) {
  459. throw new InvalidOperationException ("Thread.Name can only be set once.");
  460. }
  461. SetName_internal (value);
  462. }
  463. }
  464. }
  465. [MonoTODO]
  466. public ThreadPriority Priority {
  467. get {
  468. return(ThreadPriority.Lowest);
  469. }
  470. set {
  471. }
  472. }
  473. public ThreadState ThreadState {
  474. get {
  475. return(state);
  476. }
  477. }
  478. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  479. private extern void Abort_internal (object stateInfo);
  480. [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
  481. public void Abort ()
  482. {
  483. Abort_internal (null);
  484. }
  485. [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
  486. public void Abort (object stateInfo)
  487. {
  488. Abort_internal (stateInfo);
  489. }
  490. [MonoTODO]
  491. [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
  492. public void Interrupt ()
  493. {
  494. }
  495. // The current thread joins with 'this'. Set ms to 0 to block
  496. // until this actually exits.
  497. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  498. private extern bool Join_internal(int ms, IntPtr handle);
  499. public void Join() {
  500. if((state & ThreadState.Unstarted) != 0) {
  501. throw new ThreadStateException("Thread has not been started");
  502. }
  503. Join_internal(Timeout.Infinite, system_thread_handle);
  504. }
  505. public bool Join(int millisecondsTimeout) {
  506. if (millisecondsTimeout != Timeout.Infinite && millisecondsTimeout < 0)
  507. throw new ArgumentException ("Timeout less than zero", "millisecondsTimeout");
  508. if((state & ThreadState.Unstarted) != 0) {
  509. throw new ThreadStateException("Thread has not been started");
  510. }
  511. return Join_internal(millisecondsTimeout, system_thread_handle);
  512. }
  513. public bool Join(TimeSpan timeout) {
  514. // LAMESPEC: says to throw ArgumentException too
  515. int ms=Convert.ToInt32(timeout.TotalMilliseconds);
  516. if(ms < 0 || ms > Int32.MaxValue) {
  517. throw new ArgumentOutOfRangeException("timeout out of range");
  518. }
  519. if((state & ThreadState.Unstarted) != 0) {
  520. throw new ThreadStateException("Thread has not been started");
  521. }
  522. return Join_internal(ms, system_thread_handle);
  523. }
  524. #if NET_1_1
  525. [MonoTODO ("seems required for multi-processors systems like Itanium")]
  526. public static void MemoryBarrier ()
  527. {
  528. // Will be implemented when we support Itanium
  529. }
  530. #endif
  531. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  532. private extern void Resume_internal();
  533. #if NET_2_0
  534. [Obsolete ("")]
  535. #endif
  536. [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
  537. public void Resume ()
  538. {
  539. if ((state & ThreadState.Unstarted) != 0 || !IsAlive ||
  540. ((state & ThreadState.Suspended) == 0 && (state & ThreadState.SuspendRequested) == 0))
  541. {
  542. throw new ThreadStateException("Thread has not been started, or is dead");
  543. }
  544. Resume_internal ();
  545. }
  546. [MonoTODO]
  547. public static void SpinWait (int iterations)
  548. {
  549. throw new NotImplementedException ();
  550. }
  551. // Launches the thread
  552. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  553. private extern void Start_internal(IntPtr handle);
  554. public void Start() {
  555. lock(synch_lock) {
  556. if((state & ThreadState.Unstarted) == 0) {
  557. throw new ThreadStateException("Thread has already been started");
  558. }
  559. // Thread_internal creates the new thread, but
  560. // blocks it until Start() is called later.
  561. system_thread_handle=Thread_internal(threadstart);
  562. if (system_thread_handle == (IntPtr) 0) {
  563. throw new SystemException ("Thread creation failed");
  564. }
  565. // Mark the thread state as Running
  566. // (which is all bits
  567. // cleared). Therefore just remove the
  568. // Unstarted bit
  569. clr_state(ThreadState.Unstarted);
  570. // Launch this thread
  571. Start_internal(system_thread_handle);
  572. }
  573. }
  574. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  575. private extern void Suspend_internal();
  576. #if NET_2_0
  577. [Obsolete ("")]
  578. #endif
  579. [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
  580. public void Suspend ()
  581. {
  582. if((state & ThreadState.Unstarted) != 0 || !IsAlive) {
  583. throw new ThreadStateException("Thread has not been started, or is dead");
  584. }
  585. Suspend_internal ();
  586. }
  587. // Closes the system thread handle
  588. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  589. private extern void Thread_free_internal(IntPtr handle);
  590. ~Thread() {
  591. // Free up the handle
  592. if (system_thread_handle != (IntPtr) 0)
  593. Thread_free_internal(system_thread_handle);
  594. }
  595. private void set_state(ThreadState set) {
  596. lock(synch_lock) {
  597. state |= set;
  598. }
  599. }
  600. private void clr_state(ThreadState clr) {
  601. lock(synch_lock) {
  602. state &= ~clr;
  603. }
  604. }
  605. #if NET_1_1
  606. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  607. extern public static byte VolatileRead (ref byte address);
  608. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  609. extern public static double VolatileRead (ref double address);
  610. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  611. extern public static short VolatileRead (ref short address);
  612. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  613. extern public static int VolatileRead (ref int address);
  614. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  615. extern public static long VolatileRead (ref long address);
  616. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  617. extern public static IntPtr VolatileRead (ref IntPtr address);
  618. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  619. extern public static object VolatileRead (ref object address);
  620. [CLSCompliant(false)]
  621. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  622. extern public static sbyte VolatileRead (ref sbyte address);
  623. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  624. extern public static float VolatileRead (ref float address);
  625. [CLSCompliant (false)]
  626. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  627. extern public static ushort VolatileRead (ref ushort address);
  628. [CLSCompliant (false)]
  629. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  630. extern public static uint VolatileRead (ref uint address);
  631. [CLSCompliant (false)]
  632. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  633. extern public static ulong VolatileRead (ref ulong address);
  634. [CLSCompliant (false)]
  635. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  636. extern public static UIntPtr VolatileRead (ref UIntPtr address);
  637. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  638. extern public static void VolatileWrite (ref byte address, byte value);
  639. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  640. extern public static void VolatileWrite (ref double address, double value);
  641. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  642. extern public static void VolatileWrite (ref short address, short value);
  643. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  644. extern public static void VolatileWrite (ref int address, int value);
  645. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  646. extern public static void VolatileWrite (ref long address, long value);
  647. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  648. extern public static void VolatileWrite (ref IntPtr address, IntPtr value);
  649. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  650. extern public static void VolatileWrite (ref object address, object value);
  651. [CLSCompliant(false)]
  652. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  653. extern public static void VolatileWrite (ref sbyte address, sbyte value);
  654. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  655. extern public static void VolatileWrite (ref float address, float value);
  656. [CLSCompliant (false)]
  657. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  658. extern public static void VolatileWrite (ref ushort address, ushort value);
  659. [CLSCompliant (false)]
  660. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  661. extern public static void VolatileWrite (ref uint address, uint value);
  662. [CLSCompliant (false)]
  663. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  664. extern public static void VolatileWrite (ref ulong address, ulong value);
  665. [CLSCompliant (false)]
  666. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  667. extern public static void VolatileWrite (ref UIntPtr address, UIntPtr value);
  668. #endif
  669. #if NET_2_0
  670. public Thread (ThreadStart start, int maxStackSize)
  671. {
  672. if (start == null)
  673. throw new ArgumentNullException ("start");
  674. if (maxStackSize < 131072)
  675. throw new ArgumentException ("< 128 kb", "maxStackSize");
  676. threadstart = start;
  677. stack_size = maxStackSize;
  678. }
  679. public Thread (ParameterizedThreadStart start)
  680. {
  681. if (start == null)
  682. throw new ArgumentNullException ("start");
  683. threadstart = start;
  684. }
  685. public Thread (ParameterizedThreadStart start, int maxStackSize)
  686. {
  687. if (start == null)
  688. throw new ArgumentNullException ("start");
  689. if (maxStackSize < 131072)
  690. throw new ArgumentException ("< 128 kb", "maxStackSize");
  691. threadstart = start;
  692. stack_size = maxStackSize;
  693. }
  694. [MonoTODO ("limited to CompressedStack support")]
  695. public ExecutionContext ExecutionContext {
  696. [ReliabilityContract (Consistency.WillNotCorruptState, CER.MayFail)]
  697. get {
  698. if (_ec == null)
  699. _ec = new ExecutionContext ();
  700. return _ec;
  701. }
  702. }
  703. public int ManagedThreadId {
  704. get { return thread_id; }
  705. }
  706. [MonoTODO]
  707. [ReliabilityContract (Consistency.WillNotCorruptState, CER.MayFail)]
  708. public static void BeginCriticalRegion ()
  709. {
  710. throw new NotImplementedException ();
  711. }
  712. [MonoTODO]
  713. [ReliabilityContract (Consistency.WillNotCorruptState, CER.Success)]
  714. public static void EndCriticalRegion ()
  715. {
  716. throw new NotImplementedException ();
  717. }
  718. [MonoTODO]
  719. public static void BeginThreadAffinity ()
  720. {
  721. throw new NotImplementedException ();
  722. }
  723. [MonoTODO]
  724. public static void EndThreadAffinity ()
  725. {
  726. throw new NotImplementedException ();
  727. }
  728. [MonoTODO]
  729. public ApartmentState GetApartmentState ()
  730. {
  731. return this.ApartmentState;
  732. }
  733. [MonoTODO]
  734. public void SetApartmentState (ApartmentState state)
  735. {
  736. this.ApartmentState = state;
  737. }
  738. [MonoTODO]
  739. public bool TrySetApartmentState (ApartmentState state)
  740. {
  741. try {
  742. this.ApartmentState = state;
  743. return true;
  744. }
  745. catch (ArgumentException) {
  746. throw;
  747. }
  748. catch {
  749. return false;
  750. }
  751. }
  752. [ComVisible (false)]
  753. public override int GetHashCode ()
  754. {
  755. // ??? overridden but not guaranteed to be unique ???
  756. return thread_id;
  757. }
  758. public void Start (object parameter)
  759. {
  760. start_obj = parameter;
  761. Start ();
  762. }
  763. #else
  764. internal ExecutionContext ExecutionContext {
  765. get {
  766. if (_ec == null)
  767. _ec = new ExecutionContext ();
  768. return _ec;
  769. }
  770. }
  771. #endif
  772. // NOTE: This method doesn't show in the class library status page because
  773. // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
  774. // But it's there!
  775. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  776. [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
  777. #if NET_1_1
  778. public
  779. #else
  780. internal
  781. #endif
  782. CompressedStack GetCompressedStack ()
  783. {
  784. // Note: returns null if no CompressedStack has been set.
  785. // However CompressedStack.GetCompressedStack returns an
  786. // (empty?) CompressedStack instance.
  787. CompressedStack cs = ExecutionContext.SecurityContext.CompressedStack;
  788. return ((cs == null) || cs.IsEmpty ()) ? null : cs.CreateCopy ();
  789. }
  790. // NOTE: This method doesn't show in the class library status page because
  791. // it cannot be "found" with the StrongNameIdentityPermission for ECMA key.
  792. // But it's there!
  793. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  794. [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
  795. #if NET_1_1
  796. public
  797. #else
  798. internal
  799. #endif
  800. void SetCompressedStack (CompressedStack stack)
  801. {
  802. ExecutionContext.SecurityContext.CompressedStack = stack;
  803. }
  804. }
  805. }