Thread.cs 9.2 KB

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