1 /* sched.cc: scheduler interface for Cygwin
3 Copyright 2001, 2003, 2006, 2008, 2011 Red Hat, Inc.
5 Written by Robert Collins <rbtcollins@hotmail.com>
7 This file is part of Cygwin.
9 This software is a copyrighted work licensed under the terms of the
10 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
17 #include "miscfuncs.h"
24 extern "C" HWND WINAPI GetForegroundWindow();
26 /* Win32 priority to UNIX priority Mapping.
27 For now, I'm just following the spec: any range of priorities is ok.
28 There are probably many many issues with this...
30 We don't want process's going realtime. Well, they probably could, but the issues
31 with avoiding the priority values 17-22 and 27-30 (not supported before win2k)
32 make that inefficient.
33 However to complicate things most unixes use lower is better priorities.
35 So we map -14 to 15, and 15 to 1 via (16- ((n+16) >> 1))
36 we then map 1 to 15 to various process class and thread priority combinations
38 Then we need to look at the threads vi process priority. As win95 98 and NT 4
39 Don't support opening threads cross-process (unless a thread HANDLE is passed around)
40 for now, we'll just use the priority class.
42 The code and logic are present to calculate the priority for thread
43 , if a thread handle can be obtained. Alternatively, if the symbols wouldn't be
44 resolved until they are used
45 we could support this on windows 2000 and ME now, and just fall back to the
46 class only on pre win2000 machines.
48 Lastly, because we can't assume that the pid we're given are Windows pids, we can't
49 alter non-cygwin started programs.
55 /* max priority for policy */
57 sched_get_priority_max (int policy)
59 if (policy < 1 || policy > 3)
67 /* min priority for policy */
69 sched_get_priority_min (int policy)
71 if (policy < 1 || policy > 3)
79 /* Check a scheduler parameter struct for valid settings */
81 valid_sched_parameters (const struct sched_param *param)
83 if (param->sched_priority < -14 || param->sched_priority > 15)
91 /* get sched params for process
93 Note, I'm never returning EPERM,
94 Always ESRCH. This is by design (If cygwin ever looks at paranoid security
95 Walking the pid values is a known hole in some os's)
98 sched_getparam (pid_t pid, struct sched_param *param)
102 if (!param || pid < 0)
108 localpid = pid ? pid : getpid ();
122 process = OpenProcess (PROCESS_QUERY_INFORMATION, FALSE, p->dwProcessId);
128 Class = GetPriorityClass (process);
129 CloseHandle (process);
135 ThreadPriority = THREAD_PRIORITY_NORMAL;
137 /* calculate the unix priority.
139 FIXME: windows 2000 supports ABOVE_NORMAL and BELOW_NORMAL class's
140 So this logic just defaults those class factors to NORMAL in the calculations */
144 case IDLE_PRIORITY_CLASS:
145 switch (ThreadPriority)
147 case THREAD_PRIORITY_IDLE:
150 case THREAD_PRIORITY_LOWEST:
153 case THREAD_PRIORITY_BELOW_NORMAL:
156 case THREAD_PRIORITY_NORMAL:
159 case THREAD_PRIORITY_ABOVE_NORMAL:
162 case THREAD_PRIORITY_HIGHEST:
168 case HIGH_PRIORITY_CLASS:
169 switch (ThreadPriority)
171 case THREAD_PRIORITY_IDLE:
174 case THREAD_PRIORITY_LOWEST:
177 case THREAD_PRIORITY_BELOW_NORMAL:
180 case THREAD_PRIORITY_NORMAL:
183 case THREAD_PRIORITY_ABOVE_NORMAL:
186 case THREAD_PRIORITY_HIGHEST:
192 case NORMAL_PRIORITY_CLASS:
194 switch (ThreadPriority)
196 case THREAD_PRIORITY_IDLE:
199 case THREAD_PRIORITY_LOWEST:
202 case THREAD_PRIORITY_BELOW_NORMAL:
205 case THREAD_PRIORITY_NORMAL:
208 case THREAD_PRIORITY_ABOVE_NORMAL:
211 case THREAD_PRIORITY_HIGHEST:
219 /* reverse out winpri = (16- ((unixpri+16) >> 1)) */
221 winpri-16 = - (unixpri +16 ) >> 1
223 -(winpri-16) = unixpri +16 >> 1
224 (-(winpri-16)) << 1 = unixpri+16
225 ((-(winpri - 16)) << 1) - 16 = unixpri
228 param->sched_priority = ((-(winpri - 16)) << 1) - 16;
233 /* get the scheduler for pid
235 All process's on WIN32 run with SCHED_FIFO.
236 So we just give an answer.
237 (WIN32 uses a multi queue FIFO).
240 sched_getscheduler (pid_t pid)
248 /* get the time quantum for pid
250 Implemented only for NT systems, it fails and sets errno to ESRCH
254 sched_rr_get_interval (pid_t pid, struct timespec *interval)
256 static const char quantable[2][2][3] =
257 {{{12, 24, 36}, { 6, 12, 18}},
258 {{36, 36, 36}, {18, 18, 18}}};
259 /* FIXME: Clocktickinterval can be 15 ms for multi-processor system. */
260 static const int clocktickinterval = 10;
261 static const int quantapertick = 3;
265 DWORD vfindex, slindex, qindex, prisep;
268 forwin = GetForegroundWindow ();
270 GetWindowThreadProcessId (forwin, &forprocid);
274 reg_key reg (HKEY_LOCAL_MACHINE, KEY_READ, L"SYSTEM", L"CurrentControlSet",
275 L"Control", L"PriorityControl", NULL);
281 prisep = reg.get_dword (L"Win32PrioritySeparation", 2);
282 pinfo pi (pid ? pid : myself->pid);
289 if (pi->dwProcessId == forprocid)
292 qindex = qindex == 3 ? 2 : qindex;
296 vfindex = ((prisep >> 2) & 3) % 3;
298 vfindex = wincap.is_server () || (prisep & 3) == 0 ? 1 : 0;
301 slindex = ((prisep >> 4) & 3) % 3;
303 slindex = wincap.is_server () ? 1 : 0;
307 nsec = quantable[vfindex][slindex][qindex] / quantapertick
308 * clocktickinterval * 1000000;
309 interval->tv_sec = nsec / 1000000000;
310 interval->tv_nsec = nsec % 1000000000;
315 /* set the scheduling parameters */
317 sched_setparam (pid_t pid, const struct sched_param *param)
325 if (!param || pid < 0)
331 if (!valid_sched_parameters (param))
337 /* winpri = (16- ((unixpri+16) >> 1)) */
338 winpri = 16 - ((param->sched_priority + 16) >> 1);
340 /* calculate our desired priority class and thread priority */
343 Class = IDLE_PRIORITY_CLASS;
344 else if (winpri > 10)
345 Class = HIGH_PRIORITY_CLASS;
347 Class = NORMAL_PRIORITY_CLASS;
351 case IDLE_PRIORITY_CLASS:
355 ThreadPriority = THREAD_PRIORITY_IDLE;
358 ThreadPriority = THREAD_PRIORITY_LOWEST;
361 ThreadPriority = THREAD_PRIORITY_BELOW_NORMAL;
364 ThreadPriority = THREAD_PRIORITY_NORMAL;
367 ThreadPriority = THREAD_PRIORITY_ABOVE_NORMAL;
370 ThreadPriority = THREAD_PRIORITY_HIGHEST;
374 case NORMAL_PRIORITY_CLASS:
378 ThreadPriority = THREAD_PRIORITY_LOWEST;
381 ThreadPriority = THREAD_PRIORITY_BELOW_NORMAL;
384 ThreadPriority = THREAD_PRIORITY_NORMAL;
387 ThreadPriority = THREAD_PRIORITY_ABOVE_NORMAL;
390 ThreadPriority = THREAD_PRIORITY_HIGHEST;
394 case HIGH_PRIORITY_CLASS:
398 ThreadPriority = THREAD_PRIORITY_BELOW_NORMAL;
401 ThreadPriority = THREAD_PRIORITY_NORMAL;
404 ThreadPriority = THREAD_PRIORITY_ABOVE_NORMAL;
407 ThreadPriority = THREAD_PRIORITY_HIGHEST;
413 localpid = pid ? pid : getpid ();
421 set_errno (1); //ESRCH);
425 OpenProcess (PROCESS_SET_INFORMATION, FALSE, (DWORD) p->dwProcessId);
428 set_errno (2); //ESRCH);
431 if (!SetPriorityClass (process, Class))
433 CloseHandle (process);
437 CloseHandle (process);
442 /* we map -14 to 15, and 15 to 1 via (16- ((n+16) >> 1)). This lines up with the allowed
443 * valueswe return elsewhere in the sched* functions. We then map in groups of three to
444 * allowed thread priority's. The reason for dropping accuracy while still returning
445 * a wide range of values is to allow more flexible code in the future.
448 sched_set_thread_priority (HANDLE thread, int priority)
451 real_pri = 16 - ((priority + 16) >> 1);
452 if (real_pri <1 || real_pri > 15)
456 real_pri = THREAD_PRIORITY_LOWEST;
457 else if (real_pri < 7)
458 real_pri = THREAD_PRIORITY_BELOW_NORMAL;
459 else if (real_pri < 10)
460 real_pri = THREAD_PRIORITY_NORMAL;
461 else if (real_pri < 13)
462 real_pri = THREAD_PRIORITY_ABOVE_NORMAL;
464 real_pri = THREAD_PRIORITY_HIGHEST;
466 if (!SetThreadPriority (thread, real_pri))
467 /* invalid handle, no access are the only expected errors. */
472 /* set the scheduler */
474 sched_setscheduler (pid_t pid, int policy,
475 const struct sched_param *param)
477 /* on win32, you can't change the scheduler. Doh! */