Fix some more shutdown races
[ghc-hetmet.git] / rts / Task.c
index dcfa5b5..9397789 100644 (file)
@@ -15,6 +15,7 @@
 #include "Capability.h"
 #include "Stats.h"
 #include "RtsFlags.h"
+#include "Storage.h"
 #include "Schedule.h"
 #include "Hash.h"
 #include "Trace.h"
@@ -63,23 +64,37 @@ initTaskManager (void)
     }
 }
 
-
-void
-stopTaskManager (void)
+nat
+freeTaskManager (void)
 {
     Task *task, *next;
 
-    debugTrace(DEBUG_sched, 
-              "stopping task manager, %d tasks still running",
-              tasksRunning);
+    ASSERT_LOCK_HELD(&sched_mutex);
 
-    ACQUIRE_LOCK(&sched_mutex);
-    for (task = task_free_list; task != NULL; task = next) {
-        next = task->next;
-        stgFree(task);
+    debugTrace(DEBUG_sched, "freeing task manager, %d tasks still running",
+               tasksRunning);
+
+    for (task = all_tasks; task != NULL; task = next) {
+        next = task->all_link;
+        if (task->stopped) {
+            // We only free resources if the Task is not in use.  A
+            // Task may still be in use if we have a Haskell thread in
+            // a foreign call while we are attempting to shut down the
+            // RTS (see conc059).
+#if defined(THREADED_RTS)
+            closeCondition(&task->cond);
+            closeMutex(&task->lock);
+#endif
+            stgFree(task);
+        }
     }
+    all_tasks = NULL;
     task_free_list = NULL;
-    RELEASE_LOCK(&sched_mutex);
+#if defined(THREADED_RTS)
+    freeThreadLocalKey(&currentTaskKey);
+#endif
+
+    return tasksRunning;
 }
 
 
@@ -91,7 +106,8 @@ newTask (void)
 #endif
     Task *task;
 
-    task = stgMallocBytes(sizeof(Task), "newTask");
+#define ROUND_TO_CACHE_LINE(x) ((((x)+63) / 64) * 64)
+    task = stgMallocBytes(ROUND_TO_CACHE_LINE(sizeof(Task)), "newTask");
     
     task->cap  = NULL;
     task->stopped = rtsFalse;
@@ -125,7 +141,6 @@ newTask (void)
     all_tasks = task;
 
     taskCount++;
-    workerCount++;
 
     return task;
 }
@@ -161,6 +176,7 @@ newBoundTask (void)
 void
 boundTaskExiting (Task *task)
 {
+    task->tso = NULL;
     task->stopped = rtsTrue;
     task->cap = NULL;
 
@@ -192,9 +208,13 @@ discardTask (Task *task)
 {
     ASSERT_LOCK_HELD(&sched_mutex);
     if (!task->stopped) {
-       debugTrace(DEBUG_sched, "discarding task %ld", TASK_ID(task));
+       debugTrace(DEBUG_sched, "discarding task %ld", (long)TASK_ID(task));
        task->cap = NULL;
-       task->tso = NULL;
+        if (task->tso == NULL) {
+            workerCount--;
+        } else {
+            task->tso = NULL;
+        }
        task->stopped = rtsTrue;
        tasksRunning--;
        task->next = task_free_list;
@@ -235,9 +255,16 @@ workerTaskStop (Task *task)
     ASSERT(myTask() == task);
 #endif
 
+    task->cap = NULL;
     taskTimeStamp(task);
     task->stopped = rtsTrue;
     tasksRunning--;
+    workerCount--;
+
+    ACQUIRE_LOCK(&sched_mutex);
+    task->next = task_free_list;
+    task_free_list = task;
+    RELEASE_LOCK(&sched_mutex);
 }
 
 void
@@ -279,7 +306,8 @@ startWorkerTask (Capability *cap,
 
   r = createOSThread(&tid, (OSThreadProc *)taskStart, task);
   if (r != 0) {
-    barf("startTask: Can't create new task");
+    sysErrorBelch("failed to create OS thread");
+    stg_exit(EXIT_FAILURE);
   }
 
   debugTrace(DEBUG_sched, "new worker task (taskCount: %d)", taskCount);
@@ -316,7 +344,7 @@ printAllTasks(void)
                debugBelch("on capability %d, ", task->cap->no);
            }
            if (task->tso) {
-               debugBelch("bound to thread %d", task->tso->id);
+             debugBelch("bound to thread %lu", (unsigned long)task->tso->id);
            } else {
                debugBelch("worker");
            }