nice.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. | https://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. #include <php.h>
  17. #include "nice.h"
  18. /*
  19. * Basic Windows implementation for the nice() function.
  20. *
  21. * This implementation uses SetPriorityClass() as a backend for defining
  22. * a process priority.
  23. *
  24. * The following values of inc, defines the value sent to SetPriorityClass():
  25. *
  26. * +-----------------------+-----------------------------+
  27. * | Expression | Priority type |
  28. * +-----------------------+-----------------------------+
  29. * | priority < -9 | HIGH_PRIORITY_CLASS |
  30. * +-----------------------+-----------------------------+
  31. * | priority < -4 | ABOVE_NORMAL_PRIORITY_CLASS |
  32. * +-----------------------+-----------------------------+
  33. * | priority > 4 | BELOW_NORMAL_PRIORITY_CLASS |
  34. * +-----------------------+-----------------------------+
  35. * | priority > 9 | IDLE_PRIORITY_CLASS |
  36. * +-----------------------+-----------------------------+
  37. *
  38. * If a value is between -4 and 4 (inclusive), then the priority will be set
  39. * to NORMAL_PRIORITY_CLASS.
  40. *
  41. * These values tries to mimic that of the UNIX version of nice().
  42. *
  43. * This is applied to the main process, not per thread, although this could
  44. * be implemented using SetThreadPriority() at one point.
  45. *
  46. * Note, the following priority classes are left out with intention:
  47. *
  48. * . REALTIME_PRIORITY_CLASS
  49. * Realtime priority class requires special system permissions to set, and
  50. * can be dangerous in certain cases.
  51. *
  52. * . PROCESS_MODE_BACKGROUND_BEGIN
  53. * . PROCESS_MODE_BACKGROUND_END
  54. * Process mode is not covered because it can easily forgotten to be changed
  55. * back and can cause unforeseen side effects that is hard to debug. Besides
  56. * that, these do generally not really fit into making a Windows somewhat
  57. * compatible nice() function.
  58. */
  59. PHPAPI int nice(zend_long p)
  60. {
  61. DWORD dwFlag = NORMAL_PRIORITY_CLASS;
  62. if (p < -9) {
  63. dwFlag = HIGH_PRIORITY_CLASS;
  64. } else if (p < -4) {
  65. dwFlag = ABOVE_NORMAL_PRIORITY_CLASS;
  66. } else if (p > 9) {
  67. dwFlag = IDLE_PRIORITY_CLASS;
  68. } else if (p > 4) {
  69. dwFlag = BELOW_NORMAL_PRIORITY_CLASS;
  70. }
  71. if (!SetPriorityClass(GetCurrentProcess(), dwFlag)) {
  72. return -1;
  73. }
  74. return 0;
  75. }