Thread.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. // CoreUtil
  2. //
  3. // Copyright (C) 2012-2014 Daiyuu Nobori. All Rights Reserved.
  4. // Copyright (C) 2012-2014 SoftEther VPN Project at University of Tsukuba. All Rights Reserved.
  5. // Comments: Tetsuo Sugiyama, Ph.D.
  6. //
  7. //
  8. // This program is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU General Public License
  10. // version 2 as published by the Free Software Foundation.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License version 2
  18. // along with this program; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  24. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  25. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  26. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  27. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. // THE LICENSE AGREEMENT IS ATTACHED ON THE SOURCE-CODE PACKAGE
  30. // AS "LICENSE.TXT" FILE. READ THE TEXT FILE IN ADVANCE TO USE THE SOFTWARE.
  31. //
  32. //
  33. // THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN,
  34. // UNDER JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY,
  35. // MERGE, PUBLISH, DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS
  36. // SOFTWARE, THAT ANY JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS
  37. // SOFTWARE OR ITS CONTENTS, AGAINST US (SOFTETHER PROJECT, SOFTETHER
  38. // CORPORATION, DAIYUU NOBORI OR OTHER SUPPLIERS), OR ANY JURIDICAL
  39. // DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND OF USING, COPYING,
  40. // MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING, AND/OR
  41. // SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
  42. // CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO
  43. // EXCLUSIVE JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO,
  44. // JAPAN. YOU MUST WAIVE ALL DEFENSES OF LACK OF PERSONAL JURISDICTION
  45. // AND FORUM NON CONVENIENS. PROCESS MAY BE SERVED ON EITHER PARTY IN
  46. // THE MANNER AUTHORIZED BY APPLICABLE LAW OR COURT RULE.
  47. //
  48. // USE ONLY IN JAPAN. DO NOT USE IT IN OTHER COUNTRIES. IMPORTING THIS
  49. // SOFTWARE INTO OTHER COUNTRIES IS AT YOUR OWN RISK. SOME COUNTRIES
  50. // PROHIBIT ENCRYPTED COMMUNICATIONS. USING THIS SOFTWARE IN OTHER
  51. // COUNTRIES MIGHT BE RESTRICTED.
  52. //
  53. //
  54. // DEAR SECURITY EXPERTS
  55. // ---------------------
  56. //
  57. // If you find a bug or a security vulnerability please kindly inform us
  58. // about the problem immediately so that we can fix the security problem
  59. // to protect a lot of users around the world as soon as possible.
  60. //
  61. // Our e-mail address for security reports is:
  62. // softether-vpn-security [at] softether.org
  63. //
  64. // Please note that the above e-mail address is not a technical support
  65. // inquiry address. If you need technical assistance, please visit
  66. // http://www.softether.org/ and ask your question on the users forum.
  67. //
  68. // Thank you for your cooperation.
  69. using System;
  70. using System.Threading;
  71. using System.Data;
  72. using System.Data.Sql;
  73. using System.Data.SqlClient;
  74. using System.Data.SqlTypes;
  75. using System.Text;
  76. using System.Configuration;
  77. using System.Collections;
  78. using System.Collections.Generic;
  79. using System.Security.Cryptography;
  80. using System.Web;
  81. using System.Web.Security;
  82. using System.Web.UI;
  83. using System.Web.UI.WebControls;
  84. using System.Web.UI.WebControls.WebParts;
  85. using System.Web.UI.HtmlControls;
  86. using System.IO;
  87. using System.Drawing;
  88. using System.Drawing.Imaging;
  89. using System.Drawing.Drawing2D;
  90. using System.Diagnostics;
  91. using System.Web.Mail;
  92. using System.Net;
  93. using System.Net.Sockets;
  94. using System.Net.NetworkInformation;
  95. using System.Net.Mail;
  96. using System.Net.Mime;
  97. using CoreUtil;
  98. #pragma warning disable 0618
  99. namespace CoreUtil
  100. {
  101. class WorkerQueuePrivate
  102. {
  103. object lockObj = new object();
  104. List<ThreadObj> thread_list;
  105. ThreadProc thread_proc;
  106. int num_worker_threads;
  107. Queue<object> taskQueue = new Queue<object>();
  108. Exception raised_exception = null;
  109. void worker_thread(object param)
  110. {
  111. while (true)
  112. {
  113. object task = null;
  114. lock (lockObj)
  115. {
  116. if (taskQueue.Count == 0)
  117. {
  118. return;
  119. }
  120. task = taskQueue.Dequeue();
  121. }
  122. try
  123. {
  124. this.thread_proc(task);
  125. }
  126. catch (Exception ex)
  127. {
  128. if (raised_exception == null)
  129. {
  130. raised_exception = ex;
  131. }
  132. Console.WriteLine(ex.Message);
  133. }
  134. }
  135. }
  136. public WorkerQueuePrivate(ThreadProc thread_proc, int num_worker_threads, object[] tasks)
  137. {
  138. thread_list = new List<ThreadObj>();
  139. int i;
  140. this.thread_proc = thread_proc;
  141. this.num_worker_threads = num_worker_threads;
  142. foreach (object task in tasks)
  143. {
  144. taskQueue.Enqueue(task);
  145. }
  146. raised_exception = null;
  147. for (i = 0; i < num_worker_threads; i++)
  148. {
  149. ThreadObj t = new ThreadObj(worker_thread);
  150. thread_list.Add(t);
  151. }
  152. foreach (ThreadObj t in thread_list)
  153. {
  154. t.WaitForEnd();
  155. }
  156. if (raised_exception != null)
  157. {
  158. throw raised_exception;
  159. }
  160. }
  161. }
  162. public static class Tick64
  163. {
  164. static object lock_obj = new object();
  165. static uint last_value = 0;
  166. static bool is_first = true;
  167. static uint num_round = 0;
  168. public static long Value
  169. {
  170. get
  171. {
  172. unchecked
  173. {
  174. lock (lock_obj)
  175. {
  176. uint current_value = (uint)(System.Environment.TickCount + 3864700935);
  177. if (is_first)
  178. {
  179. last_value = current_value;
  180. is_first = false;
  181. }
  182. if (last_value > current_value)
  183. {
  184. num_round++;
  185. }
  186. last_value = current_value;
  187. ulong ret = 4294967296UL * (ulong)num_round + current_value;
  188. return (long)ret;
  189. }
  190. }
  191. }
  192. }
  193. public static uint ValueUInt32
  194. {
  195. get
  196. {
  197. unchecked
  198. {
  199. return (uint)((ulong)Value);
  200. }
  201. }
  202. }
  203. }
  204. public class Event
  205. {
  206. EventWaitHandle h;
  207. public const int Infinite = Timeout.Infinite;
  208. public Event()
  209. {
  210. init(false);
  211. }
  212. public Event(bool manualReset)
  213. {
  214. init(manualReset);
  215. }
  216. void init(bool manualReset)
  217. {
  218. h = new EventWaitHandle(false, (manualReset ? EventResetMode.ManualReset : EventResetMode.AutoReset));
  219. }
  220. public void Set()
  221. {
  222. h.Set();
  223. }
  224. public bool Wait()
  225. {
  226. return Wait(Infinite);
  227. }
  228. public bool Wait(int millisecs)
  229. {
  230. return h.WaitOne(millisecs, false);
  231. }
  232. static EventWaitHandle[] toArray(Event[] events)
  233. {
  234. List<EventWaitHandle> list = new List<EventWaitHandle>();
  235. foreach (Event e in events)
  236. {
  237. list.Add(e.h);
  238. }
  239. return list.ToArray();
  240. }
  241. public static bool WaitAll(Event[] events)
  242. {
  243. return WaitAll(events, Infinite);
  244. }
  245. public static bool WaitAll(Event[] events, int millisecs)
  246. {
  247. if (events.Length <= 64)
  248. {
  249. return waitAllInner(events, millisecs);
  250. }
  251. else
  252. {
  253. return waitAllMulti(events, millisecs);
  254. }
  255. }
  256. static bool waitAllMulti(Event[] events, int millisecs)
  257. {
  258. int numBlocks = (events.Length + 63) / 64;
  259. List<Event>[] list = new List<Event>[numBlocks];
  260. int i;
  261. for (i = 0; i < numBlocks; i++)
  262. {
  263. list[i] = new List<Event>();
  264. }
  265. for (i = 0; i < events.Length; i++)
  266. {
  267. list[i / 64].Add(events[i]);
  268. }
  269. double start = Time.NowDouble;
  270. double giveup = start + (double)millisecs / 1000.0;
  271. foreach (List<Event> o in list)
  272. {
  273. double now = Time.NowDouble;
  274. if (now <= giveup || millisecs < 0)
  275. {
  276. int waitmsecs;
  277. if (millisecs >= 0)
  278. {
  279. waitmsecs = (int)((giveup - now) * 1000.0);
  280. }
  281. else
  282. {
  283. waitmsecs = Timeout.Infinite;
  284. }
  285. bool ret = waitAllInner(o.ToArray(), waitmsecs);
  286. if (ret == false)
  287. {
  288. return false;
  289. }
  290. }
  291. else
  292. {
  293. return false;
  294. }
  295. }
  296. return true;
  297. }
  298. static bool waitAllInner(Event[] events, int millisecs)
  299. {
  300. if (events.Length == 1)
  301. {
  302. return events[0].Wait(millisecs);
  303. }
  304. return EventWaitHandle.WaitAll(toArray(events), millisecs, false);
  305. }
  306. public static bool WaitAny(Event[] events)
  307. {
  308. return WaitAny(events, Infinite);
  309. }
  310. public static bool WaitAny(Event[] events, int millisecs)
  311. {
  312. if (events.Length == 1)
  313. {
  314. return events[0].Wait(millisecs);
  315. }
  316. return ((WaitHandle.WaitTimeout == EventWaitHandle.WaitAny(toArray(events), millisecs, false)) ? false : true);
  317. }
  318. public IntPtr Handle
  319. {
  320. get
  321. {
  322. return h.Handle;
  323. }
  324. }
  325. }
  326. public class ThreadData
  327. {
  328. static LocalDataStoreSlot slot = Thread.AllocateDataSlot();
  329. public readonly SortedDictionary<string, object> DataList = new SortedDictionary<string, object>();
  330. public static ThreadData CurrentThreadData
  331. {
  332. get
  333. {
  334. return GetCurrentThreadData();
  335. }
  336. }
  337. public static ThreadData GetCurrentThreadData()
  338. {
  339. ThreadData t;
  340. try
  341. {
  342. t = (ThreadData)Thread.GetData(slot);
  343. }
  344. catch
  345. {
  346. t = null;
  347. }
  348. if (t == null)
  349. {
  350. t = new ThreadData();
  351. Thread.SetData(slot, t);
  352. }
  353. return t;
  354. }
  355. }
  356. public delegate void ThreadProc(object userObject);
  357. public class ThreadObj
  358. {
  359. static int defaultStackSize = 100000;
  360. static LocalDataStoreSlot currentObjSlot = Thread.AllocateDataSlot();
  361. public const int Infinite = Timeout.Infinite;
  362. ThreadProc proc;
  363. Thread thread;
  364. EventWaitHandle waitInit;
  365. EventWaitHandle waitEnd;
  366. EventWaitHandle waitInitForUser;
  367. public Thread Thread
  368. {
  369. get { return thread; }
  370. }
  371. object userObject;
  372. public ThreadObj(ThreadProc threadProc)
  373. {
  374. init(threadProc, null, 0);
  375. }
  376. public ThreadObj(ThreadProc threadProc, int stacksize)
  377. {
  378. init(threadProc, null, stacksize);
  379. }
  380. public ThreadObj(ThreadProc threadProc, object userObject)
  381. {
  382. init(threadProc, userObject, 0);
  383. }
  384. public ThreadObj(ThreadProc threadProc, object userObject, int stacksize)
  385. {
  386. init(threadProc, userObject, stacksize);
  387. }
  388. void init(ThreadProc threadProc, object userObject, int stacksize)
  389. {
  390. if (stacksize == 0)
  391. {
  392. stacksize = defaultStackSize;
  393. }
  394. this.proc = threadProc;
  395. this.userObject = userObject;
  396. waitInit = new EventWaitHandle(false, EventResetMode.AutoReset);
  397. waitEnd = new EventWaitHandle(false, EventResetMode.ManualReset);
  398. waitInitForUser = new EventWaitHandle(false, EventResetMode.ManualReset);
  399. this.thread = new Thread(new ParameterizedThreadStart(commonThreadProc), stacksize);
  400. this.thread.Start(this);
  401. waitInit.WaitOne();
  402. }
  403. public static int DefaultStackSize
  404. {
  405. get
  406. {
  407. return defaultStackSize;
  408. }
  409. set
  410. {
  411. defaultStackSize = value;
  412. }
  413. }
  414. void commonThreadProc(object obj)
  415. {
  416. Thread.SetData(currentObjSlot, this);
  417. waitInit.Set();
  418. try
  419. {
  420. this.proc(this.userObject);
  421. }
  422. finally
  423. {
  424. waitEnd.Set();
  425. }
  426. }
  427. public static ThreadObj GetCurrentThreadObj()
  428. {
  429. return (ThreadObj)Thread.GetData(currentObjSlot);
  430. }
  431. public static void NoticeInited()
  432. {
  433. GetCurrentThreadObj().waitInitForUser.Set();
  434. }
  435. public void WaitForInit()
  436. {
  437. waitInitForUser.WaitOne();
  438. }
  439. public void WaitForEnd(int timeout)
  440. {
  441. waitEnd.WaitOne(timeout, false);
  442. }
  443. public void WaitForEnd()
  444. {
  445. waitEnd.WaitOne();
  446. }
  447. public static void Sleep(int millisec)
  448. {
  449. if (millisec == 0x7fffffff)
  450. {
  451. millisec = ThreadObj.Infinite;
  452. }
  453. Thread.Sleep(millisec);
  454. }
  455. public static void Yield()
  456. {
  457. Thread.Sleep(0);
  458. }
  459. public static void ProcessWorkQueue(ThreadProc thread_proc, int num_worker_threads, object[] tasks)
  460. {
  461. WorkerQueuePrivate q = new WorkerQueuePrivate(thread_proc, num_worker_threads, tasks);
  462. }
  463. }
  464. }
  465. // Developed by SoftEther VPN Project at University of Tsukuba in Japan.
  466. // Department of Computer Science has dozens of overly-enthusiastic geeks.
  467. // Join us: http://www.tsukuba.ac.jp/english/admission/