Thread.cs 11 KB

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