Thread.cs 9.8 KB

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