From 4986f4409edcc62129b05aff8593013338d1b4cf Mon Sep 17 00:00:00 2001 From: Jb Evain Date: Mon, 2 May 2011 11:39:21 +0200 Subject: [PATCH] Properly check the maxstacksize asked for the Thread --- mcs/class/corlib/System.Threading/Thread.cs | 33 ++++++++++++++------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/mcs/class/corlib/System.Threading/Thread.cs b/mcs/class/corlib/System.Threading/Thread.cs index 8016f69c5d1..f8c1c4e3241 100644 --- a/mcs/class/corlib/System.Threading/Thread.cs +++ b/mcs/class/corlib/System.Threading/Thread.cs @@ -887,17 +887,34 @@ namespace System.Threading { #endif + static int CheckStackSize (int maxStackSize) + { + if (maxStackSize < 0) + throw new ArgumentOutOfRangeException ("less than zero", "maxStackSize"); + + if (maxStackSize < 131072) // make sure stack is at least 128k big + return 131072; + + int page_size = Environment.SystemPageSize; + + if ((maxStackSize % page_size) != 0) // round up to a divisible of page size + maxStackSize = (maxStackSize / (page_size - 1)) * page_size; + + int default_stack_size = (IntPtr.Size / 4) * 1024 * 1024; // from wthreads.c + + if (maxStackSize > default_stack_size) + return default_stack_size; + + return maxStackSize; + } + public Thread (ThreadStart start, int maxStackSize) { if (start == null) throw new ArgumentNullException ("start"); - if (maxStackSize < 0) - throw new ArgumentOutOfRangeException ("less than zero", "maxStackSize"); - if (maxStackSize < 131072) //make sure stack is at least 128k big - maxStackSize = 131072; threadstart = start; - Internal.stack_size = maxStackSize; + Internal.stack_size = CheckStackSize (maxStackSize);; } public Thread (ParameterizedThreadStart start) @@ -912,13 +929,9 @@ namespace System.Threading { { if (start == null) throw new ArgumentNullException ("start"); - if (maxStackSize < 0) - throw new ArgumentOutOfRangeException ("less than zero", "maxStackSize"); - if (maxStackSize < 131072) //make sure stack is at least 128k big - maxStackSize = 131072; threadstart = start; - Internal.stack_size = maxStackSize; + Internal.stack_size = CheckStackSize (maxStackSize); } [MonoTODO ("limited to CompressedStack support")] -- 2.25.1