Thread.cs 10 KB

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