Thread.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. //
  2. // System.Threading.Thread.cs
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. //
  7. // (C) Ximian, Inc. http://www.ximian.com
  8. //
  9. using System.Runtime.Remoting.Contexts;
  10. using System.Security.Principal;
  11. using System.Globalization;
  12. using System.Runtime.CompilerServices;
  13. using System.Collections;
  14. namespace System.Threading
  15. {
  16. public sealed class Thread
  17. {
  18. // stores a thread handle
  19. private IntPtr system_thread_handle;
  20. private CultureInfo current_culture;
  21. private bool threadpool_thread = false;
  22. private ThreadState state = ThreadState.Unstarted;
  23. private object abort_exc;
  24. internal object abort_state;
  25. /* thread_id is only accessed from unmanaged code */
  26. private int thread_id;
  27. /* start_notify is used by the runtime to signal that Start()
  28. * is ok to return
  29. */
  30. private IntPtr start_notify;
  31. private IntPtr stack_ptr;
  32. private IntPtr static_data;
  33. private IntPtr jit_data;
  34. private IntPtr lock_data;
  35. private ThreadStart threadstart;
  36. private string thread_name=null;
  37. public static Context CurrentContext {
  38. get {
  39. return(AppDomain.InternalGetContext ());
  40. }
  41. }
  42. [MonoTODO]
  43. public static IPrincipal CurrentPrincipal {
  44. get {
  45. // FIXME -
  46. // System.Security.Principal.IPrincipal
  47. // not yet implemented
  48. return(null);
  49. }
  50. set {
  51. }
  52. }
  53. // Looks up the object associated with the current thread
  54. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  55. private extern static Thread CurrentThread_internal();
  56. public static Thread CurrentThread {
  57. get {
  58. return(CurrentThread_internal());
  59. }
  60. }
  61. internal static int CurrentThreadId {
  62. get {
  63. return CurrentThread.thread_id;
  64. }
  65. }
  66. // Looks up the slot hash for the current thread
  67. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  68. private extern static Hashtable SlotHash_lookup();
  69. // Stores the slot hash for the current thread
  70. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  71. private extern static void SlotHash_store(Hashtable slothash);
  72. private static Hashtable GetTLSSlotHash() {
  73. Hashtable slothash=SlotHash_lookup();
  74. if(slothash==null) {
  75. // Not synchronised, because this is
  76. // thread specific anyway.
  77. slothash=new Hashtable();
  78. SlotHash_store(slothash);
  79. }
  80. return(slothash);
  81. }
  82. public static LocalDataStoreSlot AllocateDataSlot() {
  83. LocalDataStoreSlot slot = new LocalDataStoreSlot();
  84. return(slot);
  85. }
  86. // Stores a hash keyed by strings of LocalDataStoreSlot objects
  87. static Hashtable datastorehash;
  88. private static void InitDataStoreHash () {
  89. lock (typeof (Thread)) {
  90. if (datastorehash == null) {
  91. datastorehash = Hashtable.Synchronized(new Hashtable());
  92. }
  93. }
  94. }
  95. public static LocalDataStoreSlot AllocateNamedDataSlot(string name) {
  96. lock (typeof (Thread)) {
  97. if (datastorehash == null)
  98. InitDataStoreHash ();
  99. LocalDataStoreSlot slot = (LocalDataStoreSlot)datastorehash[name];
  100. if(slot!=null) {
  101. // This exception isnt documented (of
  102. // course) but .net throws it
  103. throw new ArgumentException("Named data slot already added");
  104. }
  105. slot = new LocalDataStoreSlot();
  106. datastorehash.Add(name, slot);
  107. return(slot);
  108. }
  109. }
  110. public static void FreeNamedDataSlot(string name) {
  111. lock (typeof (Thread)) {
  112. if (datastorehash == null)
  113. InitDataStoreHash ();
  114. LocalDataStoreSlot slot=(LocalDataStoreSlot)datastorehash[name];
  115. if(slot!=null) {
  116. datastorehash.Remove(slot);
  117. }
  118. }
  119. }
  120. public static object GetData(LocalDataStoreSlot slot) {
  121. Hashtable slothash=GetTLSSlotHash();
  122. return(slothash[slot]);
  123. }
  124. public static AppDomain GetDomain() {
  125. return AppDomain.CurrentDomain;
  126. }
  127. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  128. public extern static int GetDomainID();
  129. public static LocalDataStoreSlot GetNamedDataSlot(string name) {
  130. if (datastorehash == null)
  131. InitDataStoreHash ();
  132. LocalDataStoreSlot slot=(LocalDataStoreSlot)datastorehash[name];
  133. if(slot==null) {
  134. slot=AllocateNamedDataSlot(name);
  135. }
  136. return(slot);
  137. }
  138. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  139. private extern static void ResetAbort_internal();
  140. public static void ResetAbort()
  141. {
  142. Thread thread=CurrentThread;
  143. thread.clr_state(ThreadState.AbortRequested);
  144. ResetAbort_internal();
  145. }
  146. public static void SetData(LocalDataStoreSlot slot,
  147. object data) {
  148. Hashtable slothash=GetTLSSlotHash();
  149. if(slothash.Contains(slot)) {
  150. slothash.Remove(slot);
  151. }
  152. slothash.Add(slot, data);
  153. }
  154. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  155. private extern static void Sleep_internal(int ms);
  156. public static void Sleep(int millisecondsTimeout) {
  157. if((millisecondsTimeout<0) && (millisecondsTimeout != Timeout.Infinite)) {
  158. throw new ArgumentException("Negative timeout");
  159. }
  160. Thread thread=CurrentThread;
  161. thread.set_state(ThreadState.WaitSleepJoin);
  162. Sleep_internal(millisecondsTimeout);
  163. thread.clr_state(ThreadState.WaitSleepJoin);
  164. }
  165. public static void Sleep(TimeSpan timeout) {
  166. // LAMESPEC: says to throw ArgumentException too
  167. int ms=Convert.ToInt32(timeout.TotalMilliseconds);
  168. if(ms < 0 || ms > Int32.MaxValue) {
  169. throw new ArgumentOutOfRangeException("Timeout out of range");
  170. }
  171. Thread thread=CurrentThread;
  172. thread.set_state(ThreadState.WaitSleepJoin);
  173. Sleep_internal(ms);
  174. thread.clr_state(ThreadState.WaitSleepJoin);
  175. }
  176. // Returns the system thread handle
  177. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  178. private extern IntPtr Thread_internal(ThreadStart start);
  179. public Thread(ThreadStart start) {
  180. if(start==null) {
  181. throw new ArgumentNullException("Null ThreadStart");
  182. }
  183. threadstart=start;
  184. // This is a two-stage thread launch. Thread_internal
  185. // creates the new thread, but blocks it until
  186. // Start() is called later.
  187. system_thread_handle=Thread_internal(start);
  188. // Should throw an exception here if
  189. // Thread_internal returns NULL
  190. if(system_thread_handle==(IntPtr)0) {
  191. throw new SystemException("Thread creation failed");
  192. }
  193. }
  194. [MonoTODO]
  195. public ApartmentState ApartmentState {
  196. get {
  197. // FIXME
  198. return(ApartmentState.Unknown);
  199. }
  200. set {
  201. }
  202. }
  203. [MonoTODO]
  204. public CultureInfo CurrentCulture {
  205. get {
  206. if (current_culture == null)
  207. current_culture = CultureInfo.InvariantCulture;
  208. return current_culture;
  209. }
  210. set {
  211. current_culture = value;
  212. }
  213. }
  214. [MonoTODO]
  215. public CultureInfo CurrentUICulture {
  216. get {
  217. // FIXME
  218. return(CurrentCulture);
  219. }
  220. set {
  221. // FIXME
  222. CurrentCulture=value;
  223. }
  224. }
  225. public bool IsThreadPoolThread {
  226. get {
  227. return IsThreadPoolThreadInternal;
  228. }
  229. }
  230. internal bool IsThreadPoolThreadInternal {
  231. get {
  232. return threadpool_thread;
  233. }
  234. set {
  235. threadpool_thread = value;
  236. }
  237. }
  238. public bool IsAlive {
  239. get {
  240. ThreadState curstate=state;
  241. if((curstate & ThreadState.Aborted) != 0 ||
  242. (curstate & ThreadState.Stopped) != 0 ||
  243. (curstate & ThreadState.Unstarted) != 0) {
  244. return(false);
  245. } else {
  246. return(true);
  247. }
  248. }
  249. }
  250. public bool IsBackground {
  251. get {
  252. if((state & ThreadState.Background) != 0) {
  253. return(true);
  254. } else {
  255. return(false);
  256. }
  257. }
  258. set {
  259. if(value==true) {
  260. set_state(ThreadState.Background);
  261. } else {
  262. clr_state(ThreadState.Background);
  263. }
  264. }
  265. }
  266. public string Name {
  267. get {
  268. return(thread_name);
  269. }
  270. set {
  271. thread_name=value;
  272. }
  273. }
  274. [MonoTODO]
  275. public ThreadPriority Priority {
  276. get {
  277. // FIXME
  278. return(ThreadPriority.Lowest);
  279. }
  280. set {
  281. }
  282. }
  283. public ThreadState ThreadState {
  284. get {
  285. return(state);
  286. }
  287. }
  288. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  289. private extern void Abort_internal (object stateInfo);
  290. public void Abort() {
  291. set_state(ThreadState.AbortRequested);
  292. Abort_internal (null);
  293. }
  294. public void Abort(object stateInfo) {
  295. set_state(ThreadState.AbortRequested);
  296. Abort_internal(stateInfo);
  297. }
  298. [MonoTODO]
  299. public void Interrupt() {
  300. // FIXME
  301. }
  302. // The current thread joins with 'this'. Set ms to 0 to block
  303. // until this actually exits.
  304. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  305. private extern bool Join_internal(int ms, IntPtr handle);
  306. public void Join() {
  307. if((state & ThreadState.Unstarted) != 0) {
  308. throw new ThreadStateException("Thread has not been started");
  309. }
  310. Thread thread=CurrentThread;
  311. thread.set_state(ThreadState.WaitSleepJoin);
  312. Join_internal(Timeout.Infinite, system_thread_handle);
  313. thread.clr_state(ThreadState.WaitSleepJoin);
  314. }
  315. public bool Join(int millisecondsTimeout) {
  316. if (millisecondsTimeout != Timeout.Infinite && millisecondsTimeout < 0)
  317. throw new ArgumentException ("Timeout less than zero", "millisecondsTimeout");
  318. if((state & ThreadState.Unstarted) != 0) {
  319. throw new ThreadStateException("Thread has not been started");
  320. }
  321. Thread thread=CurrentThread;
  322. thread.set_state(ThreadState.WaitSleepJoin);
  323. bool ret=Join_internal(millisecondsTimeout,
  324. system_thread_handle);
  325. thread.clr_state(ThreadState.WaitSleepJoin);
  326. return(ret);
  327. }
  328. public bool Join(TimeSpan timeout) {
  329. // LAMESPEC: says to throw ArgumentException too
  330. int ms=Convert.ToInt32(timeout.TotalMilliseconds);
  331. if(ms < 0 || ms > Int32.MaxValue) {
  332. throw new ArgumentOutOfRangeException("timeout out of range");
  333. }
  334. if((state & ThreadState.Unstarted) != 0) {
  335. throw new ThreadStateException("Thread has not been started");
  336. }
  337. Thread thread=CurrentThread;
  338. thread.set_state(ThreadState.WaitSleepJoin);
  339. bool ret=Join_internal(ms, system_thread_handle);
  340. thread.clr_state(ThreadState.WaitSleepJoin);
  341. return(ret);
  342. }
  343. [MonoTODO]
  344. public void Resume() {
  345. throw new NotImplementedException ();
  346. }
  347. // Launches the thread
  348. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  349. private extern void Start_internal(IntPtr handle);
  350. public void Start() {
  351. lock(this) {
  352. if((state & ThreadState.Unstarted) == 0) {
  353. throw new ThreadStateException("Thread has already been started");
  354. }
  355. // Launch this thread
  356. Start_internal(system_thread_handle);
  357. // Mark the thread state as Running
  358. // (which is all bits
  359. // cleared). Therefore just remove the
  360. // Unstarted bit
  361. clr_state(ThreadState.Unstarted);
  362. }
  363. }
  364. [MonoTODO]
  365. public void Suspend() {
  366. if((state & ThreadState.Unstarted) != 0 || !IsAlive) {
  367. throw new ThreadStateException("Thread has not been started, or is dead");
  368. }
  369. set_state(ThreadState.SuspendRequested);
  370. // FIXME - somehow let the interpreter know that
  371. // this thread should now suspend
  372. Console.WriteLine ("WARNING: Thread.Suspend () partially implemented");
  373. }
  374. // Closes the system thread handle
  375. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  376. private extern void Thread_free_internal(IntPtr handle);
  377. ~Thread() {
  378. // Free up the handle
  379. Thread_free_internal(system_thread_handle);
  380. }
  381. private void set_state(ThreadState set) {
  382. lock(this) {
  383. state |= set;
  384. }
  385. }
  386. private void clr_state(ThreadState clr) {
  387. lock(this) {
  388. state &= ~clr;
  389. }
  390. }
  391. }
  392. }