Thread.cs 27 KB

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