Thread.cs 11 KB

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