Do not link ghc stage1 using -threaded, only for stage2 or 3
[ghc-hetmet.git] / rts / Threads.c
index d2cac62..501c751 100644 (file)
@@ -119,7 +119,7 @@ createThread(Capability *cap, nat size)
   /* put a stop frame on the stack */
     tso->sp -= sizeofW(StgStopFrame);
     SET_HDR((StgClosure*)tso->sp,(StgInfoTable *)&stg_stop_thread_info,CCS_SYSTEM);
-    tso->link = END_TSO_QUEUE;
+    tso->_link = END_TSO_QUEUE;
     
   // ToDo: check this
 #if defined(GRAN)
@@ -145,8 +145,8 @@ createThread(Capability *cap, nat size)
      */
     ACQUIRE_LOCK(&sched_mutex);
     tso->id = next_thread_id++;  // while we have the mutex
-    tso->global_link = all_threads;
-    all_threads = tso;
+    tso->global_link = g0s0->threads;
+    g0s0->threads = tso;
     RELEASE_LOCK(&sched_mutex);
     
 #if defined(DIST)
@@ -210,6 +210,8 @@ createThread(Capability *cap, nat size)
     }
 #endif 
     
+    postEvent (cap, EVENT_CREATE_THREAD, tso->id, 0);
+
 #if defined(GRAN)
     debugTrace(GRAN_DEBUG_pri,
               "==__ schedule: Created TSO %d (%p);",
@@ -292,17 +294,17 @@ rts_getThreadId(StgPtr tso)
    -------------------------------------------------------------------------- */
 
 void
-removeThreadFromQueue (StgTSO **queue, StgTSO *tso)
+removeThreadFromQueue (Capability *cap, StgTSO **queue, StgTSO *tso)
 {
     StgTSO *t, *prev;
 
     prev = NULL;
-    for (t = *queue; t != END_TSO_QUEUE; prev = t, t = t->link) {
+    for (t = *queue; t != END_TSO_QUEUE; prev = t, t = t->_link) {
        if (t == tso) {
            if (prev) {
-               prev->link = t->link;
+               setTSOLink(cap,prev,t->_link);
            } else {
-               *queue = t->link;
+               *queue = t->_link;
            }
            return;
        }
@@ -311,17 +313,18 @@ removeThreadFromQueue (StgTSO **queue, StgTSO *tso)
 }
 
 void
-removeThreadFromDeQueue (StgTSO **head, StgTSO **tail, StgTSO *tso)
+removeThreadFromDeQueue (Capability *cap, 
+                         StgTSO **head, StgTSO **tail, StgTSO *tso)
 {
     StgTSO *t, *prev;
 
     prev = NULL;
-    for (t = *head; t != END_TSO_QUEUE; prev = t, t = t->link) {
+    for (t = *head; t != END_TSO_QUEUE; prev = t, t = t->_link) {
        if (t == tso) {
            if (prev) {
-               prev->link = t->link;
+               setTSOLink(cap,prev,t->_link);
            } else {
-               *head = t->link;
+               *head = t->_link;
            }
            if (*tail == tso) {
                if (prev) {
@@ -337,9 +340,9 @@ removeThreadFromDeQueue (StgTSO **head, StgTSO **tail, StgTSO *tso)
 }
 
 void
-removeThreadFromMVarQueue (StgMVar *mvar, StgTSO *tso)
+removeThreadFromMVarQueue (Capability *cap, StgMVar *mvar, StgTSO *tso)
 {
-    removeThreadFromDeQueue (&mvar->head, &mvar->tail, tso);
+    removeThreadFromDeQueue (cap, &mvar->head, &mvar->tail, tso);
 }
 
 /* ----------------------------------------------------------------------------
@@ -489,8 +492,8 @@ unblockOne_ (Capability *cap, StgTSO *tso,
   ASSERT(tso->why_blocked != NotBlocked);
 
   tso->why_blocked = NotBlocked;
-  next = tso->link;
-  tso->link = END_TSO_QUEUE;
+  next = tso->_link;
+  tso->_link = END_TSO_QUEUE;
 
 #if defined(THREADED_RTS)
   if (tso->cap == cap || (!tsoLocked(tso) && 
@@ -502,22 +505,30 @@ unblockOne_ (Capability *cap, StgTSO *tso,
          ASSERT(tso->bound->cap == tso->cap);
          tso->bound->cap = cap;
       }
+
       tso->cap = cap;
       appendToRunQueue(cap,tso);
-      // we're holding a newly woken thread, make sure we context switch
-      // quickly so we can migrate it if necessary.
-      context_switch = 1;
+
+      // context-switch soonish so we can migrate the new thread if
+      // necessary.  NB. not contextSwitchCapability(cap), which would
+      // force a context switch immediately.
+      cap->context_switch = 1;
   } else {
       // we'll try to wake it up on the Capability it was last on.
-      wakeupThreadOnCapability_lock(tso->cap, tso);
+      wakeupThreadOnCapability(cap, tso->cap, tso);
   }
 #else
   appendToRunQueue(cap,tso);
-  context_switch = 1;
+
+  // context-switch soonish so we can migrate the new thread if
+  // necessary.  NB. not contextSwitchCapability(cap), which would
+  // force a context switch immediately.
+  cap->context_switch = 1;
 #endif
 
-  debugTrace(DEBUG_sched,
-            "waking up thread %ld on cap %d",
+  postEvent (cap, EVENT_THREAD_WAKEUP, tso->id, tso->cap->no);
+
+  debugTrace(DEBUG_sched, "waking up thread %ld on cap %d",
             (long)tso->id, tso->cap->no);
 
   return next;
@@ -657,13 +668,13 @@ awakenBlockedQueue(Capability *cap, StgTSO *tso)
  * used by Control.Concurrent for error checking.
  * ------------------------------------------------------------------------- */
  
-StgBool
+HsBool
 rtsSupportsBoundThreads(void)
 {
 #if defined(THREADED_RTS)
-  return rtsTrue;
+  return HS_BOOL_TRUE;
 #else
-  return rtsFalse;
+  return HS_BOOL_FALSE;
 #endif
 }
 
@@ -697,7 +708,7 @@ printThreadBlockage(StgTSO *tso)
     break;
 #if defined(mingw32_HOST_OS)
     case BlockedOnDoProc:
-    debugBelch("is blocked on proc (request: %ld)", tso->block_info.async_result->reqID);
+    debugBelch("is blocked on proc (request: %u)", tso->block_info.async_result->reqID);
     break;
 #endif
   case BlockedOnDelay:
@@ -762,6 +773,11 @@ printThreadStatus(StgTSO *t)
        default:
            printThreadBlockage(t);
        }
+        if (t->flags & TSO_DIRTY) {
+            debugBelch(" (TSO_DIRTY)");
+        } else if (t->flags & TSO_LINK_DIRTY) {
+            debugBelch(" (TSO_LINK_DIRTY)");
+        }
        debugBelch("\n");
     }
 }
@@ -770,7 +786,7 @@ void
 printAllThreads(void)
 {
   StgTSO *t, *next;
-  nat i;
+  nat i, s;
   Capability *cap;
 
 # if defined(GRAN)
@@ -792,21 +808,23 @@ printAllThreads(void)
   for (i = 0; i < n_capabilities; i++) {
       cap = &capabilities[i];
       debugBelch("threads on capability %d:\n", cap->no);
-      for (t = cap->run_queue_hd; t != END_TSO_QUEUE; t = t->link) {
+      for (t = cap->run_queue_hd; t != END_TSO_QUEUE; t = t->_link) {
          printThreadStatus(t);
       }
   }
 
   debugBelch("other threads:\n");
-  for (t = all_threads; t != END_TSO_QUEUE; t = next) {
+  for (s = 0; s < total_steps; s++) {
+    for (t = all_steps[s].threads; t != END_TSO_QUEUE; t = next) {
       if (t->why_blocked != NotBlocked) {
          printThreadStatus(t);
       }
       if (t->what_next == ThreadRelocated) {
-         next = t->link;
+         next = t->_link;
       } else {
          next = t->global_link;
       }
+    }
   }
 }
 
@@ -815,7 +833,7 @@ void
 printThreadQueue(StgTSO *t)
 {
     nat i = 0;
-    for (; t != END_TSO_QUEUE; t = t->link) {
+    for (; t != END_TSO_QUEUE; t = t->_link) {
        printThreadStatus(t);
        i++;
     }