@@ -104,7 +104,7 @@ yield ()
104104}
105105
106106/*
107- Mapping of nice value from/to Windows priority
107+ Mapping of nice value or sched_priority from/to Windows priority
108108 ('batch' is used for SCHED_BATCH policy).
109109
110110 nice_to_winprio() winprio_to_nice()
@@ -115,6 +115,14 @@ yield ()
115115 -12...-5 -13..-19 3 ABOVE_NORMAL_PRIORITY_CLASS -8 -16
116116 -13..-19 -20 4 HIGH_PRIORITY_CLASS -16 -20
117117 -20 - 5 REALTIME_PRIORITY_CLASS -20 -20
118+
119+ schedprio_to_winprio() winprio_to_schedprio()
120+ 1....6 0 IDLE_PRIORITY_CLASS 3
121+ 7...12 1 BELOW_NORMAL_PRIORITY_CLASS 9
122+ 13...18 2 NORMAL_PRIORITY_CLASS 15
123+ 19...24 3 ABOVE_NORMAL_PRIORITY_CLASS 21
124+ 25...30 4 HIGH_PRIORITY_CLASS 27
125+ 31...32 5 REALTIME_PRIORITY_CLASS 32
118126*/
119127
120128/* *_PRIORITY_CLASS -> 0...5 */
@@ -167,9 +175,25 @@ nice_to_winprio_impl (int nice, bool batch = false)
167175 return level_to_winprio (level);
168176}
169177
178+ /* *_PRIORITY_CLASS -> sched_priority */
179+ constexpr int
180+ winprio_to_schedprio_impl (DWORD prio)
181+ {
182+ int level = winprio_to_level (prio);
183+ return (level < 5 ? 3 + level * 6 : 32 );
184+ }
185+
186+ /* sched_priority -> *_PRIORITY_CLASS */
187+ constexpr DWORD
188+ schedprio_to_winprio_impl (int schedprio)
189+ {
190+ int level = (schedprio <= 1 ? 0 : (schedprio < 32 ? (schedprio - 1 ) / 6 : 5 ));
191+ return level_to_winprio (level);
192+ }
193+
170194/* Check consistency at compile time. */
171195constexpr bool
172- check_nice_winprio_mapping ()
196+ check_nice_schedprio_winprio_mapping ()
173197{
174198 for (int nice = -NZERO; nice < NZERO; nice++)
175199 for (int batch = 0 ; batch <= 1 ; batch++) {
@@ -179,16 +203,28 @@ check_nice_winprio_mapping ()
179203 if (prio != prio2)
180204 return false ;
181205 }
206+ for (int schedprio = 1 ; schedprio <= 32 ; schedprio++)
207+ {
208+ DWORD prio = schedprio_to_winprio_impl (schedprio);
209+ int schedprio2 = winprio_to_schedprio_impl (prio);
210+ DWORD prio2 = schedprio_to_winprio_impl (schedprio2);
211+ if (prio != prio2)
212+ return false ;
213+ }
182214 return true ;
183215}
184216
185- static_assert (check_nice_winprio_mapping ());
217+ static_assert (check_nice_schedprio_winprio_mapping ());
186218static_assert (nice_to_winprio_impl(NZERO-1 , false ) == IDLE_PRIORITY_CLASS);
187219static_assert (nice_to_winprio_impl(0 , true ) == BELOW_NORMAL_PRIORITY_CLASS);
188220static_assert (winprio_to_nice_impl(BELOW_NORMAL_PRIORITY_CLASS, true ) == 0 );
189221static_assert (nice_to_winprio_impl(0 , false ) == NORMAL_PRIORITY_CLASS);
190222static_assert (winprio_to_nice_impl(NORMAL_PRIORITY_CLASS, false ) == 0 );
191223static_assert (nice_to_winprio_impl(-NZERO, false ) == REALTIME_PRIORITY_CLASS);
224+ static_assert (schedprio_to_winprio_impl(1 ) == IDLE_PRIORITY_CLASS);
225+ static_assert (schedprio_to_winprio_impl(15 ) == NORMAL_PRIORITY_CLASS);
226+ static_assert (winprio_to_schedprio_impl(NORMAL_PRIORITY_CLASS) == 15 );
227+ static_assert (schedprio_to_winprio_impl(32 ) == REALTIME_PRIORITY_CLASS);
192228
193229/* Get a default value for the nice factor. */
194230int
@@ -210,6 +246,20 @@ nice_to_winprio (int &nice, bool batch /* = false */)
210246 return nice_to_winprio_impl (nice, batch);
211247}
212248
249+ /* Get a default sched_priority from a Win32 priority. */
250+ int
251+ winprio_to_schedprio (DWORD prio)
252+ {
253+ return winprio_to_schedprio_impl (prio);
254+ }
255+
256+ /* Get a Win32 priority matching the sched_priority. */
257+ DWORD
258+ schedprio_to_winprio (int schedprio)
259+ {
260+ return schedprio_to_winprio_impl (schedprio);
261+ }
262+
213263/* Set Win32 priority or return false on failure. Also return
214264 false and revert to the original priority if a different (lower)
215265 priority is set instead. Always revert to original priority if
0 commit comments