Thread.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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. #if NET_2_0
  42. using System.Runtime.ConstrainedExecution;
  43. using System.Runtime.InteropServices;
  44. #endif
  45. namespace System.Threading
  46. {
  47. public sealed class Thread
  48. {
  49. #region Sync with object.h
  50. // stores a thread handle
  51. private IntPtr system_thread_handle;
  52. private CultureInfo current_culture;
  53. private CultureInfo current_ui_culture;
  54. private bool threadpool_thread;
  55. /* accessed only from unmanaged code */
  56. private IntPtr name;
  57. private int name_len;
  58. private ThreadState state = ThreadState.Unstarted;
  59. private object abort_exc;
  60. internal object abort_state;
  61. /* thread_id is only accessed from unmanaged code */
  62. private int thread_id;
  63. /* start_notify is used by the runtime to signal that Start()
  64. * is ok to return
  65. */
  66. private IntPtr start_notify;
  67. private IntPtr stack_ptr;
  68. private IntPtr static_data;
  69. private IntPtr jit_data;
  70. private IntPtr lock_data;
  71. private IntPtr appdomain_refs;
  72. private bool interruption_requested;
  73. private IntPtr suspend_event;
  74. private IntPtr resume_event;
  75. private object synch_lock = new Object();
  76. private IntPtr serialized_culture_info;
  77. private int serialized_culture_info_len;
  78. private IntPtr serialized_ui_culture_info;
  79. private int serialized_ui_culture_info_len;
  80. #endregion
  81. private ThreadStart threadstart;
  82. private string thread_name=null;
  83. private IPrincipal _principal;
  84. public static Context CurrentContext {
  85. get {
  86. return(AppDomain.InternalGetContext ());
  87. }
  88. }
  89. public static IPrincipal CurrentPrincipal {
  90. get {
  91. IPrincipal p = null;
  92. Thread th = CurrentThread;
  93. lock (th) {
  94. p = th._principal;
  95. if (p == null) {
  96. p = GetDomain ().DefaultPrincipal;
  97. th._principal = p;
  98. }
  99. }
  100. return p;
  101. }
  102. [SecurityPermission (SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlPolicy)]
  103. set {
  104. CurrentThread._principal = value;
  105. }
  106. }
  107. // Looks up the object associated with the current thread
  108. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  109. private extern static Thread CurrentThread_internal();
  110. public static Thread CurrentThread {
  111. #if NET_2_0
  112. [ReliabilityContract (Consistency.WillNotCorruptState, CER.MayFail)]
  113. #endif
  114. get {
  115. return(CurrentThread_internal());
  116. }
  117. }
  118. internal static int CurrentThreadId {
  119. get {
  120. return CurrentThread.thread_id;
  121. }
  122. }
  123. // Looks up the slot hash for the current thread
  124. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  125. private extern static Hashtable SlotHash_lookup();
  126. // Stores the slot hash for the current thread
  127. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  128. private extern static void SlotHash_store(Hashtable slothash);
  129. private static Hashtable GetTLSSlotHash() {
  130. Hashtable slothash=SlotHash_lookup();
  131. if(slothash==null) {
  132. // Not synchronised, because this is
  133. // thread specific anyway.
  134. slothash=new Hashtable();
  135. SlotHash_store(slothash);
  136. }
  137. return(slothash);
  138. }
  139. internal static object ResetDataStoreStatus () {
  140. Hashtable slothash=SlotHash_lookup();
  141. SlotHash_store(null);
  142. return slothash;
  143. }
  144. internal static void RestoreDataStoreStatus (object data) {
  145. SlotHash_store((Hashtable)data);
  146. }
  147. public static LocalDataStoreSlot AllocateDataSlot() {
  148. LocalDataStoreSlot slot = new LocalDataStoreSlot();
  149. return(slot);
  150. }
  151. // Stores a hash keyed by strings of LocalDataStoreSlot objects
  152. static Hashtable datastorehash;
  153. private static object datastore_lock = new object ();
  154. private static void InitDataStoreHash () {
  155. lock (datastore_lock) {
  156. if (datastorehash == null) {
  157. datastorehash = Hashtable.Synchronized(new Hashtable());
  158. }
  159. }
  160. }
  161. public static LocalDataStoreSlot AllocateNamedDataSlot(string name) {
  162. lock (datastore_lock) {
  163. if (datastorehash == null)
  164. InitDataStoreHash ();
  165. LocalDataStoreSlot slot = (LocalDataStoreSlot)datastorehash[name];
  166. if(slot!=null) {
  167. // This exception isnt documented (of
  168. // course) but .net throws it
  169. throw new ArgumentException("Named data slot already added");
  170. }
  171. slot = new LocalDataStoreSlot();
  172. datastorehash.Add(name, slot);
  173. return(slot);
  174. }
  175. }
  176. public static void FreeNamedDataSlot(string name) {
  177. lock (datastore_lock) {
  178. if (datastorehash == null)
  179. InitDataStoreHash ();
  180. LocalDataStoreSlot slot=(LocalDataStoreSlot)datastorehash[name];
  181. if(slot!=null) {
  182. datastorehash.Remove(slot);
  183. }
  184. }
  185. }
  186. public static object GetData(LocalDataStoreSlot slot) {
  187. Hashtable slothash=GetTLSSlotHash();
  188. return(slothash[slot]);
  189. }
  190. public static AppDomain GetDomain() {
  191. return AppDomain.CurrentDomain;
  192. }
  193. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  194. public extern static int GetDomainID();
  195. public static LocalDataStoreSlot GetNamedDataSlot(string name) {
  196. lock (datastore_lock) {
  197. if (datastorehash == null)
  198. InitDataStoreHash ();
  199. LocalDataStoreSlot slot=(LocalDataStoreSlot)datastorehash[name];
  200. if(slot==null) {
  201. slot=AllocateNamedDataSlot(name);
  202. }
  203. return(slot);
  204. }
  205. }
  206. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  207. private extern static void ResetAbort_internal();
  208. public static void ResetAbort()
  209. {
  210. ResetAbort_internal();
  211. }
  212. public static void SetData(LocalDataStoreSlot slot,
  213. object data) {
  214. Hashtable slothash=GetTLSSlotHash();
  215. if(slothash.Contains(slot)) {
  216. slothash.Remove(slot);
  217. }
  218. slothash.Add(slot, data);
  219. }
  220. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  221. private extern static void Sleep_internal(int ms);
  222. public static void Sleep(int millisecondsTimeout) {
  223. if((millisecondsTimeout<0) && (millisecondsTimeout != Timeout.Infinite)) {
  224. throw new ArgumentException("Negative timeout");
  225. }
  226. Thread thread=CurrentThread;
  227. Sleep_internal(millisecondsTimeout);
  228. }
  229. public static void Sleep(TimeSpan timeout) {
  230. // LAMESPEC: says to throw ArgumentException too
  231. int ms=Convert.ToInt32(timeout.TotalMilliseconds);
  232. if(ms < 0 || ms > Int32.MaxValue) {
  233. throw new ArgumentOutOfRangeException("Timeout out of range");
  234. }
  235. Thread thread=CurrentThread;
  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. if (CurrentCulture.LCID == value.LCID)
  335. return;
  336. BinaryFormatter bf = new BinaryFormatter();
  337. MemoryStream ms = new MemoryStream ();
  338. bf.Serialize (ms, value);
  339. SetCachedCurrentCulture (value);
  340. SetSerializedCurrentCulture (ms.GetBuffer ());
  341. }
  342. finally {
  343. in_currentculture = false;
  344. }
  345. }
  346. }
  347. public CultureInfo CurrentUICulture {
  348. get {
  349. if (current_ui_culture == null) {
  350. lock (synch_lock) {
  351. if(current_ui_culture==null) {
  352. /* We don't
  353. * distinguish
  354. * between
  355. * System and
  356. * UI cultures
  357. */
  358. current_ui_culture = CultureInfo.ConstructCurrentUICulture ();
  359. }
  360. }
  361. }
  362. return(current_ui_culture);
  363. }
  364. set {
  365. current_ui_culture = value;
  366. }
  367. }
  368. public bool IsThreadPoolThread {
  369. get {
  370. return IsThreadPoolThreadInternal;
  371. }
  372. }
  373. internal bool IsThreadPoolThreadInternal {
  374. get {
  375. return threadpool_thread;
  376. }
  377. set {
  378. threadpool_thread = value;
  379. }
  380. }
  381. public bool IsAlive {
  382. get {
  383. ThreadState curstate=state;
  384. if((curstate & ThreadState.Aborted) != 0 ||
  385. (curstate & ThreadState.Stopped) != 0 ||
  386. (curstate & ThreadState.Unstarted) != 0) {
  387. return(false);
  388. } else {
  389. return(true);
  390. }
  391. }
  392. }
  393. public bool IsBackground {
  394. get {
  395. if((state & ThreadState.Background) != 0) {
  396. return(true);
  397. } else {
  398. return(false);
  399. }
  400. }
  401. set {
  402. if(value==true) {
  403. set_state(ThreadState.Background);
  404. } else {
  405. clr_state(ThreadState.Background);
  406. }
  407. }
  408. }
  409. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  410. private extern string GetName_internal ();
  411. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  412. private extern void SetName_internal (String name);
  413. /*
  414. * The thread name must be shared by appdomains, so it is stored in
  415. * unmanaged code.
  416. */
  417. public string Name {
  418. get {
  419. return GetName_internal ();
  420. }
  421. set {
  422. lock (synch_lock) {
  423. if(Name!=null) {
  424. throw new InvalidOperationException ("Thread.Name can only be set once.");
  425. }
  426. SetName_internal (value);
  427. }
  428. }
  429. }
  430. [MonoTODO]
  431. public ThreadPriority Priority {
  432. get {
  433. return(ThreadPriority.Lowest);
  434. }
  435. set {
  436. }
  437. }
  438. public ThreadState ThreadState {
  439. get {
  440. return(state);
  441. }
  442. }
  443. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  444. private extern void Abort_internal (object stateInfo);
  445. public void Abort() {
  446. Abort_internal (null);
  447. }
  448. public void Abort(object stateInfo) {
  449. Abort_internal(stateInfo);
  450. }
  451. [MonoTODO]
  452. public void Interrupt() {
  453. }
  454. // The current thread joins with 'this'. Set ms to 0 to block
  455. // until this actually exits.
  456. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  457. private extern bool Join_internal(int ms, IntPtr handle);
  458. public void Join() {
  459. if((state & ThreadState.Unstarted) != 0) {
  460. throw new ThreadStateException("Thread has not been started");
  461. }
  462. Thread thread=CurrentThread;
  463. Join_internal(Timeout.Infinite, system_thread_handle);
  464. }
  465. public bool Join(int millisecondsTimeout) {
  466. if (millisecondsTimeout != Timeout.Infinite && millisecondsTimeout < 0)
  467. throw new ArgumentException ("Timeout less than zero", "millisecondsTimeout");
  468. if((state & ThreadState.Unstarted) != 0) {
  469. throw new ThreadStateException("Thread has not been started");
  470. }
  471. Thread thread=CurrentThread;
  472. return Join_internal(millisecondsTimeout, system_thread_handle);
  473. }
  474. public bool Join(TimeSpan timeout) {
  475. // LAMESPEC: says to throw ArgumentException too
  476. int ms=Convert.ToInt32(timeout.TotalMilliseconds);
  477. if(ms < 0 || ms > Int32.MaxValue) {
  478. throw new ArgumentOutOfRangeException("timeout out of range");
  479. }
  480. if((state & ThreadState.Unstarted) != 0) {
  481. throw new ThreadStateException("Thread has not been started");
  482. }
  483. Thread thread=CurrentThread;
  484. return Join_internal(ms, system_thread_handle);
  485. }
  486. #if NET_1_1
  487. [MonoTODO ("seems required for multi-processors systems like Itanium")]
  488. public static void MemoryBarrier ()
  489. {
  490. throw new NotImplementedException ();
  491. }
  492. #endif
  493. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  494. private extern void Resume_internal();
  495. #if NET_2_0
  496. [Obsolete ("")]
  497. #endif
  498. public void Resume ()
  499. {
  500. if ((state & ThreadState.Unstarted) != 0 || !IsAlive ||
  501. ((state & ThreadState.Suspended) == 0 && (state & ThreadState.SuspendRequested) == 0))
  502. {
  503. throw new ThreadStateException("Thread has not been started, or is dead");
  504. }
  505. Resume_internal ();
  506. }
  507. [MonoTODO]
  508. public static void SpinWait (int iterations)
  509. {
  510. throw new NotImplementedException ();
  511. }
  512. // Launches the thread
  513. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  514. private extern void Start_internal(IntPtr handle);
  515. public void Start() {
  516. lock(synch_lock) {
  517. if((state & ThreadState.Unstarted) == 0) {
  518. throw new ThreadStateException("Thread has already been started");
  519. }
  520. // Thread_internal creates the new thread, but
  521. // blocks it until Start() is called later.
  522. system_thread_handle=Thread_internal(threadstart);
  523. if (system_thread_handle == (IntPtr) 0) {
  524. throw new SystemException ("Thread creation failed");
  525. }
  526. // Launch this thread
  527. Start_internal(system_thread_handle);
  528. // Mark the thread state as Running
  529. // (which is all bits
  530. // cleared). Therefore just remove the
  531. // Unstarted bit
  532. clr_state(ThreadState.Unstarted);
  533. }
  534. }
  535. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  536. private extern void Suspend_internal();
  537. #if NET_2_0
  538. [Obsolete ("")]
  539. #endif
  540. public void Suspend() {
  541. if((state & ThreadState.Unstarted) != 0 || !IsAlive) {
  542. throw new ThreadStateException("Thread has not been started, or is dead");
  543. }
  544. Suspend_internal ();
  545. }
  546. // Closes the system thread handle
  547. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  548. private extern void Thread_free_internal(IntPtr handle);
  549. ~Thread() {
  550. // Free up the handle
  551. if (system_thread_handle != (IntPtr) 0)
  552. Thread_free_internal(system_thread_handle);
  553. }
  554. private void set_state(ThreadState set) {
  555. lock(synch_lock) {
  556. state |= set;
  557. }
  558. }
  559. private void clr_state(ThreadState clr) {
  560. lock(synch_lock) {
  561. state &= ~clr;
  562. }
  563. }
  564. #if NET_1_1
  565. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  566. extern public static byte VolatileRead (ref byte address);
  567. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  568. extern public static double VolatileRead (ref double address);
  569. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  570. extern public static short VolatileRead (ref short address);
  571. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  572. extern public static int VolatileRead (ref int address);
  573. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  574. extern public static long VolatileRead (ref long address);
  575. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  576. extern public static IntPtr VolatileRead (ref IntPtr address);
  577. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  578. extern public static object VolatileRead (ref object address);
  579. [CLSCompliant(false)]
  580. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  581. extern public static sbyte VolatileRead (ref sbyte address);
  582. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  583. extern public static float VolatileRead (ref float address);
  584. [CLSCompliant (false)]
  585. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  586. extern public static ushort VolatileRead (ref ushort address);
  587. [CLSCompliant (false)]
  588. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  589. extern public static uint VolatileRead (ref uint address);
  590. [CLSCompliant (false)]
  591. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  592. extern public static ulong VolatileRead (ref ulong address);
  593. [CLSCompliant (false)]
  594. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  595. extern public static UIntPtr VolatileRead (ref UIntPtr address);
  596. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  597. extern public static void VolatileWrite (ref byte address, byte value);
  598. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  599. extern public static void VolatileWrite (ref double address, double value);
  600. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  601. extern public static void VolatileWrite (ref short address, short value);
  602. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  603. extern public static void VolatileWrite (ref int address, int value);
  604. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  605. extern public static void VolatileWrite (ref long address, long value);
  606. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  607. extern public static void VolatileWrite (ref IntPtr address, IntPtr value);
  608. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  609. extern public static void VolatileWrite (ref object address, object value);
  610. [CLSCompliant(false)]
  611. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  612. extern public static void VolatileWrite (ref sbyte address, sbyte value);
  613. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  614. extern public static void VolatileWrite (ref float address, float value);
  615. [CLSCompliant (false)]
  616. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  617. extern public static void VolatileWrite (ref ushort address, ushort value);
  618. [CLSCompliant (false)]
  619. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  620. extern public static void VolatileWrite (ref uint address, uint value);
  621. [CLSCompliant (false)]
  622. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  623. extern public static void VolatileWrite (ref ulong address, ulong value);
  624. [CLSCompliant (false)]
  625. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  626. extern public static void VolatileWrite (ref UIntPtr address, UIntPtr value);
  627. #endif
  628. #if NET_2_0
  629. [MonoTODO ("stack size is ignored")]
  630. public Thread (ThreadStart start, int maxStackSize)
  631. {
  632. if (start == null)
  633. throw new ArgumentNullException ("start");
  634. if (maxStackSize < 131072)
  635. throw new ArgumentException ("< 128 kb", "maxStackSize");
  636. threadstart=start;
  637. }
  638. [MonoTODO]
  639. public Thread (ParameterizedThreadStart start)
  640. {
  641. if (start == null)
  642. throw new ArgumentNullException ("start");
  643. throw new NotImplementedException ();
  644. }
  645. [MonoTODO]
  646. public Thread (ParameterizedThreadStart start, int maxStackSize)
  647. {
  648. if (start == null)
  649. throw new ArgumentNullException ("start");
  650. if (maxStackSize < 131072)
  651. throw new ArgumentException ("< 128 kb", "maxStackSize");
  652. throw new NotImplementedException ();
  653. }
  654. [MonoTODO]
  655. public ExecutionContext ExecutionContext {
  656. [ReliabilityContract (Consistency.WillNotCorruptState, CER.MayFail)]
  657. get { throw new NotImplementedException (); }
  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. [MonoTODO]
  709. public CompressedStack GetCompressedStack ()
  710. {
  711. throw new NotImplementedException ();
  712. }
  713. [MonoTODO]
  714. public void SetCompressedStack (CompressedStack stack)
  715. {
  716. throw new NotImplementedException ();
  717. }
  718. [ComVisible (false)]
  719. public override int GetHashCode ()
  720. {
  721. // ??? overridden but not guaranteed to be unique ???
  722. return thread_id;
  723. }
  724. [MonoTODO]
  725. public void Start (object parameter)
  726. {
  727. throw new NotImplementedException ();
  728. }
  729. #endif
  730. }
  731. }