Thread.cs 8.6 KB

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