okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
2.0 KiB43 linesraw
1// io_priority_test (#4733): on Windows a process that starts at Low I/O priority is put back to Normal; elsewhere the call does nothing.
2#include "strata/platform/io_priority.hpp"
3
4#include <cstdio>
5#include <cstdlib>
6
7#if defined(_WIN32)
8#include <windows.h>
9static unsigned long io_prio() {
10    using Query = LONG(NTAPI*)(HANDLE, ULONG, PVOID, ULONG, PULONG);
11    auto q = reinterpret_cast<Query>(reinterpret_cast<void*>(GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "NtQueryInformationProcess")));
12    ULONG v = 99, r = 0;
13    return q != nullptr && q(GetCurrentProcess(), 33, &v, sizeof v, &r) == 0 ? v : 99;
14}
15static bool set_io_prio(ULONG v) {
16    using Set = LONG(NTAPI*)(HANDLE, ULONG, PVOID, ULONG);
17    auto s = reinterpret_cast<Set>(reinterpret_cast<void*>(GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "NtSetInformationProcess")));
18    return s != nullptr && s(GetCurrentProcess(), 33, &v, sizeof v) == 0;
19}
20#endif
21
22int main() {
23#if defined(_WIN32)
24    int bad = 0;
25    // 1. started at Low: back to Normal, and the call reports what it found
26    if (!set_io_prio(1)) { std::printf("io_priority_test: SKIP (cannot lower the I/O priority here)\n"); return 77; }
27    int was = strata::platform::restore_normal_io_priority(stderr);
28    if (was != 1 || io_prio() != 2) { std::printf("FAIL lowered: was=%d now=%lu\n", was, io_prio()); ++bad; }
29    // 2. already Normal: untouched, reported as 2
30    was = strata::platform::restore_normal_io_priority(nullptr);
31    if (was != 2 || io_prio() != 2) { std::printf("FAIL normal: was=%d now=%lu\n", was, io_prio()); ++bad; }
32    // 3. STRATA_KEEP_IO_PRIORITY=1 leaves a low value alone
33    _putenv("STRATA_KEEP_IO_PRIORITY=1");
34    set_io_prio(1);
35    was = strata::platform::restore_normal_io_priority(nullptr);
36    if (was != -1 || io_prio() != 1) { std::printf("FAIL keep: was=%d now=%lu\n", was, io_prio()); ++bad; }
37    std::printf(bad == 0 ? "io_priority_test: OK\n" : "io_priority_test: %d FAILED\n", bad);
38    return bad == 0 ? 0 : 1;
39#else
40    std::printf("io_priority_test: SKIP (Windows only)\n");
41    return 77;
42#endif
43}