Thread.cs 25 KB

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