From e340456c5de9f0407419046304efb8ccf415130f Mon Sep 17 00:00:00 2001 From: Koundinya Veluri Date: Mon, 21 May 2018 12:09:03 -0700 Subject: [PATCH] Fix test for Thread constructor taking stack size (dotnet/corefx#29798) * Fix test for Thread constructor taking stack size Leave some pages worth of buffer when allocating stack space. See https://github.com/dotnet/coreclr/issues/17170. * Fix comment * Fix comment Commit migrated from https://github.com/dotnet/corefx/commit/0c9ecd37fd9c9404205f195bb9ce74cce7d4fd8d --- .../System.Threading.Thread/tests/ThreadTests.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/libraries/System.Threading.Thread/tests/ThreadTests.cs b/src/libraries/System.Threading.Thread/tests/ThreadTests.cs index 78ab0bb..f44cc10 100644 --- a/src/libraries/System.Threading.Thread/tests/ThreadTests.cs +++ b/src/libraries/System.Threading.Thread/tests/ThreadTests.cs @@ -26,11 +26,14 @@ namespace System.Threading.Threads.Tests [Fact] public static void ConstructorTest() { - const int SmallStackSize = 64 << 10; // 64 KB, currently accepted in all supported platforms, and is the PAL minimum - const int LargeStackSize = 2 << 20; // 2 MB, see https://github.com/dotnet/coreclr/issues/17170 + const int SmallStackSizeBytes = 64 << 10; // 64 KB, currently accepted in all supported platforms, and is the PAL minimum + const int LargeStackSizeBytes = 16 << 20; // 16 MB int pageSizeBytes = Environment.SystemPageSize; + // Leave some buffer for other data structures that will take some of the allocated stack space + int stackSizeBufferBytes = pageSizeBytes * 16; + Action startThreadAndJoin = t => { @@ -43,7 +46,7 @@ namespace System.Threading.Threads.Tests { // Try to stack-allocate an array to verify that close to the expected amount of stack space is actually // available - int bufferSizeBytes = Math.Max(16 << 10, stackSizeBytes - SmallStackSize); + int bufferSizeBytes = Math.Max(SmallStackSizeBytes / 4, stackSizeBytes - stackSizeBufferBytes); unsafe { byte* buffer = stackalloc byte[bufferSizeBytes]; @@ -56,12 +59,12 @@ namespace System.Threading.Threads.Tests }; startThreadAndJoin(new Thread(() => verifyStackSize(0))); startThreadAndJoin(new Thread(() => verifyStackSize(0), 0)); - startThreadAndJoin(new Thread(() => verifyStackSize(SmallStackSize), SmallStackSize)); - startThreadAndJoin(new Thread(() => verifyStackSize(LargeStackSize), LargeStackSize)); + startThreadAndJoin(new Thread(() => verifyStackSize(SmallStackSizeBytes), SmallStackSizeBytes)); + startThreadAndJoin(new Thread(() => verifyStackSize(LargeStackSizeBytes), LargeStackSizeBytes)); startThreadAndJoin(new Thread(state => verifyStackSize(0))); startThreadAndJoin(new Thread(state => verifyStackSize(0), 0)); - startThreadAndJoin(new Thread(state => verifyStackSize(SmallStackSize), SmallStackSize)); - startThreadAndJoin(new Thread(state => verifyStackSize(LargeStackSize), LargeStackSize)); + startThreadAndJoin(new Thread(state => verifyStackSize(SmallStackSizeBytes), SmallStackSizeBytes)); + startThreadAndJoin(new Thread(state => verifyStackSize(LargeStackSizeBytes), LargeStackSizeBytes)); Assert.Throws(() => new Thread((ThreadStart)null)); Assert.Throws(() => new Thread((ThreadStart)null, 0)); -- 2.7.4