Thread.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. namespace System.Threading
  14. {
  15. public sealed class Thread
  16. {
  17. public static Context CurrentContext {
  18. get {
  19. // FIXME -
  20. // System.Runtime.Remoting.Context not
  21. // yet implemented
  22. return(null);
  23. }
  24. }
  25. public static IPrincipal CurrentPrincipal {
  26. get {
  27. // FIXME -
  28. // System.Security.Principal.IPrincipal
  29. // not yet implemented
  30. return(null);
  31. }
  32. set {
  33. }
  34. }
  35. // Looks up the object associated with the current thread
  36. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  37. private extern static Thread CurrentThread_internal();
  38. public static Thread CurrentThread {
  39. get {
  40. return(CurrentThread_internal());
  41. }
  42. }
  43. public static LocalDataStoreSlot AllocateDataSlot() {
  44. // FIXME
  45. return(null);
  46. }
  47. public static LocalDataStoreSlot AllocateNamedDataSlot(string name) {
  48. // FIXME
  49. return(null);
  50. }
  51. public static void FreeNamedDataSlot(string name) {
  52. // FIXME
  53. }
  54. public static object GetData(LocalDataStoreSlot slot) {
  55. // FIXME
  56. return(null);
  57. }
  58. public static AppDomain GetDomain() {
  59. // FIXME
  60. return(null);
  61. }
  62. public static int GetDomainID() {
  63. // FIXME
  64. return(0);
  65. }
  66. public static LocalDataStoreSlot GetNamedDataSlot(string name) {
  67. // FIXME
  68. return(null);
  69. }
  70. public static void ResetAbort() {
  71. // FIXME
  72. }
  73. public static void SetData(LocalDataStoreSlot slot, object data) {
  74. // FIXME
  75. }
  76. // Returns milliseconds remaining (due to interrupted sleep)
  77. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  78. private extern static int Sleep_internal(int ms);
  79. // Causes thread to give up its timeslice
  80. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  81. private extern static void Schedule_internal();
  82. public static void Sleep(int millisecondsTimeout) {
  83. if(millisecondsTimeout<0) {
  84. throw new ArgumentException("Negative timeout");
  85. }
  86. if(millisecondsTimeout==0) {
  87. // Schedule another thread
  88. Schedule_internal();
  89. } else {
  90. Thread thread=CurrentThread;
  91. thread.state |= ThreadState.WaitSleepJoin;
  92. int ms_remaining=Sleep_internal(millisecondsTimeout);
  93. thread.state &= ~ThreadState.WaitSleepJoin;
  94. if(ms_remaining>0) {
  95. throw new ThreadInterruptedException("Thread interrupted while sleeping");
  96. }
  97. }
  98. }
  99. public static void Sleep(TimeSpan timeout) {
  100. // LAMESPEC: says to throw ArgumentException too
  101. if(timeout.Milliseconds < 0 || timeout.Milliseconds > Int32.MaxValue) {
  102. throw new ArgumentOutOfRangeException("Timeout out of range");
  103. }
  104. if(timeout.Milliseconds==0) {
  105. // Schedule another thread
  106. Schedule_internal();
  107. } else {
  108. Thread thread=CurrentThread;
  109. thread.state |= ThreadState.WaitSleepJoin;
  110. int ms_remaining=Sleep_internal(timeout.Milliseconds);
  111. thread.state &= ~ThreadState.WaitSleepJoin;
  112. if(ms_remaining>0) {
  113. throw new ThreadInterruptedException("Thread interrupted while sleeping");
  114. }
  115. }
  116. }
  117. private ThreadStart start_delegate=null;
  118. public Thread(ThreadStart start) {
  119. if(start==null) {
  120. throw new ArgumentNullException("Null ThreadStart");
  121. }
  122. // Nothing actually happens here, the fun
  123. // begins when Thread.Start() is called. For
  124. // now, just record what the ThreadStart
  125. // delegate is.
  126. start_delegate=start;
  127. }
  128. public ApartmentState ApartmentState {
  129. get {
  130. // FIXME
  131. return(ApartmentState.Unknown);
  132. }
  133. set {
  134. }
  135. }
  136. public CultureInfo CurrentCulture {
  137. get {
  138. // FIXME
  139. return(null);
  140. }
  141. set {
  142. }
  143. }
  144. public CultureInfo CurrentUICulture {
  145. get {
  146. // FIXME
  147. return(null);
  148. }
  149. set {
  150. }
  151. }
  152. public bool IsAlive {
  153. get {
  154. // LAMESPEC: is a Stopped thread dead?
  155. if((state & ThreadState.Running) != 0 ||
  156. (state & ThreadState.Stopped) != 0) {
  157. return(true);
  158. } else {
  159. return(false);
  160. }
  161. }
  162. }
  163. public bool IsBackground {
  164. get {
  165. // FIXME
  166. return(false);
  167. }
  168. set {
  169. }
  170. }
  171. private string thread_name=null;
  172. public string Name {
  173. get {
  174. return(thread_name);
  175. }
  176. set {
  177. thread_name=value;
  178. }
  179. }
  180. public ThreadPriority Priority {
  181. get {
  182. // FIXME
  183. return(ThreadPriority.Lowest);
  184. }
  185. set {
  186. }
  187. }
  188. private ThreadState state=ThreadState.Unstarted;
  189. public ThreadState ThreadState {
  190. get {
  191. return(state);
  192. }
  193. }
  194. public void Abort() {
  195. // FIXME
  196. }
  197. public void Abort(object stateInfo) {
  198. // FIXME
  199. }
  200. public void Interrupt() {
  201. // FIXME
  202. }
  203. // The current thread joins with 'this'. Set ms to 0 to block
  204. // until this actually exits.
  205. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  206. private extern /*bool*/ int Join_internal(int ms, UInt32 handle);
  207. public void Join() {
  208. if(state == ThreadState.Unstarted) {
  209. throw new ThreadStateException("Thread has not been started");
  210. }
  211. Thread thread=CurrentThread;
  212. thread.state |= ThreadState.WaitSleepJoin;
  213. Join_internal(0, system_thread_handle);
  214. thread.state &= ~ThreadState.WaitSleepJoin;
  215. }
  216. public bool Join(int millisecondsTimeout) {
  217. if(millisecondsTimeout<0) {
  218. throw new ArgumentException("Timeout less than zero");
  219. }
  220. if(state == ThreadState.Unstarted) {
  221. throw new ThreadStateException("Thread has not been started");
  222. }
  223. Thread thread=CurrentThread;
  224. thread.state |= ThreadState.WaitSleepJoin;
  225. bool ret=(Join_internal(millisecondsTimeout,
  226. system_thread_handle)==1);
  227. thread.state &= ~ThreadState.WaitSleepJoin;
  228. return(ret);
  229. }
  230. public bool Join(TimeSpan timeout) {
  231. // LAMESPEC: says to throw ArgumentException too
  232. if(timeout.Milliseconds < 0 || timeout.Milliseconds > Int32.MaxValue) {
  233. throw new ArgumentOutOfRangeException("timeout out of range");
  234. }
  235. if(state == ThreadState.Unstarted) {
  236. throw new ThreadStateException("Thread has not been started");
  237. }
  238. Thread thread=CurrentThread;
  239. thread.state |= ThreadState.WaitSleepJoin;
  240. bool ret=(Join_internal(timeout.Milliseconds,
  241. system_thread_handle)==1);
  242. thread.state &= ~ThreadState.WaitSleepJoin;
  243. return(ret);
  244. }
  245. public void Resume() {
  246. // FIXME
  247. }
  248. // stores a pthread_t, which is defined as unsigned long
  249. // on my system. I _think_ windows uses "unsigned int" for
  250. // its thread handles, so that _should_ work too.
  251. private UInt32 system_thread_handle;
  252. // Returns the system thread handle
  253. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  254. private extern UInt32 Start_internal(ThreadStart start);
  255. public void Start() {
  256. if(state!=ThreadState.Unstarted) {
  257. throw new ThreadStateException("Thread has already been started");
  258. }
  259. state=ThreadState.Running;
  260. // Don't rely on system_thread_handle being
  261. // set before start_delegate runs!
  262. system_thread_handle=Start_internal(start_delegate);
  263. }
  264. public void Suspend() {
  265. // FIXME
  266. }
  267. ~Thread() {
  268. // FIXME
  269. }
  270. }
  271. }