Thread.cs 9.8 KB

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