Thread.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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 Novell (http://www.novell.com)
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.Runtime.Remoting.Contexts;
  33. using System.Runtime.Serialization;
  34. using System.Runtime.Serialization.Formatters.Binary;
  35. using System.Security.Permissions;
  36. using System.Security.Principal;
  37. using System.Globalization;
  38. using System.Runtime.CompilerServices;
  39. using System.IO;
  40. using System.Collections;
  41. using System.Security;
  42. #if NET_2_0
  43. using System.Runtime.ConstrainedExecution;
  44. using System.Runtime.InteropServices;
  45. #endif
  46. namespace System.Threading
  47. {
  48. public sealed class Thread
  49. {
  50. #region Sync with object.h
  51. // stores a thread handle
  52. private IntPtr system_thread_handle;
  53. private IntPtr culture_info;
  54. private IntPtr ui_culture_info;
  55. private bool threadpool_thread;
  56. /* accessed only from unmanaged code */
  57. private IntPtr name;
  58. private int name_len;
  59. private ThreadState state = ThreadState.Unstarted;
  60. private object abort_exc;
  61. internal object abort_state;
  62. /* thread_id is only accessed from unmanaged code */
  63. private int thread_id;
  64. /* start_notify is used by the runtime to signal that Start()
  65. * is ok to return
  66. */
  67. private IntPtr start_notify;
  68. private IntPtr stack_ptr;
  69. private IntPtr static_data;
  70. private IntPtr jit_data;
  71. private IntPtr lock_data;
  72. private IntPtr appdomain_refs;
  73. private bool interruption_requested;
  74. private IntPtr suspend_event;
  75. private IntPtr resume_event;
  76. private object synch_lock = new Object();
  77. private IntPtr serialized_culture_info;
  78. private int serialized_culture_info_len;
  79. private IntPtr serialized_ui_culture_info;
  80. private int serialized_ui_culture_info_len;
  81. #endregion
  82. private ThreadStart threadstart;
  83. private string thread_name=null;
  84. private IPrincipal _principal;
  85. private CompressedStack _stack;
  86. public static Context CurrentContext {
  87. get {
  88. return(AppDomain.InternalGetContext ());
  89. }
  90. }
  91. public static IPrincipal CurrentPrincipal {
  92. get {
  93. IPrincipal p = null;
  94. Thread th = CurrentThread;
  95. lock (th) {
  96. p = th._principal;
  97. if (p == null) {
  98. p = GetDomain ().DefaultPrincipal;
  99. th._principal = p;
  100. }
  101. }
  102. return p;
  103. }
  104. [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
  105. set {
  106. CurrentThread._principal = value;
  107. }
  108. }
  109. // Looks up the object associated with the current thread
  110. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  111. private extern static Thread CurrentThread_internal();
  112. public static Thread CurrentThread {
  113. #if NET_2_0
  114. [ReliabilityContract (Consistency.WillNotCorruptState, CER.MayFail)]
  115. #endif
  116. get {
  117. return(CurrentThread_internal());
  118. }
  119. }
  120. internal static int CurrentThreadId {
  121. get {
  122. return CurrentThread.thread_id;
  123. }
  124. }
  125. // Looks up the slot hash for the current thread
  126. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  127. private extern static Hashtable SlotHash_lookup();
  128. // Stores the slot hash for the current thread
  129. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  130. private extern static void SlotHash_store(Hashtable slothash);
  131. private static Hashtable GetTLSSlotHash() {
  132. Hashtable slothash=SlotHash_lookup();
  133. if(slothash==null) {
  134. // Not synchronised, because this is
  135. // thread specific anyway.
  136. slothash=new Hashtable();
  137. SlotHash_store(slothash);
  138. }
  139. return(slothash);
  140. }
  141. internal static object ResetDataStoreStatus () {
  142. Hashtable slothash=SlotHash_lookup();
  143. SlotHash_store(null);
  144. return slothash;
  145. }
  146. internal static void RestoreDataStoreStatus (object data) {
  147. SlotHash_store((Hashtable)data);
  148. }
  149. public static LocalDataStoreSlot AllocateDataSlot() {
  150. LocalDataStoreSlot slot = new LocalDataStoreSlot();
  151. return(slot);
  152. }
  153. // Stores a hash keyed by strings of LocalDataStoreSlot objects
  154. static Hashtable datastorehash;
  155. private static object datastore_lock = new object ();
  156. private static void InitDataStoreHash () {
  157. lock (datastore_lock) {
  158. if (datastorehash == null) {
  159. datastorehash = Hashtable.Synchronized(new Hashtable());
  160. }
  161. }
  162. }
  163. public static LocalDataStoreSlot AllocateNamedDataSlot(string name) {
  164. lock (datastore_lock) {
  165. if (datastorehash == null)
  166. InitDataStoreHash ();
  167. LocalDataStoreSlot slot = (LocalDataStoreSlot)datastorehash[name];
  168. if(slot!=null) {
  169. // This exception isnt documented (of
  170. // course) but .net throws it
  171. throw new ArgumentException("Named data slot already added");
  172. }
  173. slot = new LocalDataStoreSlot();
  174. datastorehash.Add(name, slot);
  175. return(slot);
  176. }
  177. }
  178. public static void FreeNamedDataSlot(string name) {
  179. lock (datastore_lock) {
  180. if (datastorehash == null)
  181. InitDataStoreHash ();
  182. LocalDataStoreSlot slot=(LocalDataStoreSlot)datastorehash[name];
  183. if(slot!=null) {
  184. datastorehash.Remove(slot);
  185. }
  186. }
  187. }
  188. public static object GetData(LocalDataStoreSlot slot) {
  189. Hashtable slothash=GetTLSSlotHash();
  190. return(slothash[slot]);
  191. }
  192. public static AppDomain GetDomain() {
  193. return AppDomain.CurrentDomain;
  194. }
  195. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  196. public extern static int GetDomainID();
  197. public static LocalDataStoreSlot GetNamedDataSlot(string name) {
  198. lock (datastore_lock) {
  199. if (datastorehash == null)
  200. InitDataStoreHash ();
  201. LocalDataStoreSlot slot=(LocalDataStoreSlot)datastorehash[name];
  202. if(slot==null) {
  203. slot=AllocateNamedDataSlot(name);
  204. }
  205. return(slot);
  206. }
  207. }
  208. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  209. private extern static void ResetAbort_internal();
  210. public static void ResetAbort()
  211. {
  212. ResetAbort_internal();
  213. }
  214. public static void SetData(LocalDataStoreSlot slot,
  215. object data) {
  216. Hashtable slothash=GetTLSSlotHash();
  217. if(slothash.Contains(slot)) {
  218. slothash.Remove(slot);
  219. }
  220. slothash.Add(slot, data);
  221. }
  222. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  223. private extern static void Sleep_internal(int ms);
  224. public static void Sleep(int millisecondsTimeout) {
  225. if((millisecondsTimeout<0) && (millisecondsTimeout != Timeout.Infinite)) {
  226. throw new ArgumentException("Negative timeout");
  227. }
  228. Sleep_internal(millisecondsTimeout);
  229. }
  230. public static void Sleep(TimeSpan timeout) {
  231. // LAMESPEC: says to throw ArgumentException too
  232. int ms=Convert.ToInt32(timeout.TotalMilliseconds);
  233. if(ms < 0 || ms > Int32.MaxValue) {
  234. throw new ArgumentOutOfRangeException("Timeout out of range");
  235. }
  236. Sleep_internal(ms);
  237. }
  238. // Returns the system thread handle
  239. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  240. private extern IntPtr Thread_internal(ThreadStart start);
  241. public Thread(ThreadStart start) {
  242. if(start==null) {
  243. throw new ArgumentNullException("Null ThreadStart");
  244. }
  245. threadstart=start;
  246. }
  247. [MonoTODO]
  248. #if NET_2_0
  249. [Obsolete ("")]
  250. #endif
  251. public ApartmentState ApartmentState {
  252. get {
  253. return(ApartmentState.Unknown);
  254. }
  255. set {
  256. }
  257. }
  258. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  259. private static extern int current_lcid ();
  260. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  261. private extern CultureInfo GetCachedCurrentCulture ();
  262. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  263. private extern byte[] GetSerializedCurrentCulture ();
  264. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  265. private extern void SetCachedCurrentCulture (CultureInfo culture);
  266. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  267. private extern void SetSerializedCurrentCulture (byte[] culture);
  268. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  269. private extern CultureInfo GetCachedCurrentUICulture ();
  270. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  271. private extern byte[] GetSerializedCurrentUICulture ();
  272. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  273. private extern void SetCachedCurrentUICulture (CultureInfo culture);
  274. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  275. private extern void SetSerializedCurrentUICulture (byte[] culture);
  276. /* If the current_lcid() isn't known by CultureInfo,
  277. * it will throw an exception which may cause
  278. * String.Concat to try and recursively look up the
  279. * CurrentCulture, which will throw an exception, etc.
  280. * Use a boolean to short-circuit this scenario.
  281. */
  282. private static bool in_currentculture=false;
  283. /*
  284. * Thread objects are shared between appdomains, and CurrentCulture
  285. * should always return an object in the calling appdomain. See bug
  286. * http://bugzilla.ximian.com/show_bug.cgi?id=50049 for more info.
  287. * This is hard to implement correctly and efficiently, so the current
  288. * implementation is not perfect: changes made in one appdomain to the
  289. * state of the current cultureinfo object are not visible to other
  290. * appdomains.
  291. */
  292. public CultureInfo CurrentCulture {
  293. get {
  294. if (in_currentculture)
  295. /* Bail out */
  296. return CultureInfo.InvariantCulture;
  297. CultureInfo culture = GetCachedCurrentCulture ();
  298. if (culture != null)
  299. return culture;
  300. byte[] arr = GetSerializedCurrentCulture ();
  301. if (arr == null) {
  302. lock (typeof (Thread)) {
  303. in_currentculture=true;
  304. culture = CultureInfo.ConstructCurrentCulture ();
  305. //
  306. // Don't serialize the culture in this case to avoid
  307. // initializing the serialization infrastructure in the
  308. // common case when the culture is not set explicitly.
  309. //
  310. SetCachedCurrentCulture (culture);
  311. in_currentculture = false;
  312. return culture;
  313. }
  314. }
  315. /*
  316. * No cultureinfo object exists for this domain, so create one
  317. * by deserializing the serialized form.
  318. */
  319. in_currentculture = true;
  320. try {
  321. BinaryFormatter bf = new BinaryFormatter ();
  322. MemoryStream ms = new MemoryStream (arr);
  323. culture = (CultureInfo)bf.Deserialize (ms);
  324. SetCachedCurrentCulture (culture);
  325. }
  326. finally {
  327. in_currentculture = false;
  328. }
  329. return culture;
  330. }
  331. set {
  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. }
  340. finally {
  341. in_currentculture = false;
  342. }
  343. }
  344. }
  345. public CultureInfo CurrentUICulture {
  346. get {
  347. if (in_currentculture)
  348. /* Bail out */
  349. return CultureInfo.InvariantCulture;
  350. CultureInfo culture = GetCachedCurrentUICulture ();
  351. if (culture != null)
  352. return culture;
  353. byte[] arr = GetSerializedCurrentUICulture ();
  354. if (arr == null) {
  355. lock (typeof (Thread)) {
  356. in_currentculture=true;
  357. /* We don't
  358. * distinguish
  359. * between
  360. * System and
  361. * UI cultures
  362. */
  363. culture = CultureInfo.ConstructCurrentUICulture ();
  364. //
  365. // Don't serialize the culture in this case to avoid
  366. // initializing the serialization infrastructure in the
  367. // common case when the culture is not set explicitly.
  368. //
  369. SetCachedCurrentUICulture (culture);
  370. in_currentculture = false;
  371. return culture;
  372. }
  373. }
  374. /*
  375. * No cultureinfo object exists for this domain, so create one
  376. * by deserializing the serialized form.
  377. */
  378. in_currentculture = true;
  379. try {
  380. BinaryFormatter bf = new BinaryFormatter ();
  381. MemoryStream ms = new MemoryStream (arr);
  382. culture = (CultureInfo)bf.Deserialize (ms);
  383. SetCachedCurrentUICulture (culture);
  384. }
  385. finally {
  386. in_currentculture = false;
  387. }
  388. return culture;
  389. }
  390. set {
  391. in_currentculture = true;
  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. }
  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=state;
  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. if((state & ThreadState.Background) != 0) {
  432. return(true);
  433. } else {
  434. return(false);
  435. }
  436. }
  437. set {
  438. if(value==true) {
  439. set_state(ThreadState.Background);
  440. } else {
  441. clr_state(ThreadState.Background);
  442. }
  443. }
  444. }
  445. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  446. private extern string GetName_internal ();
  447. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  448. private extern void SetName_internal (String name);
  449. /*
  450. * The thread name must be shared by appdomains, so it is stored in
  451. * unmanaged code.
  452. */
  453. public string Name {
  454. get {
  455. return GetName_internal ();
  456. }
  457. set {
  458. lock (synch_lock) {
  459. if(Name!=null) {
  460. throw new InvalidOperationException ("Thread.Name can only be set once.");
  461. }
  462. SetName_internal (value);
  463. }
  464. }
  465. }
  466. [MonoTODO]
  467. public ThreadPriority Priority {
  468. get {
  469. return(ThreadPriority.Lowest);
  470. }
  471. set {
  472. }
  473. }
  474. public ThreadState ThreadState {
  475. get {
  476. return(state);
  477. }
  478. }
  479. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  480. private extern void Abort_internal (object stateInfo);
  481. public void Abort() {
  482. Abort_internal (null);
  483. }
  484. public void Abort(object stateInfo) {
  485. Abort_internal(stateInfo);
  486. }
  487. [MonoTODO]
  488. public void Interrupt() {
  489. }
  490. // The current thread joins with 'this'. Set ms to 0 to block
  491. // until this actually exits.
  492. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  493. private extern bool Join_internal(int ms, IntPtr handle);
  494. public void Join() {
  495. if((state & ThreadState.Unstarted) != 0) {
  496. throw new ThreadStateException("Thread has not been started");
  497. }
  498. Join_internal(Timeout.Infinite, system_thread_handle);
  499. }
  500. public bool Join(int millisecondsTimeout) {
  501. if (millisecondsTimeout != Timeout.Infinite && millisecondsTimeout < 0)
  502. throw new ArgumentException ("Timeout less than zero", "millisecondsTimeout");
  503. if((state & ThreadState.Unstarted) != 0) {
  504. throw new ThreadStateException("Thread has not been started");
  505. }
  506. return Join_internal(millisecondsTimeout, system_thread_handle);
  507. }
  508. public bool Join(TimeSpan timeout) {
  509. // LAMESPEC: says to throw ArgumentException too
  510. int ms=Convert.ToInt32(timeout.TotalMilliseconds);
  511. if(ms < 0 || ms > Int32.MaxValue) {
  512. throw new ArgumentOutOfRangeException("timeout out of range");
  513. }
  514. if((state & ThreadState.Unstarted) != 0) {
  515. throw new ThreadStateException("Thread has not been started");
  516. }
  517. return Join_internal(ms, system_thread_handle);
  518. }
  519. #if NET_1_1
  520. [MonoTODO ("seems required for multi-processors systems like Itanium")]
  521. public static void MemoryBarrier ()
  522. {
  523. throw new NotImplementedException ();
  524. }
  525. #endif
  526. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  527. private extern void Resume_internal();
  528. #if NET_2_0
  529. [Obsolete ("")]
  530. #endif
  531. public void Resume ()
  532. {
  533. if ((state & ThreadState.Unstarted) != 0 || !IsAlive ||
  534. ((state & ThreadState.Suspended) == 0 && (state & ThreadState.SuspendRequested) == 0))
  535. {
  536. throw new ThreadStateException("Thread has not been started, or is dead");
  537. }
  538. Resume_internal ();
  539. }
  540. [MonoTODO]
  541. public static void SpinWait (int iterations)
  542. {
  543. throw new NotImplementedException ();
  544. }
  545. // Launches the thread
  546. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  547. private extern void Start_internal(IntPtr handle);
  548. public void Start() {
  549. lock(synch_lock) {
  550. if((state & ThreadState.Unstarted) == 0) {
  551. throw new ThreadStateException("Thread has already been started");
  552. }
  553. // Thread_internal creates the new thread, but
  554. // blocks it until Start() is called later.
  555. system_thread_handle=Thread_internal(threadstart);
  556. if (system_thread_handle == (IntPtr) 0) {
  557. throw new SystemException ("Thread creation failed");
  558. }
  559. // Launch this thread
  560. Start_internal(system_thread_handle);
  561. // Mark the thread state as Running
  562. // (which is all bits
  563. // cleared). Therefore just remove the
  564. // Unstarted bit
  565. clr_state(ThreadState.Unstarted);
  566. }
  567. }
  568. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  569. private extern void Suspend_internal();
  570. #if NET_2_0
  571. [Obsolete ("")]
  572. #endif
  573. public void Suspend() {
  574. if((state & ThreadState.Unstarted) != 0 || !IsAlive) {
  575. throw new ThreadStateException("Thread has not been started, or is dead");
  576. }
  577. Suspend_internal ();
  578. }
  579. // Closes the system thread handle
  580. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  581. private extern void Thread_free_internal(IntPtr handle);
  582. ~Thread() {
  583. // Free up the handle
  584. if (system_thread_handle != (IntPtr) 0)
  585. Thread_free_internal(system_thread_handle);
  586. }
  587. private void set_state(ThreadState set) {
  588. lock(synch_lock) {
  589. state |= set;
  590. }
  591. }
  592. private void clr_state(ThreadState clr) {
  593. lock(synch_lock) {
  594. state &= ~clr;
  595. }
  596. }
  597. #if NET_1_1
  598. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  599. extern public static byte VolatileRead (ref byte address);
  600. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  601. extern public static double VolatileRead (ref double address);
  602. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  603. extern public static short VolatileRead (ref short address);
  604. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  605. extern public static int VolatileRead (ref int address);
  606. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  607. extern public static long VolatileRead (ref long address);
  608. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  609. extern public static IntPtr VolatileRead (ref IntPtr address);
  610. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  611. extern public static object VolatileRead (ref object address);
  612. [CLSCompliant(false)]
  613. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  614. extern public static sbyte VolatileRead (ref sbyte address);
  615. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  616. extern public static float VolatileRead (ref float address);
  617. [CLSCompliant (false)]
  618. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  619. extern public static ushort VolatileRead (ref ushort address);
  620. [CLSCompliant (false)]
  621. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  622. extern public static uint VolatileRead (ref uint address);
  623. [CLSCompliant (false)]
  624. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  625. extern public static ulong VolatileRead (ref ulong address);
  626. [CLSCompliant (false)]
  627. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  628. extern public static UIntPtr VolatileRead (ref UIntPtr address);
  629. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  630. extern public static void VolatileWrite (ref byte address, byte value);
  631. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  632. extern public static void VolatileWrite (ref double address, double value);
  633. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  634. extern public static void VolatileWrite (ref short address, short value);
  635. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  636. extern public static void VolatileWrite (ref int address, int value);
  637. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  638. extern public static void VolatileWrite (ref long address, long value);
  639. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  640. extern public static void VolatileWrite (ref IntPtr address, IntPtr value);
  641. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  642. extern public static void VolatileWrite (ref object address, object value);
  643. [CLSCompliant(false)]
  644. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  645. extern public static void VolatileWrite (ref sbyte address, sbyte value);
  646. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  647. extern public static void VolatileWrite (ref float address, float value);
  648. [CLSCompliant (false)]
  649. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  650. extern public static void VolatileWrite (ref ushort address, ushort value);
  651. [CLSCompliant (false)]
  652. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  653. extern public static void VolatileWrite (ref uint address, uint value);
  654. [CLSCompliant (false)]
  655. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  656. extern public static void VolatileWrite (ref ulong address, ulong value);
  657. [CLSCompliant (false)]
  658. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  659. extern public static void VolatileWrite (ref UIntPtr address, UIntPtr value);
  660. #endif
  661. #if NET_2_0
  662. [MonoTODO ("stack size is ignored")]
  663. public Thread (ThreadStart start, int maxStackSize)
  664. {
  665. if (start == null)
  666. throw new ArgumentNullException ("start");
  667. if (maxStackSize < 131072)
  668. throw new ArgumentException ("< 128 kb", "maxStackSize");
  669. threadstart=start;
  670. }
  671. [MonoTODO]
  672. public Thread (ParameterizedThreadStart start)
  673. {
  674. if (start == null)
  675. throw new ArgumentNullException ("start");
  676. throw new NotImplementedException ();
  677. }
  678. [MonoTODO]
  679. public Thread (ParameterizedThreadStart start, int maxStackSize)
  680. {
  681. if (start == null)
  682. throw new ArgumentNullException ("start");
  683. if (maxStackSize < 131072)
  684. throw new ArgumentException ("< 128 kb", "maxStackSize");
  685. throw new NotImplementedException ();
  686. }
  687. [MonoTODO]
  688. public ExecutionContext ExecutionContext {
  689. [ReliabilityContract (Consistency.WillNotCorruptState, CER.MayFail)]
  690. get { throw new NotImplementedException (); }
  691. }
  692. public int ManagedThreadId {
  693. get { return thread_id; }
  694. }
  695. [MonoTODO]
  696. [ReliabilityContract (Consistency.WillNotCorruptState, CER.MayFail)]
  697. public static void BeginCriticalRegion ()
  698. {
  699. throw new NotImplementedException ();
  700. }
  701. [MonoTODO]
  702. [ReliabilityContract (Consistency.WillNotCorruptState, CER.Success)]
  703. public static void EndCriticalRegion ()
  704. {
  705. throw new NotImplementedException ();
  706. }
  707. [MonoTODO]
  708. public static void BeginThreadAffinity ()
  709. {
  710. throw new NotImplementedException ();
  711. }
  712. [MonoTODO]
  713. public static void EndThreadAffinity ()
  714. {
  715. throw new NotImplementedException ();
  716. }
  717. [MonoTODO]
  718. public ApartmentState GetApartmentState ()
  719. {
  720. return this.ApartmentState;
  721. }
  722. [MonoTODO]
  723. public void SetApartmentState (ApartmentState state)
  724. {
  725. this.ApartmentState = state;
  726. }
  727. [MonoTODO]
  728. public bool TrySetApartmentState (ApartmentState state)
  729. {
  730. try {
  731. this.ApartmentState = state;
  732. return true;
  733. }
  734. catch (ArgumentException) {
  735. throw;
  736. }
  737. catch {
  738. return false;
  739. }
  740. }
  741. public CompressedStack GetCompressedStack ()
  742. {
  743. // Note: returns null if no CompressedStack has been set.
  744. // However CompressedStack.GetCompressedStack returns an
  745. // (empty?) CompressedStack instance.
  746. return _stack;
  747. }
  748. public void SetCompressedStack (CompressedStack stack)
  749. {
  750. _stack = stack;
  751. }
  752. [ComVisible (false)]
  753. public override int GetHashCode ()
  754. {
  755. // ??? overridden but not guaranteed to be unique ???
  756. return thread_id;
  757. }
  758. [MonoTODO]
  759. public void Start (object parameter)
  760. {
  761. throw new NotImplementedException ();
  762. }
  763. #else
  764. internal CompressedStack GetCompressedStack ()
  765. {
  766. return _stack;
  767. }
  768. #endif
  769. }
  770. }