2016-10-16 10:17:35 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2019-01-30 17:03:12 +08:00
|
|
|
| Copyright (c) The PHP Group |
|
2016-10-16 10:17:35 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available through the world-wide-web at the following url: |
|
2021-05-06 18:16:35 +08:00
|
|
|
| https://www.php.net/license/3_01.txt |
|
2016-10-16 10:17:35 +08:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Kalle Sommer Nielsen <kalle@php.net> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <php.h>
|
|
|
|
#include "nice.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Basic Windows implementation for the nice() function.
|
|
|
|
*
|
2017-12-31 12:35:25 +08:00
|
|
|
* This implementation uses SetPriorityClass() as a backend for defining
|
2016-10-16 10:17:35 +08:00
|
|
|
* a process priority.
|
|
|
|
*
|
|
|
|
* The following values of inc, defines the value sent to SetPriorityClass():
|
|
|
|
*
|
|
|
|
* +-----------------------+-----------------------------+
|
|
|
|
* | Expression | Priority type |
|
|
|
|
* +-----------------------+-----------------------------+
|
2016-10-19 05:25:59 +08:00
|
|
|
* | priority < -9 | HIGH_PRIORITY_CLASS |
|
2016-10-16 10:17:35 +08:00
|
|
|
* +-----------------------+-----------------------------+
|
2016-10-19 05:25:59 +08:00
|
|
|
* | priority < -4 | ABOVE_NORMAL_PRIORITY_CLASS |
|
2016-10-16 10:17:35 +08:00
|
|
|
* +-----------------------+-----------------------------+
|
2016-10-19 05:25:59 +08:00
|
|
|
* | priority > 4 | BELOW_NORMAL_PRIORITY_CLASS |
|
2016-10-16 10:17:35 +08:00
|
|
|
* +-----------------------+-----------------------------+
|
2016-10-19 05:25:59 +08:00
|
|
|
* | priority > 9 | IDLE_PRIORITY_CLASS |
|
2016-10-16 10:17:35 +08:00
|
|
|
* +-----------------------+-----------------------------+
|
|
|
|
*
|
2016-10-19 05:25:59 +08:00
|
|
|
* If a value is between -4 and 4 (inclusive), then the priority will be set
|
|
|
|
* to NORMAL_PRIORITY_CLASS.
|
2016-10-16 11:21:30 +08:00
|
|
|
*
|
2016-10-19 05:25:59 +08:00
|
|
|
* These values tries to mimic that of the UNIX version of nice().
|
2016-10-16 11:21:30 +08:00
|
|
|
*
|
2017-12-31 12:35:25 +08:00
|
|
|
* This is applied to the main process, not per thread, although this could
|
2016-10-16 10:17:35 +08:00
|
|
|
* be implemented using SetThreadPriority() at one point.
|
2016-11-19 07:32:07 +08:00
|
|
|
*
|
2016-11-19 17:10:48 +08:00
|
|
|
* Note, the following priority classes are left out with intention:
|
|
|
|
*
|
|
|
|
* . REALTIME_PRIORITY_CLASS
|
2017-12-31 12:35:25 +08:00
|
|
|
* Realtime priority class requires special system permissions to set, and
|
2016-11-19 17:10:48 +08:00
|
|
|
* can be dangerous in certain cases.
|
|
|
|
*
|
|
|
|
* . PROCESS_MODE_BACKGROUND_BEGIN
|
|
|
|
* . PROCESS_MODE_BACKGROUND_END
|
2017-12-31 12:35:25 +08:00
|
|
|
* Process mode is not covered because it can easily forgotten to be changed
|
2020-01-16 19:04:00 +08:00
|
|
|
* back and can cause unforeseen side effects that is hard to debug. Besides
|
2016-11-19 17:10:48 +08:00
|
|
|
* that, these do generally not really fit into making a Windows somewhat
|
|
|
|
* compatible nice() function.
|
2016-10-16 10:17:35 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
PHPAPI int nice(zend_long p)
|
|
|
|
{
|
|
|
|
DWORD dwFlag = NORMAL_PRIORITY_CLASS;
|
|
|
|
|
2017-12-31 12:35:25 +08:00
|
|
|
if (p < -9) {
|
2016-10-16 10:17:35 +08:00
|
|
|
dwFlag = HIGH_PRIORITY_CLASS;
|
2016-10-19 05:25:59 +08:00
|
|
|
} else if (p < -4) {
|
2016-10-16 10:17:35 +08:00
|
|
|
dwFlag = ABOVE_NORMAL_PRIORITY_CLASS;
|
2016-10-19 05:25:59 +08:00
|
|
|
} else if (p > 9) {
|
2016-10-16 10:17:35 +08:00
|
|
|
dwFlag = IDLE_PRIORITY_CLASS;
|
2016-10-19 05:25:59 +08:00
|
|
|
} else if (p > 4) {
|
2016-10-16 11:21:30 +08:00
|
|
|
dwFlag = BELOW_NORMAL_PRIORITY_CLASS;
|
2016-10-16 10:17:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetPriorityClass(GetCurrentProcess(), dwFlag)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|