Thread.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. if (datastorehash == null)
  97. InitDataStoreHash ();
  98. LocalDataStoreSlot slot = (LocalDataStoreSlot)datastorehash[name];
  99. if(slot!=null) {
  100. // This exception isnt documented (of
  101. // course) but .net throws it
  102. throw new ArgumentException("Named data slot already added");
  103. }
  104. slot = new LocalDataStoreSlot();
  105. datastorehash.Add(name, slot);
  106. return(slot);
  107. }
  108. public static void FreeNamedDataSlot(string name) {
  109. if (datastorehash == null)
  110. InitDataStoreHash ();
  111. LocalDataStoreSlot slot=(LocalDataStoreSlot)datastorehash[name];
  112. if(slot!=null) {
  113. datastorehash.Remove(slot);
  114. }
  115. }
  116. public static object GetData(LocalDataStoreSlot slot) {
  117. Hashtable slothash=GetTLSSlotHash();
  118. return(slothash[slot]);
  119. }
  120. public static AppDomain GetDomain() {
  121. return AppDomain.CurrentDomain;
  122. }
  123. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  124. public extern static int GetDomainID();
  125. public static LocalDataStoreSlot GetNamedDataSlot(string name) {
  126. if (datastorehash == null)
  127. InitDataStoreHash ();
  128. LocalDataStoreSlot slot=(LocalDataStoreSlot)datastorehash[name];
  129. if(slot==null) {
  130. slot=AllocateNamedDataSlot(name);
  131. }
  132. return(slot);
  133. }
  134. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  135. private extern static void ResetAbort_internal();
  136. public static void ResetAbort()
  137. {
  138. Thread thread=CurrentThread;
  139. thread.clr_state(ThreadState.AbortRequested);
  140. ResetAbort_internal();
  141. }
  142. public static void SetData(LocalDataStoreSlot slot,
  143. object data) {
  144. Hashtable slothash=GetTLSSlotHash();
  145. if(slothash.Contains(slot)) {
  146. slothash.Remove(slot);
  147. }
  148. slothash.Add(slot, data);
  149. }
  150. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  151. private extern static void Sleep_internal(int ms);
  152. public static void Sleep(int millisecondsTimeout) {
  153. if((millisecondsTimeout<0) && (millisecondsTimeout != Timeout.Infinite)) {
  154. throw new ArgumentException("Negative timeout");
  155. }
  156. Thread thread=CurrentThread;
  157. thread.set_state(ThreadState.WaitSleepJoin);
  158. Sleep_internal(millisecondsTimeout);
  159. thread.clr_state(ThreadState.WaitSleepJoin);
  160. }
  161. public static void Sleep(TimeSpan timeout) {
  162. // LAMESPEC: says to throw ArgumentException too
  163. int ms=Convert.ToInt32(timeout.TotalMilliseconds);
  164. if(ms < 0 || ms > Int32.MaxValue) {
  165. throw new ArgumentOutOfRangeException("Timeout out of range");
  166. }
  167. Thread thread=CurrentThread;
  168. thread.set_state(ThreadState.WaitSleepJoin);
  169. Sleep_internal(ms);
  170. thread.clr_state(ThreadState.WaitSleepJoin);
  171. }
  172. // Returns the system thread handle
  173. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  174. private extern IntPtr Thread_internal(ThreadStart start);
  175. public Thread(ThreadStart start) {
  176. if(start==null) {
  177. throw new ArgumentNullException("Null ThreadStart");
  178. }
  179. threadstart=start;
  180. // This is a two-stage thread launch. Thread_internal
  181. // creates the new thread, but blocks it until
  182. // Start() is called later.
  183. system_thread_handle=Thread_internal(start);
  184. // Should throw an exception here if
  185. // Thread_internal returns NULL
  186. if(system_thread_handle==(IntPtr)0) {
  187. throw new SystemException("Thread creation failed");
  188. }
  189. }
  190. [MonoTODO]
  191. public ApartmentState ApartmentState {
  192. get {
  193. // FIXME
  194. return(ApartmentState.Unknown);
  195. }
  196. set {
  197. }
  198. }
  199. [MonoTODO]
  200. public CultureInfo CurrentCulture {
  201. get {
  202. if (current_culture == null)
  203. current_culture = CultureInfo.InvariantCulture;
  204. return current_culture;
  205. }
  206. set {
  207. current_culture = value;
  208. }
  209. }
  210. [MonoTODO]
  211. public CultureInfo CurrentUICulture {
  212. get {
  213. // FIXME
  214. return(CurrentCulture);
  215. }
  216. set {
  217. // FIXME
  218. CurrentCulture=value;
  219. }
  220. }
  221. public bool IsThreadPoolThread {
  222. get {
  223. return IsThreadPoolThreadInternal;
  224. }
  225. }
  226. internal bool IsThreadPoolThreadInternal {
  227. get {
  228. return threadpool_thread;
  229. }
  230. set {
  231. threadpool_thread = value;
  232. }
  233. }
  234. public bool IsAlive {
  235. get {
  236. ThreadState curstate=state;
  237. if((curstate & ThreadState.Aborted) != 0 ||
  238. (curstate & ThreadState.Stopped) != 0 ||
  239. (curstate & ThreadState.Unstarted) != 0) {
  240. return(false);
  241. } else {
  242. return(true);
  243. }
  244. }
  245. }
  246. public bool IsBackground {
  247. get {
  248. if((state & ThreadState.Background) != 0) {
  249. return(true);
  250. } else {
  251. return(false);
  252. }
  253. }
  254. set {
  255. if(value==true) {
  256. set_state(ThreadState.Background);
  257. } else {
  258. clr_state(ThreadState.Background);
  259. }
  260. }
  261. }
  262. public string Name {
  263. get {
  264. return(thread_name);
  265. }
  266. set {
  267. thread_name=value;
  268. }
  269. }
  270. [MonoTODO]
  271. public ThreadPriority Priority {
  272. get {
  273. // FIXME
  274. return(ThreadPriority.Lowest);
  275. }
  276. set {
  277. }
  278. }
  279. public ThreadState ThreadState {
  280. get {
  281. return(state);
  282. }
  283. }
  284. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  285. private extern void Abort_internal (object stateInfo);
  286. public void Abort() {
  287. set_state(ThreadState.AbortRequested);
  288. Abort_internal (null);
  289. }
  290. public void Abort(object stateInfo) {
  291. set_state(ThreadState.AbortRequested);
  292. Abort_internal(stateInfo);
  293. }
  294. [MonoTODO]
  295. public void Interrupt() {
  296. // FIXME
  297. }
  298. // The current thread joins with 'this'. Set ms to 0 to block
  299. // until this actually exits.
  300. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  301. private extern bool Join_internal(int ms, IntPtr handle);
  302. public void Join() {
  303. if((state & ThreadState.Unstarted) != 0) {
  304. throw new ThreadStateException("Thread has not been started");
  305. }
  306. Thread thread=CurrentThread;
  307. thread.set_state(ThreadState.WaitSleepJoin);
  308. Join_internal(Timeout.Infinite, system_thread_handle);
  309. thread.clr_state(ThreadState.WaitSleepJoin);
  310. }
  311. public bool Join(int millisecondsTimeout) {
  312. if (millisecondsTimeout != Timeout.Infinite && millisecondsTimeout < 0)
  313. throw new ArgumentException ("Timeout less than zero", "millisecondsTimeout");
  314. if((state & ThreadState.Unstarted) != 0) {
  315. throw new ThreadStateException("Thread has not been started");
  316. }
  317. Thread thread=CurrentThread;
  318. thread.set_state(ThreadState.WaitSleepJoin);
  319. bool ret=Join_internal(millisecondsTimeout,
  320. system_thread_handle);
  321. thread.clr_state(ThreadState.WaitSleepJoin);
  322. return(ret);
  323. }
  324. public bool Join(TimeSpan timeout) {
  325. // LAMESPEC: says to throw ArgumentException too
  326. int ms=Convert.ToInt32(timeout.TotalMilliseconds);
  327. if(ms < 0 || ms > Int32.MaxValue) {
  328. throw new ArgumentOutOfRangeException("timeout out of range");
  329. }
  330. if((state & ThreadState.Unstarted) != 0) {
  331. throw new ThreadStateException("Thread has not been started");
  332. }
  333. Thread thread=CurrentThread;
  334. thread.set_state(ThreadState.WaitSleepJoin);
  335. bool ret=Join_internal(ms, system_thread_handle);
  336. thread.clr_state(ThreadState.WaitSleepJoin);
  337. return(ret);
  338. }
  339. [MonoTODO]
  340. public void Resume() {
  341. throw new NotImplementedException ();
  342. }
  343. // Launches the thread
  344. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  345. private extern void Start_internal(IntPtr handle);
  346. public void Start() {
  347. lock(this) {
  348. if((state & ThreadState.Unstarted) == 0) {
  349. throw new ThreadStateException("Thread has already been started");
  350. }
  351. // Launch this thread
  352. Start_internal(system_thread_handle);
  353. // Mark the thread state as Running
  354. // (which is all bits
  355. // cleared). Therefore just remove the
  356. // Unstarted bit
  357. clr_state(ThreadState.Unstarted);
  358. }
  359. }
  360. [MonoTODO]
  361. public void Suspend() {
  362. if((state & ThreadState.Unstarted) != 0 || !IsAlive) {
  363. throw new ThreadStateException("Thread has not been started, or is dead");
  364. }
  365. set_state(ThreadState.SuspendRequested);
  366. // FIXME - somehow let the interpreter know that
  367. // this thread should now suspend
  368. Console.WriteLine ("WARNING: Thread.Suspend () partially implemented");
  369. }
  370. // Closes the system thread handle
  371. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  372. private extern void Thread_free_internal(IntPtr handle);
  373. ~Thread() {
  374. // Free up the handle
  375. Thread_free_internal(system_thread_handle);
  376. }
  377. private void set_state(ThreadState set) {
  378. lock(this) {
  379. state |= set;
  380. }
  381. }
  382. private void clr_state(ThreadState clr) {
  383. lock(this) {
  384. state &= ~clr;
  385. }
  386. }
  387. }
  388. }