Thread.cs 8.8 KB

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