Thread.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using SharpCifs.Util.DbsHelper;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. namespace SharpCifs.Util.Sharpen
  6. {
  7. public class Thread : IRunnable
  8. {
  9. private static ThreadGroup DefaultGroup = new ThreadGroup();
  10. [ThreadStatic]
  11. private static Thread WrapperThread;
  12. public static Thread CurrentThread()
  13. {
  14. if (Thread.WrapperThread == null)
  15. {
  16. Thread.WrapperThread = new Thread(System.Environment.CurrentManagedThreadId);
  17. }
  18. return Thread.WrapperThread;
  19. }
  20. public CancellationTokenSource Canceller => this._canceller;
  21. public bool IsCanceled
  22. {
  23. get
  24. {
  25. if (this._canceller?.IsCancellationRequested == true
  26. && !this._isCanceled)
  27. {
  28. this._isCanceled = true;
  29. }
  30. return this._isCanceled;
  31. }
  32. }
  33. private IRunnable _runnable;
  34. private ThreadGroup _tgroup;
  35. private System.Threading.Tasks.Task _task = null;
  36. private CancellationTokenSource _canceller = null;
  37. private string _name = string.Empty;
  38. private bool _isBackground = true;
  39. private bool _interrupted = false;
  40. private int? _id = null;
  41. private bool _isRunning = false;
  42. private bool _isCanceled = false;
  43. public Thread() : this(null, null, null)
  44. {
  45. }
  46. public Thread(string name) : this(null, null, name)
  47. {
  48. }
  49. public Thread(ThreadGroup grp, string name) : this(null, grp, name)
  50. {
  51. }
  52. public Thread(IRunnable runnable) : this(runnable, null, null)
  53. {
  54. }
  55. private Thread(IRunnable runnable, ThreadGroup grp, string name)
  56. {
  57. this._runnable = runnable ?? this;
  58. this._tgroup = grp ?? DefaultGroup;
  59. this._tgroup.Add(this);
  60. if (name != null)
  61. {
  62. this._name = name;
  63. }
  64. }
  65. private Thread(int threadId)
  66. {
  67. this._id = threadId;
  68. this._tgroup = DefaultGroup;
  69. this._tgroup.Add(this);
  70. }
  71. public string GetName()
  72. {
  73. return this._name;
  74. }
  75. public ThreadGroup GetThreadGroup()
  76. {
  77. return this._tgroup;
  78. }
  79. public static void Yield()
  80. {
  81. }
  82. public void Interrupt()
  83. {
  84. this._interrupted = true;
  85. this._canceller?.Cancel(true);
  86. }
  87. public static bool Interrupted()
  88. {
  89. if (Thread.WrapperThread == null)
  90. {
  91. return false;
  92. }
  93. Thread wrapperThread = Thread.WrapperThread;
  94. lock (wrapperThread)
  95. {
  96. bool interrupted = Thread.WrapperThread._interrupted;
  97. Thread.WrapperThread._interrupted = false;
  98. return interrupted;
  99. }
  100. }
  101. public bool IsAlive()
  102. {
  103. if (this._task == null)
  104. return true; //実行されていない
  105. //Taskが存在し、続行中のときtrue
  106. return (!this._task.IsCanceled
  107. && !this._task.IsFaulted
  108. && !this._task.IsCompleted);
  109. }
  110. public void Join()
  111. {
  112. this._task?.Wait();
  113. }
  114. public void Join(long timeout)
  115. {
  116. this._task?.Wait((int) timeout);
  117. }
  118. public virtual void Run()
  119. {
  120. }
  121. public void SetDaemon(bool daemon)
  122. {
  123. this._isBackground = daemon;
  124. }
  125. public void SetName(string name)
  126. {
  127. this._name = name;
  128. }
  129. public static void Sleep(long milis)
  130. {
  131. System.Threading.Tasks.Task.Delay((int) milis).Wait();
  132. }
  133. public void Start(bool isSynced = false)
  134. {
  135. if (this._isRunning)
  136. throw new InvalidOperationException("Thread Already started.");
  137. this._canceller = new CancellationTokenSource();
  138. this._task = System.Threading.Tasks.Task.Run(() =>
  139. {
  140. Thread.WrapperThread = this;
  141. this._id = System.Environment.CurrentManagedThreadId;
  142. //Log.Out("Thread.Start - Task Start");
  143. this._isRunning = true;
  144. try
  145. {
  146. this._runnable.Run();
  147. //Log.Out("Thread.Start - Task Normaly End");
  148. }
  149. catch (Exception exception)
  150. {
  151. //Log.Out("Thread.Start - Task Error End");
  152. Console.WriteLine(exception);
  153. }
  154. finally
  155. {
  156. this._isRunning = false;
  157. this._tgroup?.Remove(this);
  158. this._canceller?.Dispose();
  159. this._canceller = null;
  160. //Log.Out("Thread.Start - Task Close Completed");
  161. }
  162. }, this._canceller.Token);
  163. //同期的に実行するとき、動作中フラグONまで待つ。
  164. if (isSynced)
  165. while (!this._isRunning)
  166. System.Threading.Tasks.Task.Delay(300).GetAwaiter().GetResult();
  167. }
  168. public void Cancel(bool isSynced = false)
  169. {
  170. //Log.Out("Thread.Cancel");
  171. this._isCanceled = true;
  172. this._canceller?.Cancel(true);
  173. //同期的に実行するとき、動作中フラグOFFまで待つ。
  174. if (isSynced)
  175. while (this._isRunning)
  176. System.Threading.Tasks.Task.Delay(300).GetAwaiter().GetResult();
  177. }
  178. public bool Equals(Thread thread)
  179. {
  180. //渡し値スレッドがnullのとき、合致しない
  181. if (thread == null)
  182. return false;
  183. //自身か渡し値スレッドの、スレッドIDが取得出来ていない(=スレッド未生成)
  184. // →合致しない
  185. if (this._id == null
  186. || thread._id == null)
  187. return false;
  188. return (this._id == thread._id);
  189. }
  190. public void Dispose()
  191. {
  192. //Log.Out("Thread.Dispose");
  193. this._runnable = null;
  194. this._tgroup = null;
  195. this._task = null;
  196. this._canceller?.Dispose();
  197. this._canceller = null;
  198. this._name = null;
  199. this._isRunning = false;
  200. this._id = null;
  201. }
  202. }
  203. public class ThreadGroup
  204. {
  205. private List<Thread> _threads = new List<Thread>();
  206. public ThreadGroup()
  207. {
  208. }
  209. public ThreadGroup(string name)
  210. {
  211. }
  212. internal void Add(Thread t)
  213. {
  214. lock (_threads)
  215. {
  216. _threads.Add(t);
  217. }
  218. }
  219. internal void Remove(Thread t)
  220. {
  221. lock (_threads)
  222. {
  223. _threads.Remove(t);
  224. }
  225. }
  226. public int Enumerate(Thread[] array)
  227. {
  228. lock (_threads)
  229. {
  230. int count = Math.Min(array.Length, _threads.Count);
  231. _threads.CopyTo(0, array, 0, count);
  232. return count;
  233. }
  234. }
  235. }
  236. }