xref: /PHP-8.0/win32/nice.c (revision c3cf01b5)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.php.net/license/3_01.txt                                  |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Kalle Sommer Nielsen <kalle@php.net>                        |
14    +----------------------------------------------------------------------+
15  */
16 
17 #include <php.h>
18 #include "nice.h"
19 
20 /*
21  * Basic Windows implementation for the nice() function.
22  *
23  * This implementation uses SetPriorityClass() as a backend for defining
24  * a process priority.
25  *
26  * The following values of inc, defines the value sent to SetPriorityClass():
27  *
28  *  +-----------------------+-----------------------------+
29  *  | Expression            | Priority type                |
30  *  +-----------------------+-----------------------------+
31  *  | priority < -9         | HIGH_PRIORITY_CLASS          |
32  *  +-----------------------+-----------------------------+
33  *  | priority < -4         | ABOVE_NORMAL_PRIORITY_CLASS  |
34  *  +-----------------------+-----------------------------+
35  *  | priority > 4          | BELOW_NORMAL_PRIORITY_CLASS  |
36  *  +-----------------------+-----------------------------+
37  *  | priority > 9          | IDLE_PRIORITY_CLASS          |
38  *  +-----------------------+-----------------------------+
39  *
40  * If a value is between -4 and 4 (inclusive), then the priority will be set
41  * to NORMAL_PRIORITY_CLASS.
42  *
43  * These values tries to mimic that of the UNIX version of nice().
44  *
45  * This is applied to the main process, not per thread, although this could
46  * be implemented using SetThreadPriority() at one point.
47  *
48  * Note, the following priority classes are left out with intention:
49  *
50  *   . REALTIME_PRIORITY_CLASS
51  *     Realtime priority class requires special system permissions to set, and
52  *     can be dangerous in certain cases.
53  *
54  *   . PROCESS_MODE_BACKGROUND_BEGIN
55  *   . PROCESS_MODE_BACKGROUND_END
56  *     Process mode is not covered because it can easily forgotten to be changed
57  *     back and can cause unforeseen side effects that is hard to debug. Besides
58  *     that, these do generally not really fit into making a Windows somewhat
59  *     compatible nice() function.
60  */
61 
nice(zend_long p)62 PHPAPI int nice(zend_long p)
63 {
64 	DWORD dwFlag = NORMAL_PRIORITY_CLASS;
65 
66 	if (p < -9) {
67 		dwFlag = HIGH_PRIORITY_CLASS;
68 	} else if (p < -4) {
69 		dwFlag = ABOVE_NORMAL_PRIORITY_CLASS;
70 	} else if (p > 9) {
71 		dwFlag = IDLE_PRIORITY_CLASS;
72 	} else if (p > 4) {
73 		dwFlag = BELOW_NORMAL_PRIORITY_CLASS;
74 	}
75 
76 	if (!SetPriorityClass(GetCurrentProcess(), dwFlag)) {
77 		return -1;
78 	}
79 
80 	return 0;
81 }
82