[threadpool] Fix overflow of max number of worker threads
authorLudovic Henry <ludovic@xamarin.com>
Mon, 14 Dec 2015 10:29:45 +0000 (10:29 +0000)
committerLudovic Henry <ludovic@xamarin.com>
Mon, 14 Dec 2015 17:09:08 +0000 (17:09 +0000)
In case all threads would wait on a Sleep or a Wait, the monitor thread would simply increase the number of max working thread, up to the point it would overflow the number of max worker threads.

mono/metadata/threadpool-ms.c

index 9e960df6e37aa1e37102d53972936951400d5bc2..1c64377b673ef48383113e93839ee5d408dca432 100644 (file)
@@ -881,8 +881,18 @@ monitor_thread (void)
 
                if (all_waitsleepjoin) {
                        ThreadPoolCounter counter;
-                       COUNTER_ATOMIC (counter, { counter._.max_working ++; });
-                       hill_climbing_force_change (counter._.max_working, TRANSITION_STARVATION);
+                       gboolean limit_worker_max_reached = FALSE;
+
+                       COUNTER_ATOMIC (counter, {
+                               if (counter._.max_working >= threadpool->limit_worker_max) {
+                                       limit_worker_max_reached = TRUE;
+                                       break;
+                               }
+                               counter._.max_working ++;
+                       });
+
+                       if (!limit_worker_max_reached)
+                               hill_climbing_force_change (counter._.max_working, TRANSITION_STARVATION);
                }
 
                threadpool->cpu_usage = mono_cpu_usage (threadpool->cpu_usage_state);