Skip to content

Commit 704a208

Browse files
committed
fix: address QA round 1
Windows: never raise an IDLE-class worker to BELOW_NORMAL. Test: also start the child nicer than the helper's level and assert it is left there, so the never-raise branch is exercised and the test cannot pass vacuously under an already-niced runner. Signed-off-by: Shane McCarron <[email protected]>
1 parent 7735622 commit 704a208

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

‎src/foundation/platform.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,12 @@ const char *cbm_resolve_cache_dir(void) {
738738

739739
bool cbm_lower_cpu_priority(void) {
740740
#ifdef _WIN32
741-
return SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS) != 0;
741+
HANDLE self = GetCurrentProcess();
742+
DWORD current = GetPriorityClass(self);
743+
if (current == IDLE_PRIORITY_CLASS || current == BELOW_NORMAL_PRIORITY_CLASS) {
744+
return true; /* already at least this low: never raise priority */
745+
}
746+
return SetPriorityClass(self, BELOW_NORMAL_PRIORITY_CLASS) != 0;
742747
#else
743748
/* getpriority can legitimately return -1, so errno is the only error signal. */
744749
errno = 0;

‎tests/test_platform.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,24 +1456,34 @@ TEST(platform_lower_cpu_priority_nices_the_process) {
14561456
pid_t child = fork();
14571457
if (child == 0) {
14581458
(void)close(fds[0]);
1459-
int report[2] = {cbm_lower_cpu_priority() ? 1 : 0, 0};
1459+
/* report: lowered ok, nice after; then, from a nicer start, ok, target, nice after */
1460+
int report[5] = {cbm_lower_cpu_priority() ? 1 : 0, 0, 0, 0, 0};
14601461
errno = 0;
14611462
report[1] = getpriority(PRIO_PROCESS, 0);
14621463
if (errno != 0) {
14631464
report[0] = 0;
14641465
}
1466+
/* Never-raise leg: start nicer than the helper's level; it must leave it. */
1467+
report[3] = report[1] >= 15 ? 19 : 15;
1468+
if (setpriority(PRIO_PROCESS, 0, report[3]) == 0) {
1469+
report[2] = cbm_lower_cpu_priority() ? 1 : 0;
1470+
errno = 0;
1471+
report[4] = getpriority(PRIO_PROCESS, 0);
1472+
}
14651473
_exit(write(fds[1], report, sizeof(report)) == (ssize_t)sizeof(report) ? 0 : 1);
14661474
}
14671475
ASSERT_TRUE(child > 0);
14681476
(void)close(fds[1]);
1469-
int report[2] = {0, 0};
1477+
int report[5] = {0, 0, 0, 0, 0};
14701478
ssize_t got = read(fds[0], report, sizeof(report));
14711479
(void)close(fds[0]);
14721480
int status = 0;
14731481
(void)waitpid(child, &status, 0);
14741482
ASSERT_EQ(got, (ssize_t)sizeof(report));
14751483
ASSERT_EQ(report[0], 1);
14761484
ASSERT_TRUE(report[1] >= CBM_LOW_CPU_PRIORITY_NICE);
1485+
ASSERT_EQ(report[2], 1);
1486+
ASSERT_EQ(report[4], report[3]);
14771487
PASS();
14781488
#endif
14791489
}

0 commit comments

Comments
 (0)