okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1// #4733: see include/strata/platform/io_priority.hpp 2#include "strata/platform/io_priority.hpp" 3 4#include <cstdlib> 5 6#if defined(_WIN32 ) 7#ifndef WIN32_LEAN_AND_MEAN 8#define WIN32_LEAN_AND_MEAN 9#endif 10#ifndef NOMINMAX 11#define NOMINMAX 12#endif 13#include <windows.h> 14#endif 15 16namespace strata::platform { 17 18int restore_normal_io_priority (std::FILE * log ) { 19#if defined(_WIN32 ) 20const char * keep = std::getenv ("STRATA_KEEP_IO_PRIORITY" ); 21if (keep != nullptr && keep [0 ]== '1' )return -1 ; 22using Query = LONG (NTAPI * )(HANDLE ,ULONG ,PVOID ,ULONG ,PULONG ); 23using Set = LONG (NTAPI * )(HANDLE ,ULONG ,PVOID ,ULONG ); 24HMODULE nt = GetModuleHandleW (L"ntdll.dll" ); 25if (nt == nullptr )return -1 ; 26auto query = reinterpret_cast < Query > (reinterpret_cast < void *> (GetProcAddress (nt ,"NtQueryInformationProcess" ))); 27auto set = reinterpret_cast < Set > (reinterpret_cast < void *> (GetProcAddress (nt ,"NtSetInformationProcess" ))); 28if (query == nullptr || set == nullptr )return -1 ; 29constexpr ULONG kProcessIoPriority = 33 ; 30ULONG now = 2 ,ret = 0 ; 31if (query (GetCurrentProcess (),kProcessIoPriority ,& now ,sizeof now ,& ret )!= 0 || ret < sizeof now )return -1 ; 32if (now >=2 )return (int )now ; 33ULONG want = 2 ; 34if (set (GetCurrentProcess (),kProcessIoPriority ,& want ,sizeof want )!= 0 ) { 35if (log != nullptr ) 36 std::fprintf (log ,"strata: this program runs at a low disk priority (inherited from the program that started it) " 37"and Windows did not let it go back to normal; reading the model can be several times slower (#4733)\n" ); 38return (int )now ; 39 } 40if (log != nullptr ) 41 std::fprintf (log ,"strata: the program that started the engine runs at a low disk priority, which the engine inherits and " 42"which makes model reads several times slower; the engine's disk priority is back to normal (#4733)\n" ); 43return (int )now ; 44#else 45 (void )log ; 46return -1 ; 47#endif 48} 49 50}// namespace strata::platform