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