Use https links in user-facing startup and error messages
[ghc.git] / rts / Task.c
index 8ce4ecc..11ba5f1 100644 (file)
@@ -19,6 +19,8 @@
 #include "Hash.h"
 #include "Trace.h"
 
+#include <string.h>
+
 #if HAVE_SIGNAL_H
 #include <signal.h>
 #endif
@@ -36,7 +38,7 @@ uint32_t peakWorkerCount;
 static int tasksInitialized = 0;
 
 static void   freeTask  (Task *task);
-static Task * newTask   (rtsBool);
+static Task * newTask   (bool);
 
 #if defined(THREADED_RTS)
 Mutex all_tasks_mutex;
@@ -124,7 +126,7 @@ Task* getTask (void)
     if (task != NULL) {
         return task;
     } else {
-        task = newTask(rtsFalse);
+        task = newTask(false);
 #if defined(THREADED_RTS)
         task->id = osThreadId();
 #endif
@@ -197,8 +199,9 @@ freeTask (Task *task)
     stgFree(task);
 }
 
+/* Must take all_tasks_mutex */
 static Task*
-newTask (rtsBool worker)
+newTask (bool worker)
 {
     Task *task;
 
@@ -207,8 +210,8 @@ newTask (rtsBool worker)
 
     task->cap           = NULL;
     task->worker        = worker;
-    task->stopped       = rtsTrue;
-    task->running_finalizers = rtsFalse;
+    task->stopped       = true;
+    task->running_finalizers = false;
     task->n_spare_incalls = 0;
     task->spare_incalls = NULL;
     task->incall        = NULL;
@@ -217,7 +220,8 @@ newTask (rtsBool worker)
 #if defined(THREADED_RTS)
     initCondition(&task->cond);
     initMutex(&task->lock);
-    task->wakeup = rtsFalse;
+    task->id = 0;
+    task->wakeup = false;
     task->node = 0;
 #endif
 
@@ -304,7 +308,7 @@ newBoundTask (void)
 
     task = getTask();
 
-    task->stopped = rtsFalse;
+    task->stopped = false;
 
     newInCall(task);
 
@@ -327,14 +331,14 @@ boundTaskExiting (Task *task)
     // call and then a callback, so it can transform into a bound
     // Task for the duration of the callback.
     if (task->incall == NULL) {
-        task->stopped = rtsTrue;
+        task->stopped = true;
     }
 
     debugTrace(DEBUG_sched, "task exiting");
 }
 
 
-#ifdef THREADED_RTS
+#if defined(THREADED_RTS)
 #define TASK_ID(t) (t)->id
 #else
 #define TASK_ID(t) (t)
@@ -413,7 +417,7 @@ workerTaskStop (Task *task)
 
 #if defined(THREADED_RTS)
 
-static void OSThreadProcAttr
+static void* OSThreadProcAttr
 workerStart(Task *task)
 {
     Capability *cap;
@@ -439,8 +443,11 @@ workerStart(Task *task)
     traceTaskCreate(task, cap);
 
     scheduleWorker(cap,task);
+
+    return NULL;
 }
 
+/* N.B. must take all_tasks_mutex */
 void
 startWorkerTask (Capability *cap)
 {
@@ -449,8 +456,8 @@ startWorkerTask (Capability *cap)
   Task *task;
 
   // A worker always gets a fresh Task structure.
-  task = newTask(rtsTrue);
-  task->stopped = rtsFalse;
+  task = newTask(true);
+  task->stopped = false;
 
   // The lock here is to synchronise with taskStart(), to make sure
   // that we have finished setting up the Task structure before the
@@ -468,7 +475,26 @@ startWorkerTask (Capability *cap)
   ASSERT_LOCK_HELD(&cap->lock);
   cap->running_task = task;
 
-  r = createOSThread(&tid, "ghc_worker", (OSThreadProc*)workerStart, task);
+  // Set the name of the worker thread to the original process name followed by
+  // ":w", but only if we're on Linux where the program_invocation_short_name
+  // global is available.
+#if defined(linux_HOST_OS)
+  size_t procname_len = strlen(program_invocation_short_name);
+  char worker_name[16];
+  // The kernel only allocates 16 bytes for thread names, so we truncate if the
+  // original name is too long. Process names go in another table that has more
+  // capacity.
+  if (procname_len >= 13) {
+      strncpy(worker_name, program_invocation_short_name, 13);
+      strcpy(worker_name + 13, ":w");
+  } else {
+      strcpy(worker_name, program_invocation_short_name);
+      strcpy(worker_name + procname_len, ":w");
+  }
+#else
+  char * worker_name = "ghc_worker";
+#endif
+  r = createOSThread(&tid, worker_name, (OSThreadProc*)workerStart, task);
   if (r != 0) {
     sysErrorBelch("failed to create OS thread");
     stg_exit(EXIT_FAILURE);
@@ -501,7 +527,7 @@ void rts_setInCallCapability (
     Task *task = getTask();
     task->preferred_capability = preferred_capability;
 
-#ifdef THREADED_RTS
+#if defined(THREADED_RTS)
     if (affinity) {
         if (RtsFlags.ParFlags.setAffinity) {
             setThreadAffinity(preferred_capability, n_capabilities);
@@ -513,7 +539,7 @@ void rts_setInCallCapability (
 void rts_pinThreadToNumaNode (
     int node USED_IF_THREADS)
 {
-#ifdef THREADED_RTS
+#if defined(THREADED_RTS)
     if (RtsFlags.GcFlags.numa) {
         Task *task = getTask();
         task->node = capNoToNumaNode(node);
@@ -524,7 +550,7 @@ void rts_pinThreadToNumaNode (
 #endif
 }
 
-#ifdef DEBUG
+#if defined(DEBUG)
 
 void printAllTasks(void);