Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
2.6 KiB

  1. // Copyright (C) 1996-1997 Microsoft Corporation. All rights reserved.
  2. #include "header.h"
  3. static DWORD thrdID;
  4. static HANDLE hThrd;
  5. static HANDLE hsemThread;
  6. static int ThreadPriority;
  7. static THRD_COMMAND ThreadCommand;
  8. static void* pThreadParam;
  9. static DWORD WINAPI WorkerThread(LPVOID pParam);
  10. BOOL ActivateThread(THRD_COMMAND cmd, void* pParam, int priority)
  11. {
  12. if (!hThrd) {
  13. if (cmd == THRD_TERMINATE) // don't start the thread just to kill it
  14. return FALSE;
  15. if (!hsemThread)
  16. hsemThread = CreateSemaphore(NULL, 0, 1, NULL);
  17. hThrd = CreateThread(NULL, 0, &WorkerThread, NULL,
  18. 0, &thrdID);
  19. if (!hThrd) {
  20. OOM();
  21. return FALSE;
  22. }
  23. SetThreadPriority(hThrd, ThreadPriority = priority);
  24. }
  25. if (g_fThreadRunning)
  26. return FALSE;
  27. if (g_fDualCPU == -1) { // haven't initialized it yet
  28. HKEY hkey;
  29. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  30. "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\1", 0,
  31. KEY_READ, &hkey) == ERROR_SUCCESS) {
  32. g_fDualCPU = TRUE;
  33. RegCloseKey(hkey);
  34. }
  35. else
  36. g_fDualCPU = FALSE;
  37. }
  38. // For multiple CPU's, we can use a normal priority for threads
  39. if (g_fDualCPU && (priority == THREAD_PRIORITY_IDLE ||
  40. priority == THREAD_PRIORITY_LOWEST ||
  41. priority == THREAD_PRIORITY_BELOW_NORMAL))
  42. priority = THREAD_PRIORITY_NORMAL;
  43. if (ThreadPriority != priority)
  44. SetThreadPriority(hThrd, ThreadPriority = priority);
  45. ThreadCommand = cmd;
  46. pThreadParam = pParam;
  47. g_fThreadRunning = TRUE;
  48. ReleaseSemaphore(hsemThread, 1, NULL);
  49. return TRUE;
  50. }
  51. /***************************************************************************
  52. FUNCTION: WaitForThread
  53. PURPOSE: Find out if the worker thread is running. If it is, and
  54. the command is anything other then THRD_ANY, then kick
  55. up the thread priority and wait for it to finish.
  56. PARAMETERS:
  57. cmd
  58. RETURNS:
  59. COMMENTS:
  60. MODIFICATION DATES:
  61. 04-Jun-1997 [ralphw]
  62. ***************************************************************************/
  63. BOOL WaitForThread(THRD_COMMAND cmd)
  64. {
  65. if (!g_fThreadRunning)
  66. return TRUE;
  67. else if (cmd != THRD_ANY && cmd != ThreadCommand)
  68. return FALSE;
  69. // Kick the thread to a high priority, and wait for it to complete
  70. SetThreadPriority(hThrd, THREAD_PRIORITY_HIGHEST);
  71. {
  72. CHourGlass waitcur;
  73. while (g_fThreadRunning)
  74. Sleep(200);
  75. }
  76. SetThreadPriority(hThrd, ThreadPriority);
  77. return TRUE;
  78. }
  79. static DWORD WINAPI WorkerThread(LPVOID pParam)
  80. {
  81. for (;;) {
  82. if (WaitForSingleObject(hsemThread, INFINITE) != WAIT_OBJECT_0)
  83. return (UINT) -1;
  84. switch (ThreadCommand) {
  85. case THRD_TERMINATE:
  86. g_fThreadRunning = FALSE;
  87. thrdID = 0;
  88. hThrd = NULL;
  89. ExitThread(0);
  90. break;
  91. }
  92. g_fThreadRunning = FALSE;
  93. }
  94. }