okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
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 () { 10using Query = LONG (NTAPI * )(HANDLE ,ULONG ,PVOID ,ULONG ,PULONG ); 11auto q = reinterpret_cast < Query > (reinterpret_cast < void *> (GetProcAddress (GetModuleHandleW (L"ntdll.dll" ),"NtQueryInformationProcess" ))); 12ULONG v = 99 ,r = 0 ; 13return q != nullptr && q (GetCurrentProcess (),33 ,& v ,sizeof v ,& r )== 0 ?v :99 ; 14} 15static bool set_io_prio (ULONG v ) { 16using Set = LONG (NTAPI * )(HANDLE ,ULONG ,PVOID ,ULONG ); 17auto s = reinterpret_cast < Set > (reinterpret_cast < void *> (GetProcAddress (GetModuleHandleW (L"ntdll.dll" ),"NtSetInformationProcess" ))); 18return s != nullptr && s (GetCurrentProcess (),33 ,& v ,sizeof v )== 0 ; 19} 20#endif 21 22int main () { 23#if defined(_WIN32 ) 24int bad = 0 ; 25// 1. started at Low: back to Normal, and the call reports what it found 26if (!set_io_prio (1 )) { std::printf ("io_priority_test: SKIP (cannot lower the I/O priority here)\n" );return 77 ; } 27int was = strata::platform::restore_normal_io_priority (stderr ); 28if (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 30was = strata::platform::restore_normal_io_priority (nullptr ); 31if (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" ); 34set_io_prio (1 ); 35was = strata::platform::restore_normal_io_priority (nullptr ); 36if (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 ); 38return bad == 0 ?0 :1 ; 39#else 40 std::printf ("io_priority_test: SKIP (Windows only)\n" ); 41return 77 ; 42#endif 43}