Thread.cs 8.9 KB

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