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.

146 lines
3.4 KiB

  1. /*
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. DisableBoostThread.cpp
  5. Abstract:
  6. DisableBoostThread disables the autoboost that threads get when they
  7. unblocked. The NT scheduler will normally temporarily boost a thread
  8. when the synchronization object gets release. 9X does not: it only check
  9. if there is a higher priority thread.
  10. This was first written for Hijaak: besied its many memory bugs, as a race
  11. condition between its worker thread and its main thread. See b#379504 for details.
  12. History:
  13. 06/28/2001 pierreys Created
  14. */
  15. #include "precomp.h"
  16. IMPLEMENT_SHIM_BEGIN(DisableBoostThread)
  17. #include "ShimHookMacro.h"
  18. APIHOOK_ENUM_BEGIN
  19. APIHOOK_ENUM_ENTRY(CreateThread)
  20. APIHOOK_ENUM_END
  21. HANDLE
  22. APIHOOK(CreateThread)(
  23. LPSECURITY_ATTRIBUTES lpsa,
  24. DWORD cbStack,
  25. LPTHREAD_START_ROUTINE lpStartAddress,
  26. LPVOID lpvThreadParm,
  27. DWORD fdwCreate,
  28. LPDWORD lpIDThread
  29. )
  30. {
  31. HANDLE hThread;
  32. //
  33. // Call the original API
  34. //
  35. hThread=ORIGINAL_API(CreateThread)(
  36. lpsa,
  37. cbStack,
  38. lpStartAddress,
  39. lpvThreadParm,
  40. fdwCreate,
  41. lpIDThread
  42. );
  43. if (hThread!=NULL)
  44. {
  45. //
  46. // We are disabling (rather weird, but TRUE means disabling)
  47. // the autoboost a thread gets for unblocking.
  48. //
  49. SetThreadPriorityBoost(hThread, TRUE);
  50. }
  51. return(hThread);
  52. }
  53. BOOL
  54. NOTIFY_FUNCTION(
  55. DWORD fdwReason
  56. )
  57. {
  58. if (fdwReason==DLL_PROCESS_ATTACH)
  59. {
  60. CSTRING_TRY
  61. {
  62. BOOL fBoostMainThread=FALSE;
  63. CString csCl(COMMAND_LINE);
  64. CStringParser csParser(csCl, L" ");
  65. int argc = csParser.GetCount();
  66. for (int i = 0; i < argc; ++i)
  67. {
  68. if (csParser[i] == L"+LowerMainThread")
  69. {
  70. DPFN( eDbgLevelSpew, "LowerMainThread Selected");
  71. //
  72. // Unboost the main thread to make sure it runs first.
  73. //
  74. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST);
  75. }
  76. else if (csParser[i] == L"+HigherMainThread")
  77. {
  78. DPFN( eDbgLevelSpew, "HigherMainThread Selected");
  79. //
  80. // Boost the main thread to make sure it runs first.
  81. //
  82. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
  83. }
  84. else if (csParser[i] == L"+BoostMainThread")
  85. {
  86. DPFN( eDbgLevelSpew, "HigherMainThread Selected");
  87. fBoostMainThread = TRUE;
  88. }
  89. else
  90. {
  91. DPFN( eDbgLevelError, "Ignoring unknown command:%S", csParser[i].Get());
  92. }
  93. }
  94. if (!fBoostMainThread)
  95. {
  96. //
  97. // We are disabling (rather weird, but TRUE means disabling)
  98. // the autoboost a thread gets for unblocking.
  99. //
  100. SetThreadPriorityBoost(GetCurrentThread(), TRUE);
  101. }
  102. }
  103. CSTRING_CATCH
  104. {
  105. DPFN( eDbgLevelError, "String error, ignoring command line");
  106. }
  107. }
  108. return TRUE;
  109. }
  110. HOOK_BEGIN
  111. CALL_NOTIFY_FUNCTION
  112. APIHOOK_ENTRY(KERNEL32.DLL, CreateThread)
  113. HOOK_END
  114. IMPLEMENT_SHIM_END