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.

245 lines
8.2 KiB

  1. #include "stdafx.h"
  2. #include <windows.h>
  3. /*
  4. * Priority.c
  5. *
  6. * This module contains the following routines
  7. * IsBelowNormalBasePriority : Checks is the current process is executing below Normal Base priority
  8. * ResetToNormalPriority : Resets current process to Normal priority
  9. * Main : Calling routine to test above 2 routines.
  10. *
  11. * Here is some MSDN data on the base priority of a process.
  12. *
  13. * Thread Priority
  14. * ===============
  15. * Base Priority
  16. *
  17. * The priority level of a thread is determined by both the priority class of
  18. * its process and its priority level. The priority class and priority level are
  19. * combined to form the base priority of each thread. The following table shows the
  20. * base priority levels for combinations of priority class and priority value.
  21. *
  22. * Process Priority Class Thread Priority Level
  23. * 1 IDLE_PRIORITY_CLASS THREAD_PRIORITY_IDLE
  24. * 1 BELOW_NORMAL_PRIORITY_CLASS THREAD_PRIORITY_IDLE
  25. * 1 NORMAL_PRIORITY_CLASS THREAD_PRIORITY_IDLE
  26. * 1 ABOVE_NORMAL_PRIORITY_CLASS THREAD_PRIORITY_IDLE
  27. * 1 HIGH_PRIORITY_CLASS THREAD_PRIORITY_IDLE
  28. * 2 IDLE_PRIORITY_CLASS THREAD_PRIORITY_LOWEST
  29. * 3 IDLE_PRIORITY_CLASS THREAD_PRIORITY_BELOW_NORMAL
  30. * 4 IDLE_PRIORITY_CLASS THREAD_PRIORITY_NORMAL
  31. * 4 BELOW_NORMAL_PRIORITY_CLASS THREAD_PRIORITY_LOWEST
  32. * 5 IDLE_PRIORITY_CLASS THREAD_PRIORITY_ABOVE_NORMAL
  33. * 5 BELOW_NORMAL_PRIORITY_CLASS THREAD_PRIORITY_BELOW_NORMAL
  34. * 5 Background NORMAL_PRIORITY_CLASS THREAD_PRIORITY_LOWEST
  35. * 6 IDLE_PRIORITY_CLASS THREAD_PRIORITY_HIGHEST
  36. * 6 BELOW_NORMAL_PRIORITY_CLASS THREAD_PRIORITY_NORMAL
  37. * 6 Background NORMAL_PRIORITY_CLASS THREAD_PRIORITY_BELOW_NORMAL
  38. * 7 BELOW_NORMAL_PRIORITY_CLASS THREAD_PRIORITY_ABOVE_NORMAL
  39. * 7 Background NORMAL_PRIORITY_CLASS THREAD_PRIORITY_NORMAL
  40. * 7 Foreground NORMAL_PRIORITY_CLASS THREAD_PRIORITY_LOWEST
  41. * 8 BELOW_NORMAL_PRIORITY_CLASS THREAD_PRIORITY_HIGHEST
  42. * 8 NORMAL_PRIORITY_CLASS THREAD_PRIORITY_ABOVE_NORMAL
  43. * 8 Foreground NORMAL_PRIORITY_CLASS THREAD_PRIORITY_BELOW_NORMAL
  44. * 8 ABOVE_NORMAL_PRIORITY_CLASS THREAD_PRIORITY_LOWEST
  45. * 9 NORMAL_PRIORITY_CLASS THREAD_PRIORITY_HIGHEST
  46. * 9 Foreground NORMAL_PRIORITY_CLASS THREAD_PRIORITY_NORMAL
  47. * 9 ABOVE_NORMAL_PRIORITY_CLASS THREAD_PRIORITY_BELOW_NORMAL
  48. * 10 Foreground NORMAL_PRIORITY_CLASS THREAD_PRIORITY_ABOVE_NORMAL
  49. * 10 ABOVE_NORMAL_PRIORITY_CLASS THREAD_PRIORITY_NORMAL
  50. * 11 Foreground NORMAL_PRIORITY_CLASS THREAD_PRIORITY_HIGHEST
  51. * 11 ABOVE_NORMAL_PRIORITY_CLASS THREAD_PRIORITY_ABOVE_NORMAL
  52. * 11 HIGH_PRIORITY_CLASS THREAD_PRIORITY_LOWEST
  53. * 12 ABOVE_NORMAL_PRIORITY_CLASS THREAD_PRIORITY_HIGHEST
  54. * 12 HIGH_PRIORITY_CLASS THREAD_PRIORITY_BELOW_NORMAL
  55. * 13 HIGH_PRIORITY_CLASS THREAD_PRIORITY_NORMAL
  56. * 14 HIGH_PRIORITY_CLASS THREAD_PRIORITY_ABOVE_NORMAL
  57. * 15 HIGH_PRIORITY_CLASS THREAD_PRIORITY_HIGHEST
  58. * 15 HIGH_PRIORITY_CLASS THREAD_PRIORITY_TIME_CRITICAL
  59. * 15 IDLE_PRIORITY_CLASS THREAD_PRIORITY_TIME_CRITICAL
  60. * 15 BELOW_NORMAL_PRIORITY_CLASS THREAD_PRIORITY_TIME_CRITICAL
  61. * 15 NORMAL_PRIORITY_CLASS THREAD_PRIORITY_TIME_CRITICAL
  62. * 15 ABOVE_NORMAL_PRIORITY_CLASS THREAD_PRIORITY_TIME_CRITICAL
  63. * 16 REALTIME_PRIORITY_CLASS THREAD_PRIORITY_IDLE
  64. * 22 REALTIME_PRIORITY_CLASS THREAD_PRIORITY_LOWEST
  65. * 23 REALTIME_PRIORITY_CLASS THREAD_PRIORITY_BELOW_NORMAL
  66. * 24 REALTIME_PRIORITY_CLASS THREAD_PRIORITY_NORMAL
  67. * 25 REALTIME_PRIORITY_CLASS THREAD_PRIORITY_ABOVE_NORMAL
  68. * 26 REALTIME_PRIORITY_CLASS THREAD_PRIORITY_HIGHEST
  69. * 31 REALTIME_PRIORITY_CLASS THREAD_PRIORITY_TIME_CRITICAL
  70. *
  71. */
  72. //
  73. // ------------------------------
  74. // | IsBelowNormalBasePriority() |
  75. // ------------------------------
  76. //
  77. // COPYRIGHT� 2001 Microsoft Corporation and Executive Software International, Inc.
  78. //
  79. // Description:
  80. // Determine whether the current thread running below the normal priority or not.
  81. //
  82. // Caller must have a PROCESS_QUERY_INFORMATION access to the process/ thread.
  83. //
  84. // PDL
  85. //
  86. // This routine taking normal base priority as "Base Priority level 7". ie
  87. // combination of NORMAL_PRIORITY_CLASS and THREAD_PRIORITY_NORMAL (background)
  88. //
  89. // These are the conditions that we can use to determine whether the
  90. // thread running at >= NORMAL priority or not.
  91. //
  92. // 1) All the thread running under IDLE_PRIORITY_CLASS has got base priority of 1 except
  93. // the thread which has the priority of THREAD_PRIORITY_TIME_CRITICAL.
  94. //
  95. // 2) The thread belongs to a BELOW_NORMAL_PRIORITY_CLASS process should run at
  96. // THREAD_PRIORITY_ABOVE_NORMAL or higher priority to get the NORMAL base priority.
  97. //
  98. // 3) All the Threads, which are running at THRED_PRIORITY_IDLE has a base priority of less than 7
  99. // except those threads are in the REALTIME_PRIORITY_CLASS process.
  100. //
  101. // 4) If the process running at NORMAL_PRIORITY_CLASS then the thread should run at
  102. // THREAD_PRIORITY_NORMAL or above.
  103. //
  104. // Calling Sequence:
  105. // None.
  106. //
  107. // Inputs:
  108. // None.
  109. //
  110. // Outputs:
  111. // None.
  112. // Return TRUE if the current thread's Base priority is below normal.
  113. // otherwise return FALSE
  114. //
  115. // Error Handling:
  116. // Error not handled, returns FALSE on error
  117. //
  118. //
  119. // Global Effects:
  120. // None
  121. BOOL
  122. IsBelowNormalBasePriority()
  123. {
  124. DWORD PriorityClass, ThreadPriority;
  125. BOOL bBelowNormal = FALSE;
  126. do {
  127. //
  128. // get the Process Priority class and the thread priority
  129. //
  130. PriorityClass = GetPriorityClass(GetCurrentProcess());
  131. if (!PriorityClass ) break;
  132. ThreadPriority = GetThreadPriority(GetCurrentThread());
  133. if (THREAD_PRIORITY_ERROR_RETURN == ThreadPriority) break;
  134. bBelowNormal = TRUE;
  135. //
  136. // Check for the 4 cases of running below Normal base priority
  137. //
  138. //
  139. // 1) Check for threads running under IDLE_PRIORITY_CLASS has got base priority of 1 except
  140. // the thread which has the priority of THREAD_PRIORITY_TIME_CRITICAL.
  141. if (IDLE_PRIORITY_CLASS == PriorityClass &&
  142. THREAD_PRIORITY_TIME_CRITICAL != ThreadPriority ) break;
  143. //
  144. // 2) Check threads that belongs to a BELOW_NORMAL_PRIORITY_CLASS process should run at
  145. // THREAD_PRIORITY_ABOVE_NORMAL or higher priority to get the NORMAL base priority.
  146. if (BELOW_NORMAL_PRIORITY_CLASS == PriorityClass &&
  147. THREAD_PRIORITY_NORMAL >= ThreadPriority ) break;
  148. //
  149. // 3) Check all the Threads, which are running at THREAD_PRIORITY_IDLE has a base priority of less than 7
  150. // except those threads are in the REALTIME_PRIORITY_CLASS process.
  151. if (THREAD_PRIORITY_IDLE == ThreadPriority &&
  152. REALTIME_PRIORITY_CLASS != PriorityClass ) break;
  153. //
  154. // 4) Check if the process is running at NORMAL_PRIORITY_CLASS then the thread should run at
  155. // THREAD_PRIORITY_NORMAL or above.
  156. if (NORMAL_PRIORITY_CLASS == PriorityClass &&
  157. THREAD_PRIORITY_NORMAL >= ThreadPriority ) break;
  158. //
  159. // If it did not meet the above criteria, then it must be running at Normal base priority or above
  160. bBelowNormal = FALSE;
  161. }while(0);
  162. return bBelowNormal;
  163. }
  164. //
  165. // --------------------------
  166. // | ResetToNormalPriority() |
  167. // --------------------------
  168. //
  169. // COPYRIGHT� 2001 Microsoft Corporation and Executive Software International, Inc.
  170. //
  171. // Description:
  172. // Sets Current Thread's Base priority to Normal
  173. //
  174. // Caller should have PROCESS_SET_INFORMATION and THREAD_SET_INFORMATION access right
  175. // for the current process / current thread..
  176. //
  177. // PDL
  178. //
  179. // Resetting base priority by setting Priority Class and Thread Priority
  180. // to NORMAL.
  181. //
  182. // Calling Sequence:
  183. // None.
  184. //
  185. // Inputs:
  186. // None.
  187. //
  188. // Outputs:
  189. // None.
  190. // returns FALSE on error. return TRUE on success
  191. //
  192. // Error Handling:
  193. // return FALSE on error
  194. //
  195. // Global Effects:
  196. // The process priority will reset to NORMAL_PRIORITY_CLASS
  197. // and current thread's priority to THREAD_PRIORITY_NORMAL.
  198. //
  199. BOOL
  200. ResetToNormalPriority()
  201. {
  202. if (!SetPriorityClass(GetCurrentProcess(),NORMAL_PRIORITY_CLASS)) return FALSE;
  203. if (!SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_NORMAL)) return FALSE;
  204. return TRUE;
  205. }
  206. #ifdef TEST_PRIORITY_MODULE
  207. int
  208. main()
  209. {
  210. for (;;) {
  211. Sleep(4000);
  212. if (IsBelowNormalBasePriority()) {
  213. ResetToNormalPriority();
  214. }
  215. }
  216. return 0;
  217. }
  218. #endif