threads.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. #ifdef SYSTEM_WIN32
  2. #pragma warning(push)
  3. #pragma warning(disable:4100) // unreferenced formal parameter
  4. #endif
  5. #ifdef SYSTEM_WIN32
  6. #define WIN32_LEAN_AND_MEAN
  7. #include <windows.h>
  8. #include <malloc.h>
  9. #endif
  10. #ifdef HAVE_CONFIG_H
  11. #include "config.h"
  12. #endif
  13. #include "cmdlib.h"
  14. #include "messages.h"
  15. #include "log.h"
  16. #include "threads.h"
  17. #include "blockmem.h"
  18. #ifdef SYSTEM_POSIX
  19. #ifdef HAVE_SYS_TIME_H
  20. #include <sys/time.h>
  21. #endif
  22. #ifdef HAVE_SYS_RESOURCE_H
  23. #include <sys/resource.h>
  24. #include <pthread.h>
  25. #endif
  26. #ifdef HAVE_PTHREAD_H
  27. #include <pthread.h>
  28. #endif
  29. #endif
  30. #include "hlassert.h"
  31. q_threadpriority g_threadpriority = DEFAULT_THREAD_PRIORITY;
  32. #define THREADTIMES_SIZE 100
  33. #define THREADTIMES_SIZEf (float)(THREADTIMES_SIZE)
  34. static int dispatch = 0;
  35. static int workcount = 0;
  36. static int oldf = 0;
  37. static bool pacifier = false;
  38. static bool threaded = false;
  39. static double threadstart = 0;
  40. static double threadtimes[THREADTIMES_SIZE];
  41. int GetThreadWork()
  42. {
  43. int r, f, i;
  44. double ct, finish, finish2, finish3;
  45. ThreadLock();
  46. if (dispatch == 0)
  47. {
  48. oldf = 0;
  49. }
  50. if (dispatch > workcount)
  51. {
  52. Developer(DEVELOPER_LEVEL_ERROR, "dispatch > workcount!!!\n");
  53. ThreadUnlock();
  54. return -1;
  55. }
  56. if (dispatch == workcount)
  57. {
  58. Developer(DEVELOPER_LEVEL_MESSAGE, "dispatch == workcount, work is complete\n");
  59. ThreadUnlock();
  60. return -1;
  61. }
  62. if (dispatch < 0)
  63. {
  64. Developer(DEVELOPER_LEVEL_ERROR, "negative dispatch!!!\n");
  65. ThreadUnlock();
  66. return -1;
  67. }
  68. f = THREADTIMES_SIZE * dispatch / workcount;
  69. if (pacifier)
  70. {
  71. printf("\r%6d /%6d", dispatch, workcount);
  72. #ifdef ZHLT_PROGRESSFILE // AJM
  73. if (g_progressfile)
  74. {
  75. }
  76. #endif
  77. if (f != oldf)
  78. {
  79. ct = I_FloatTime();
  80. /* Fill in current time for threadtimes record */
  81. for (i = oldf; i <= f; i++)
  82. {
  83. if (threadtimes[i] < 1)
  84. {
  85. threadtimes[i] = ct;
  86. }
  87. }
  88. oldf = f;
  89. if (f > 10)
  90. {
  91. finish = (ct - threadtimes[0]) * (THREADTIMES_SIZEf - f) / f;
  92. finish2 = 10.0 * (ct - threadtimes[f - 10]) * (THREADTIMES_SIZEf - f) / THREADTIMES_SIZEf;
  93. finish3 = THREADTIMES_SIZEf * (ct - threadtimes[f - 1]) * (THREADTIMES_SIZEf - f) / THREADTIMES_SIZEf;
  94. if (finish > 1.0)
  95. {
  96. printf(" (%d%%: est. time to completion %ld/%ld/%ld secs) ", f, (long)(finish), (long)(finish2),
  97. (long)(finish3));
  98. #ifdef ZHLT_PROGRESSFILE // AJM
  99. if (g_progressfile)
  100. {
  101. }
  102. #endif
  103. }
  104. else
  105. {
  106. printf(" (%d%%: est. time to completion <1 sec) ", f);
  107. #ifdef ZHLT_PROGRESSFILE // AJM
  108. if (g_progressfile)
  109. {
  110. }
  111. #endif
  112. }
  113. }
  114. }
  115. }
  116. else
  117. {
  118. if (f != oldf)
  119. {
  120. oldf = f;
  121. switch (f)
  122. {
  123. case 10:
  124. case 20:
  125. case 30:
  126. case 40:
  127. case 50:
  128. case 60:
  129. case 70:
  130. case 80:
  131. case 90:
  132. case 100:
  133. /*
  134. case 5:
  135. case 15:
  136. case 25:
  137. case 35:
  138. case 45:
  139. case 55:
  140. case 65:
  141. case 75:
  142. case 85:
  143. case 95:
  144. */
  145. printf("%d%%...", f);
  146. default:
  147. break;
  148. }
  149. }
  150. }
  151. r = dispatch;
  152. dispatch++;
  153. ThreadUnlock();
  154. return r;
  155. }
  156. q_threadfunction workfunction;
  157. static void ThreadWorkerFunction(int unused)
  158. {
  159. int work;
  160. while ((work = GetThreadWork()) != -1)
  161. {
  162. workfunction(work);
  163. }
  164. }
  165. #ifdef SYSTEM_WIN32
  166. #pragma warning(pop)
  167. #endif
  168. void RunThreadsOnIndividual(int workcnt, bool showpacifier, q_threadfunction func)
  169. {
  170. workfunction = func;
  171. RunThreadsOn(workcnt, showpacifier, ThreadWorkerFunction);
  172. }
  173. #ifndef SINGLE_THREADED
  174. /*====================
  175. | Begin SYSTEM_WIN32
  176. =*/
  177. #ifdef SYSTEM_WIN32
  178. #define USED
  179. #include <windows.h>
  180. int g_numthreads = DEFAULT_NUMTHREADS;
  181. static CRITICAL_SECTION crit;
  182. static int enter;
  183. void ThreadSetPriority(q_threadpriority type)
  184. {
  185. int val;
  186. g_threadpriority = type;
  187. switch (g_threadpriority)
  188. {
  189. case eThreadPriorityLow:
  190. val = IDLE_PRIORITY_CLASS;
  191. break;
  192. case eThreadPriorityHigh:
  193. val = HIGH_PRIORITY_CLASS;
  194. break;
  195. case eThreadPriorityNormal:
  196. default:
  197. val = NORMAL_PRIORITY_CLASS;
  198. break;
  199. }
  200. SetPriorityClass(GetCurrentProcess(), val);
  201. }
  202. #if 0
  203. static void AdjustPriority(HANDLE hThread)
  204. {
  205. int val;
  206. switch (g_threadpriority)
  207. {
  208. case eThreadPriorityLow:
  209. val = THREAD_PRIORITY_HIGHEST;
  210. break;
  211. case eThreadPriorityHigh:
  212. val = THREAD_PRIORITY_LOWEST;
  213. break;
  214. case eThreadPriorityNormal:
  215. default:
  216. val = THREAD_PRIORITY_NORMAL;
  217. break;
  218. }
  219. SetThreadPriority(hThread, val);
  220. }
  221. #endif
  222. void ThreadSetDefault()
  223. {
  224. SYSTEM_INFO info;
  225. if (g_numthreads == -1) // not set manually
  226. {
  227. GetSystemInfo(&info);
  228. g_numthreads = info.dwNumberOfProcessors;
  229. if (g_numthreads < 1 || g_numthreads > 32)
  230. {
  231. g_numthreads = 1;
  232. }
  233. }
  234. }
  235. void ThreadLock()
  236. {
  237. if (!threaded)
  238. {
  239. return;
  240. }
  241. EnterCriticalSection(&crit);
  242. if (enter)
  243. {
  244. Warning("Recursive ThreadLock\n");
  245. }
  246. enter++;
  247. }
  248. void ThreadUnlock()
  249. {
  250. if (!threaded)
  251. {
  252. return;
  253. }
  254. if (!enter)
  255. {
  256. Error("ThreadUnlock without lock\n");
  257. }
  258. enter--;
  259. LeaveCriticalSection(&crit);
  260. }
  261. q_threadfunction q_entry;
  262. static DWORD WINAPI ThreadEntryStub(LPVOID pParam)
  263. {
  264. q_entry((int)pParam);
  265. return 0;
  266. }
  267. void threads_InitCrit()
  268. {
  269. InitializeCriticalSection(&crit);
  270. threaded = true;
  271. }
  272. void threads_UninitCrit()
  273. {
  274. DeleteCriticalSection(&crit);
  275. }
  276. void RunThreadsOn(int workcnt, bool showpacifier, q_threadfunction func)
  277. {
  278. DWORD threadid[MAX_THREADS];
  279. HANDLE threadhandle[MAX_THREADS];
  280. int i;
  281. double start, end;
  282. threadstart = I_FloatTime();
  283. start = threadstart;
  284. for (i = 0; i < THREADTIMES_SIZE; i++)
  285. {
  286. threadtimes[i] = 0;
  287. }
  288. dispatch = 0;
  289. workcount = workcnt;
  290. oldf = -1;
  291. pacifier = showpacifier;
  292. threaded = true;
  293. q_entry = func;
  294. if (workcount < dispatch)
  295. {
  296. Developer(DEVELOPER_LEVEL_ERROR, "RunThreadsOn: Workcount(%i) < dispatch(%i)\n", workcount, dispatch);
  297. }
  298. hlassume(workcount >= dispatch, assume_BadWorkcount);
  299. //
  300. // Create all the threads (suspended)
  301. //
  302. threads_InitCrit();
  303. for (i = 0; i < g_numthreads; i++)
  304. {
  305. HANDLE hThread = CreateThread(NULL,
  306. 0,
  307. (LPTHREAD_START_ROUTINE) ThreadEntryStub,
  308. (LPVOID) i,
  309. CREATE_SUSPENDED,
  310. &threadid[i]);
  311. if (hThread != NULL)
  312. {
  313. threadhandle[i] = hThread;
  314. }
  315. else
  316. {
  317. LPVOID lpMsgBuf;
  318. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  319. FORMAT_MESSAGE_FROM_SYSTEM |
  320. FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  321. (LPTSTR) & lpMsgBuf, 0, NULL);
  322. // Process any inserts in lpMsgBuf.
  323. // ...
  324. // Display the string.
  325. Developer(DEVELOPER_LEVEL_ERROR, "CreateThread #%d [%08X] failed : %s\n", i, threadhandle[i], lpMsgBuf);
  326. Fatal(assume_THREAD_ERROR, "Unable to create thread #%d", i);
  327. // Free the buffer.
  328. LocalFree(lpMsgBuf);
  329. }
  330. }
  331. CheckFatal();
  332. // Start all the threads
  333. for (i = 0; i < g_numthreads; i++)
  334. {
  335. if (ResumeThread(threadhandle[i]) == 0xFFFFFFFF)
  336. {
  337. LPVOID lpMsgBuf;
  338. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  339. FORMAT_MESSAGE_FROM_SYSTEM |
  340. FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  341. (LPTSTR) & lpMsgBuf, 0, NULL);
  342. // Process any inserts in lpMsgBuf.
  343. // ...
  344. // Display the string.
  345. Developer(DEVELOPER_LEVEL_ERROR, "ResumeThread #%d [%08X] failed : %s\n", i, threadhandle[i], lpMsgBuf);
  346. Fatal(assume_THREAD_ERROR, "Unable to start thread #%d", i);
  347. // Free the buffer.
  348. LocalFree(lpMsgBuf);
  349. }
  350. }
  351. CheckFatal();
  352. // Wait for threads to complete
  353. for (i = 0; i < g_numthreads; i++)
  354. {
  355. Developer(DEVELOPER_LEVEL_MESSAGE, "WaitForSingleObject on thread #%d [%08X]\n", i, threadhandle[i]);
  356. WaitForSingleObject(threadhandle[i], INFINITE);
  357. }
  358. threads_UninitCrit();
  359. q_entry = NULL;
  360. threaded = false;
  361. end = I_FloatTime();
  362. if (pacifier)
  363. {
  364. printf("\r%60s\r", "");
  365. }
  366. Log(" (%.2f seconds)\n", end - start);
  367. }
  368. #endif
  369. /*=
  370. | End SYSTEM_WIN32
  371. =====================*/
  372. /*====================
  373. | Begin SYSTEM_POSIX
  374. =*/
  375. #ifdef SYSTEM_POSIX
  376. #define USED
  377. int g_numthreads = DEFAULT_NUMTHREADS;
  378. void ThreadSetPriority(q_threadpriority type)
  379. {
  380. int val;
  381. g_threadpriority = type;
  382. // Currently in Linux land users are incapable of raising the priority level of their processes
  383. // Unless you are root -high is useless . . .
  384. switch (g_threadpriority)
  385. {
  386. case eThreadPriorityLow:
  387. val = PRIO_MAX;
  388. break;
  389. case eThreadPriorityHigh:
  390. val = PRIO_MIN;
  391. break;
  392. case eThreadPriorityNormal:
  393. default:
  394. val = 0;
  395. break;
  396. }
  397. setpriority(PRIO_PROCESS, 0, val);
  398. }
  399. void ThreadSetDefault()
  400. {
  401. if (g_numthreads == -1)
  402. {
  403. g_numthreads = 1;
  404. }
  405. }
  406. typedef void* pthread_addr_t;
  407. pthread_mutex_t* my_mutex;
  408. void ThreadLock()
  409. {
  410. if (my_mutex)
  411. {
  412. pthread_mutex_lock(my_mutex);
  413. }
  414. }
  415. void ThreadUnlock()
  416. {
  417. if (my_mutex)
  418. {
  419. pthread_mutex_unlock(my_mutex);
  420. }
  421. }
  422. q_threadfunction q_entry;
  423. static void* CDECL ThreadEntryStub(void* pParam)
  424. {
  425. q_entry((int)pParam);
  426. return NULL;
  427. }
  428. void threads_InitCrit()
  429. {
  430. pthread_mutexattr_t mattrib;
  431. if (!my_mutex)
  432. {
  433. my_mutex = (pthread_mutex_t*)Alloc(sizeof(*my_mutex));
  434. if (pthread_mutexattr_init(&mattrib) == -1)
  435. {
  436. Error("pthread_mutex_attr_init failed");
  437. }
  438. if (pthread_mutex_init(my_mutex, &mattrib) == -1)
  439. {
  440. Error("pthread_mutex_init failed");
  441. }
  442. }
  443. }
  444. void threads_UninitCrit()
  445. {
  446. Free(my_mutex);
  447. my_mutex = NULL;
  448. }
  449. /*
  450. * =============
  451. * RunThreadsOn
  452. * =============
  453. */
  454. void RunThreadsOn(int workcnt, bool showpacifier, q_threadfunction func)
  455. {
  456. int i;
  457. pthread_t work_threads[MAX_THREADS];
  458. pthread_addr_t status;
  459. pthread_attr_t attrib;
  460. double start, end;
  461. threadstart = I_FloatTime();
  462. start = threadstart;
  463. for (i = 0; i < THREADTIMES_SIZE; i++)
  464. {
  465. threadtimes[i] = 0;
  466. }
  467. dispatch = 0;
  468. workcount = workcnt;
  469. oldf = -1;
  470. pacifier = showpacifier;
  471. threaded = true;
  472. q_entry = func;
  473. if (pacifier)
  474. {
  475. setbuf(stdout, NULL);
  476. }
  477. threads_InitCrit();
  478. if (pthread_attr_init(&attrib) == -1)
  479. {
  480. Error("pthread_attr_init failed");
  481. }
  482. #ifdef _POSIX_THREAD_ATTR_STACKSIZE
  483. if (pthread_attr_setstacksize(&attrib, 0x400000) == -1)
  484. {
  485. Error("pthread_attr_setstacksize failed");
  486. }
  487. #endif
  488. for (i = 0; i < g_numthreads; i++)
  489. {
  490. if (pthread_create(&work_threads[i], &attrib, ThreadEntryStub, (void*)i) == -1)
  491. {
  492. Error("pthread_create failed");
  493. }
  494. }
  495. for (i = 0; i < g_numthreads; i++)
  496. {
  497. if (pthread_join(work_threads[i], &status) == -1)
  498. {
  499. Error("pthread_join failed");
  500. }
  501. }
  502. threads_UninitCrit();
  503. q_entry = NULL;
  504. threaded = false;
  505. end = I_FloatTime();
  506. if (pacifier)
  507. {
  508. printf("\r%60s\r", "");
  509. }
  510. Log(" (%.2f seconds)\n", end - start);
  511. }
  512. #endif /*SYSTEM_POSIX */
  513. /*=
  514. | End SYSTEM_POSIX
  515. =====================*/
  516. #endif /*SINGLE_THREADED */
  517. /*====================
  518. | Begin SINGLE_THREADED
  519. =*/
  520. #ifdef SINGLE_THREADED
  521. int g_numthreads = 1;
  522. void ThreadSetPriority(q_threadpriority type)
  523. {
  524. }
  525. void threads_InitCrit()
  526. {
  527. }
  528. void threads_UninitCrit()
  529. {
  530. }
  531. void ThreadSetDefault()
  532. {
  533. g_numthreads = 1;
  534. }
  535. void ThreadLock()
  536. {
  537. }
  538. void ThreadUnlock()
  539. {
  540. }
  541. void RunThreadsOn(int workcnt, bool showpacifier, q_threadfunction func)
  542. {
  543. int i;
  544. double start, end;
  545. dispatch = 0;
  546. workcount = workcnt;
  547. oldf = -1;
  548. pacifier = showpacifier;
  549. threadstart = I_FloatTime();
  550. start = threadstart;
  551. for (i = 0; i < THREADTIMES_SIZE; i++)
  552. {
  553. threadtimes[i] = 0.0;
  554. }
  555. if (pacifier)
  556. {
  557. setbuf(stdout, NULL);
  558. }
  559. func(0);
  560. end = I_FloatTime();
  561. if (pacifier)
  562. {
  563. printf("\r%60s\r", "");
  564. }
  565. Log(" (%.2f seconds)\n", end - start);
  566. }
  567. #endif
  568. /*=
  569. | End SINGLE_THREADED
  570. =====================*/