Keep a maximum of 6 spare worker threads per Capability (#4262)
authorSimon Marlow <marlowsd@gmail.com>
Thu, 25 Nov 2010 13:57:29 +0000 (13:57 +0000)
committerSimon Marlow <marlowsd@gmail.com>
Thu, 25 Nov 2010 13:57:29 +0000 (13:57 +0000)
includes/rts/Constants.h
rts/Capability.c
rts/Capability.h
rts/Schedule.c

index 140aaa4..e21a893 100644 (file)
  */
 #define SPIN_COUNT 1000
 
+/* -----------------------------------------------------------------------------
+   Spare workers per Capability in the threaded RTS
+
+   No more than MAX_SPARE_WORKERS will be kept in the thread pool
+   associated with each Capability.
+   -------------------------------------------------------------------------- */
+
+#define MAX_SPARE_WORKERS 6
+
 #endif /* RTS_CONSTANTS_H */
index 2fcd72c..8234359 100644 (file)
@@ -219,6 +219,7 @@ initCapability( Capability *cap, nat i )
     initMutex(&cap->lock);
     cap->running_task      = NULL; // indicates cap is free
     cap->spare_workers     = NULL;
+    cap->n_spare_workers   = 0;
     cap->suspended_ccalls  = NULL;
     cap->returning_tasks_hd = NULL;
     cap->returning_tasks_tl = NULL;
@@ -460,9 +461,24 @@ releaseCapabilityAndQueueWorker (Capability* cap USED_IF_THREADS)
     // in which case it is not replaced on the spare_worker queue.
     // This happens when the system is shutting down (see
     // Schedule.c:workerStart()).
-    if (!isBoundTask(task) && !task->stopped) {
-       task->next = cap->spare_workers;
-       cap->spare_workers = task;
+    if (!isBoundTask(task) && !task->stopped)
+    {
+        if (cap->n_spare_workers < MAX_SPARE_WORKERS)
+        {
+            task->next = cap->spare_workers;
+            cap->spare_workers = task;
+            cap->n_spare_workers++;
+        }
+        else
+        {
+            debugTrace(DEBUG_sched, "%d spare workers already, exiting",
+                       cap->n_spare_workers);
+            releaseCapability_(cap,rtsFalse);
+            // hold the lock until after workerTaskStop; c.f. scheduleWorker()
+            workerTaskStop(task);
+            RELEASE_LOCK(&cap->lock);
+            shutdownThread();
+        }
     }
     // Bound tasks just float around attached to their TSOs.
 
@@ -619,7 +635,8 @@ yieldCapability (Capability** pCap, Task *task)
                }
                cap->spare_workers = task->next;
                task->next = NULL;
-           }
+                cap->n_spare_workers--;
+            }
            cap->running_task = task;
            RELEASE_LOCK(&cap->lock);
            break;
@@ -708,12 +725,13 @@ shutdownCapability (Capability *cap, Task *task, rtsBool safe)
                 if (!osThreadIsAlive(t->id)) {
                     debugTrace(DEBUG_sched, 
                                "worker thread %p has died unexpectedly", (void *)t->id);
-                        if (!prev) {
-                            cap->spare_workers = t->next;
-                        } else {
-                            prev->next = t->next;
-                        }
-                        prev = t;
+                    cap->n_spare_workers--;
+                    if (!prev) {
+                        cap->spare_workers = t->next;
+                    } else {
+                        prev->next = t->next;
+                    }
+                    prev = t;
                 }
             }
         }
index a32062a..a15ce15 100644 (file)
@@ -79,6 +79,7 @@ struct Capability_ {
 #if defined(THREADED_RTS)
     // Worker Tasks waiting in the wings.  Singly-linked.
     Task *spare_workers;
+    nat n_spare_workers; // count of above
 
     // This lock protects running_task, returning_tasks_{hd,tl}, wakeup_queue.
     Mutex lock;
index f6a9ef2..8e39814 100644 (file)
@@ -1607,7 +1607,8 @@ forkProcess(HsStablePtr *entry
        // Wipe our spare workers list, they no longer exist.  New
        // workers will be created if necessary.
        cap->spare_workers = NULL;
-       cap->returning_tasks_hd = NULL;
+        cap->n_spare_workers = 0;
+        cap->returning_tasks_hd = NULL;
        cap->returning_tasks_tl = NULL;
 #endif