Thread.cs 10 KB

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