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