nice.c 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include <php.h>
  19. #include "nice.h"
  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 unforseen 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. PHPAPI int nice(zend_long p)
  62. {
  63. DWORD dwFlag = NORMAL_PRIORITY_CLASS;
  64. if (p < -9) {
  65. dwFlag = HIGH_PRIORITY_CLASS;
  66. } else if (p < -4) {
  67. dwFlag = ABOVE_NORMAL_PRIORITY_CLASS;
  68. } else if (p > 9) {
  69. dwFlag = IDLE_PRIORITY_CLASS;
  70. } else if (p > 4) {
  71. dwFlag = BELOW_NORMAL_PRIORITY_CLASS;
  72. }
  73. if (!SetPriorityClass(GetCurrentProcess(), dwFlag)) {
  74. return -1;
  75. }
  76. return 0;
  77. }
  78. /*
  79. * Local variables:
  80. * tab-width: 4
  81. * c-basic-offset: 4
  82. * End:
  83. * vim600: sw=4 ts=4 fdm=marker
  84. * vim<600: sw=4 ts=4
  85. */