Properly check the maxstacksize asked for the Thread
authorJb Evain <jbevain@gmail.com>
Mon, 2 May 2011 09:39:21 +0000 (11:39 +0200)
committerJb Evain <jbevain@gmail.com>
Mon, 2 May 2011 09:42:48 +0000 (11:42 +0200)
mcs/class/corlib/System.Threading/Thread.cs

index 8016f69c5d13912ea7823387a82dedd286da5fba..f8c1c4e324185051087313c9ca399a4ab8832644 100644 (file)
@@ -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")]