Thread.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. namespace SharpCifs.Util.Sharpen
  4. {
  5. public class Thread : IRunnable
  6. {
  7. private static ThreadGroup _defaultGroup = new ThreadGroup ();
  8. private bool _interrupted;
  9. private IRunnable _runnable;
  10. private ThreadGroup _tgroup;
  11. private System.Threading.Thread _thread;
  12. [ThreadStatic]
  13. private static Thread _wrapperThread;
  14. public Thread () : this(null, null, null)
  15. {
  16. }
  17. public Thread (string name) : this (null, null, name)
  18. {
  19. }
  20. public Thread (ThreadGroup grp, string name) : this (null, grp, name)
  21. {
  22. }
  23. public Thread (IRunnable runnable): this (runnable, null, null)
  24. {
  25. }
  26. Thread (IRunnable runnable, ThreadGroup grp, string name)
  27. {
  28. _thread = new System.Threading.Thread (InternalRun);
  29. this._runnable = runnable ?? this;
  30. _tgroup = grp ?? _defaultGroup;
  31. _tgroup.Add (this);
  32. if (name != null)
  33. _thread.Name = name;
  34. }
  35. private Thread (System.Threading.Thread t)
  36. {
  37. _thread = t;
  38. _tgroup = _defaultGroup;
  39. _tgroup.Add (this);
  40. }
  41. public static Thread CurrentThread ()
  42. {
  43. if (_wrapperThread == null) {
  44. _wrapperThread = new Thread (System.Threading.Thread.CurrentThread);
  45. }
  46. return _wrapperThread;
  47. }
  48. public string GetName ()
  49. {
  50. return _thread.Name;
  51. }
  52. public ThreadGroup GetThreadGroup ()
  53. {
  54. return _tgroup;
  55. }
  56. private void InternalRun ()
  57. {
  58. _wrapperThread = this;
  59. try {
  60. _runnable.Run ();
  61. } catch (Exception exception) {
  62. Console.WriteLine (exception);
  63. } finally {
  64. _tgroup.Remove (this);
  65. }
  66. }
  67. public static void Yield ()
  68. {
  69. }
  70. public void Interrupt ()
  71. {
  72. lock (_thread) {
  73. _interrupted = true;
  74. //thread.Interrupt ();
  75. //TODO: implement CancellationToken
  76. //_thread.Abort();
  77. throw new NotImplementedException("implement CancellationToken for thread");
  78. }
  79. }
  80. public static bool Interrupted ()
  81. {
  82. if (Thread._wrapperThread == null) {
  83. return false;
  84. }
  85. Thread wrapperThread = Thread._wrapperThread;
  86. lock (wrapperThread) {
  87. bool interrupted = Thread._wrapperThread._interrupted;
  88. Thread._wrapperThread._interrupted = false;
  89. return interrupted;
  90. }
  91. }
  92. public bool IsAlive ()
  93. {
  94. return _thread.IsAlive;
  95. }
  96. public void Join ()
  97. {
  98. _thread.Join ();
  99. }
  100. public void Join (long timeout)
  101. {
  102. _thread.Join ((int)timeout);
  103. }
  104. public virtual void Run ()
  105. {
  106. }
  107. public void SetDaemon (bool daemon)
  108. {
  109. _thread.IsBackground = daemon;
  110. }
  111. public void SetName (string name)
  112. {
  113. _thread.Name = name;
  114. }
  115. public static void Sleep (long milis)
  116. {
  117. System.Threading.Thread.Sleep ((int)milis);
  118. }
  119. public void Start ()
  120. {
  121. _thread.Start ();
  122. }
  123. public void Abort ()
  124. {
  125. //TODO: implement CancellationToken
  126. //_thread.Abort ();
  127. throw new NotImplementedException("implement CancellationToken for thread");
  128. }
  129. }
  130. public class ThreadGroup
  131. {
  132. private List<Thread> _threads = new List<Thread> ();
  133. public ThreadGroup()
  134. {
  135. }
  136. public ThreadGroup (string name)
  137. {
  138. }
  139. internal void Add (Thread t)
  140. {
  141. lock (_threads) {
  142. _threads.Add (t);
  143. }
  144. }
  145. internal void Remove (Thread t)
  146. {
  147. lock (_threads) {
  148. _threads.Remove (t);
  149. }
  150. }
  151. public int Enumerate (Thread[] array)
  152. {
  153. lock (_threads) {
  154. int count = Math.Min (array.Length, _threads.Count);
  155. _threads.CopyTo (0, array, 0, count);
  156. return count;
  157. }
  158. }
  159. }
  160. }