Implement setStackSize for Windows

This commit is contained in:
PoweredByPie 2024-05-18 04:27:09 -07:00
parent e42d00c961
commit 6a3f906382
2 changed files with 21 additions and 4 deletions

View file

@ -59,11 +59,11 @@ unsigned int getMaxCPU()
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
#ifndef _WIN32
size_t savedStackSize = 0; size_t savedStackSize = 0;
void setStackSize(size_t stackSize) void setStackSize(size_t stackSize)
{ {
#ifndef _WIN32
struct rlimit limit; struct rlimit limit;
if (getrlimit(RLIMIT_STACK, &limit) == 0 && limit.rlim_cur < stackSize) { if (getrlimit(RLIMIT_STACK, &limit) == 0 && limit.rlim_cur < stackSize) {
savedStackSize = limit.rlim_cur; savedStackSize = limit.rlim_cur;
@ -81,8 +81,27 @@ void setStackSize(size_t stackSize)
); );
} }
} }
#else
ULONG currStackSize = 0;
// This retrieves the current promised stack size
SetThreadStackGuarantee(&currStackSize);
if (currStackSize < stackSize) {
savedStackSize = currStackSize;
ULONG newStackSize = stackSize;
if (SetThreadStackGuarantee(&newStackSize) == 0) {
logger->log(
lvlError,
HintFmt(
"Failed to increase stack size from %1% to %2%: %3%",
savedStackSize,
stackSize,
std::to_string(GetLastError())
).str()
);
}
}
#endif
} }
#endif
void restoreProcessContext(bool restoreMounts) void restoreProcessContext(bool restoreMounts)
{ {

View file

@ -17,12 +17,10 @@ namespace nix {
*/ */
unsigned int getMaxCPU(); unsigned int getMaxCPU();
#ifndef _WIN32 // TODO implement on Windows, if needed.
/** /**
* Change the stack size. * Change the stack size.
*/ */
void setStackSize(size_t stackSize); void setStackSize(size_t stackSize);
#endif
/** /**
* Restore the original inherited Unix process context (such as signal * Restore the original inherited Unix process context (such as signal