rts: Add --internal-counters RTS flag and several counters
[ghc.git] / rts / Messages.c
index 91ee9a6..a9c794d 100644 (file)
    Send a message to another Capability
    ------------------------------------------------------------------------- */
 
-#ifdef THREADED_RTS
+#if defined(THREADED_RTS)
 
 void sendMessage(Capability *from_cap, Capability *to_cap, Message *msg)
 {
     ACQUIRE_LOCK(&to_cap->lock);
 
-#ifdef DEBUG    
+#if defined(DEBUG)
     {
         const StgInfoTable *i = msg->header.info;
         if (i != &stg_MSG_THROWTO_info &&
@@ -44,11 +44,11 @@ void sendMessage(Capability *from_cap, Capability *to_cap, Message *msg)
     recordClosureMutated(from_cap,(StgClosure*)msg);
 
     if (to_cap->running_task == NULL) {
-       to_cap->running_task = myTask(); 
+        to_cap->running_task = myTask();
             // precond for releaseCapability_()
-       releaseCapability_(to_cap,rtsFalse);
+        releaseCapability_(to_cap,false);
     } else {
-        contextSwitchCapability(to_cap);
+        interruptCapability(to_cap);
     }
 
     RELEASE_LOCK(&to_cap->lock);
@@ -60,7 +60,7 @@ void sendMessage(Capability *from_cap, Capability *to_cap, Message *msg)
    Handle a message
    ------------------------------------------------------------------------- */
 
-#ifdef THREADED_RTS
+#if defined(THREADED_RTS)
 
 void
 executeMessage (Capability *cap, Message *m)
@@ -73,14 +73,14 @@ loop:
     if (i == &stg_MSG_TRY_WAKEUP_info)
     {
         StgTSO *tso = ((MessageWakeup *)m)->tso;
-        debugTraceCap(DEBUG_sched, cap, "message: try wakeup thread %ld", 
-                      (lnat)tso->id);
+        debugTraceCap(DEBUG_sched, cap, "message: try wakeup thread %ld",
+                      (W_)tso->id);
         tryWakeupThread(cap, tso);
     }
     else if (i == &stg_MSG_THROWTO_info)
     {
         MessageThrowTo *t = (MessageThrowTo *)m;
-        nat r;
+        uint32_t r;
         const StgInfoTable *i;
 
         i = lockClosure((StgClosure*)m);
@@ -89,8 +89,8 @@ loop:
             goto loop;
         }
 
-        debugTraceCap(DEBUG_sched, cap, "message: throwTo %ld -> %ld", 
-                      (lnat)t->source->id, (lnat)t->target->id);
+        debugTraceCap(DEBUG_sched, cap, "message: throwTo %ld -> %ld",
+                      (W_)t->source->id, (W_)t->target->id);
 
         ASSERT(t->source->why_blocked == BlockedOnMsgThrowTo);
         ASSERT(t->source->block_info.closure == (StgClosure *)m);
@@ -98,11 +98,13 @@ loop:
         r = throwToMsg(cap, t);
 
         switch (r) {
-        case THROWTO_SUCCESS:
+        case THROWTO_SUCCESS: {
             // this message is done
-            unlockClosure((StgClosure*)m, &stg_MSG_NULL_info);
-            tryWakeupThread(cap, t->source);
+            StgTSO *source = t->source;
+            doneWithMsgThrowTo(t);
+            tryWakeupThread(cap, source);
             break;
+        }
         case THROWTO_BLOCKED:
             // unlock the message
             unlockClosure((StgClosure*)m, &stg_MSG_THROWTO_info);
@@ -111,7 +113,7 @@ loop:
     }
     else if (i == &stg_MSG_BLACKHOLE_info)
     {
-        nat r;
+        uint32_t r;
         MessageBlackHole *b = (MessageBlackHole*)m;
 
         r = messageBlackHole(cap, b);
@@ -127,6 +129,9 @@ loop:
     }
     else if (i == &stg_WHITEHOLE_info)
     {
+#if defined(PROF_SPIN)
+        ++whitehole_executeMessage_spin;
+#endif
         goto loop;
     }
     else
@@ -142,10 +147,10 @@ loop:
 
    This is called from two places: either we just entered a BLACKHOLE
    (stg_BLACKHOLE_info), or we received a MSG_BLACKHOLE in our
-   cap->inbox.  
+   cap->inbox.
 
    We need to establish whether the BLACKHOLE belongs to
-   this Capability, and 
+   this Capability, and
      - if so, arrange to block the current thread on it
      - otherwise, forward the message to the right place
 
@@ -156,16 +161,16 @@ loop:
 
    ------------------------------------------------------------------------- */
 
-nat messageBlackHole(Capability *cap, MessageBlackHole *msg)
+uint32_t messageBlackHole(Capability *cap, MessageBlackHole *msg)
 {
     const StgInfoTable *info;
     StgClosure *p;
     StgBlockingQueue *bq;
-    StgClosure *bh = msg->bh;
+    StgClosure *bh = UNTAG_CLOSURE(msg->bh);
     StgTSO *owner;
 
-    debugTraceCap(DEBUG_sched, cap, "message: thread %d blocking on blackhole %p", 
-                  (lnat)msg->tso->id, msg->bh);
+    debugTraceCap(DEBUG_sched, cap, "message: thread %d blocking on "
+                  "blackhole %p", (W_)msg->tso->id, msg->bh);
 
     info = bh->header.info;
 
@@ -173,8 +178,9 @@ nat messageBlackHole(Capability *cap, MessageBlackHole *msg)
     // BLACKHOLE has already been updated, and GC has shorted out the
     // indirection, so the pointer no longer points to a BLACKHOLE at
     // all.
-    if (info != &stg_BLACKHOLE_info && 
-        info != &stg_CAF_BLACKHOLE_info && 
+    if (info != &stg_BLACKHOLE_info &&
+        info != &stg_CAF_BLACKHOLE_info &&
+        info != &__stg_EAGER_BLACKHOLE_info &&
         info != &stg_WHITEHOLE_info) {
         // if it is a WHITEHOLE, then a thread is in the process of
         // trying to BLACKHOLE it.  But we know that it was once a
@@ -186,7 +192,9 @@ nat messageBlackHole(Capability *cap, MessageBlackHole *msg)
     // The blackhole must indirect to a TSO, a BLOCKING_QUEUE, an IND,
     // or a value.
 loop:
-    p = UNTAG_CLOSURE(((StgInd*)bh)->indirectee);
+    // NB. VOLATILE_LOAD(), because otherwise gcc hoists the load
+    // and turns this into an infinite loop.
+    p = UNTAG_CLOSURE((StgClosure*)VOLATILE_LOAD(&((StgInd*)bh)->indirectee));
     info = p->header.info;
 
     if (info == &stg_IND_info)
@@ -200,12 +208,13 @@ loop:
 
     else if (info == &stg_TSO_info)
     {
-        owner = deRefTSO((StgTSO *)p);
+        owner = (StgTSO*)p;
 
-#ifdef THREADED_RTS
+#if defined(THREADED_RTS)
         if (owner->cap != cap) {
             sendMessage(cap, owner->cap, (Message*)msg);
-            debugTraceCap(DEBUG_sched, cap, "forwarding message to cap %d", owner->cap->no);
+            debugTraceCap(DEBUG_sched, cap, "forwarding message to cap %d",
+                          owner->cap->no);
             return 1;
         }
 #endif
@@ -214,15 +223,15 @@ loop:
         // BLACKHOLE, so we first create a BLOCKING_QUEUE object.
 
         bq = (StgBlockingQueue*)allocate(cap, sizeofW(StgBlockingQueue));
-            
+
         // initialise the BLOCKING_QUEUE object
         SET_HDR(bq, &stg_BLOCKING_QUEUE_DIRTY_info, CCS_SYSTEM);
         bq->bh = bh;
         bq->queue = msg;
         bq->owner = owner;
-        
+
         msg->link = (MessageBlackHole*)END_TSO_QUEUE;
-        
+
         // All BLOCKING_QUEUES are linked in a list on owner->bq, so
         // that we can search through them in the event that there is
         // a collision to update a BLACKHOLE and a BLOCKING_QUEUE
@@ -241,8 +250,7 @@ loop:
         // the current thread, since in that case it will not be on
         // the run queue.
         if (owner->why_blocked == NotBlocked && owner->id != msg->tso->id) {
-            removeFromRunQueue(cap, owner);
-            pushOnRunQueue(cap,owner);
+            promoteInRunQueue(cap, owner);
         }
 
         // point to the BLOCKING_QUEUE from the BLACKHOLE
@@ -250,26 +258,27 @@ loop:
         ((StgInd*)bh)->indirectee = (StgClosure *)bq;
         recordClosureMutated(cap,bh); // bh was mutated
 
-        debugTraceCap(DEBUG_sched, cap, "thread %d blocked on thread %d", 
-                      (lnat)msg->tso->id, (lnat)owner->id);
+        debugTraceCap(DEBUG_sched, cap, "thread %d blocked on thread %d",
+                      (W_)msg->tso->id, (W_)owner->id);
 
         return 1; // blocked
     }
-    else if (info == &stg_BLOCKING_QUEUE_CLEAN_info || 
+    else if (info == &stg_BLOCKING_QUEUE_CLEAN_info ||
              info == &stg_BLOCKING_QUEUE_DIRTY_info)
     {
         StgBlockingQueue *bq = (StgBlockingQueue *)p;
 
         ASSERT(bq->bh == bh);
 
-        owner = deRefTSO(bq->owner);
+        owner = bq->owner;
 
         ASSERT(owner != END_TSO_QUEUE);
 
-#ifdef THREADED_RTS
+#if defined(THREADED_RTS)
         if (owner->cap != cap) {
             sendMessage(cap, owner->cap, (Message*)msg);
-            debugTraceCap(DEBUG_sched, cap, "forwarding message to cap %d", owner->cap->no);
+            debugTraceCap(DEBUG_sched, cap, "forwarding message to cap %d",
+                          owner->cap->no);
             return 1;
         }
 #endif
@@ -283,18 +292,60 @@ loop:
             recordClosureMutated(cap,(StgClosure*)bq);
         }
 
-        debugTraceCap(DEBUG_sched, cap, "thread %d blocked on thread %d", 
-                      (lnat)msg->tso->id, (lnat)owner->id);
+        debugTraceCap(DEBUG_sched, cap,
+                      "thread %d blocked on existing BLOCKING_QUEUE "
+                      "owned by thread %d",
+                      (W_)msg->tso->id, (W_)owner->id);
 
         // See above, #3838
         if (owner->why_blocked == NotBlocked && owner->id != msg->tso->id) {
-            removeFromRunQueue(cap, owner);
-            pushOnRunQueue(cap,owner);
+            promoteInRunQueue(cap, owner);
         }
 
         return 1; // blocked
     }
-    
+
     return 0; // not blocked
 }
 
+// A shorter version of messageBlackHole(), that just returns the
+// owner (or NULL if the owner cannot be found, because the blackhole
+// has been updated in the meantime).
+
+StgTSO * blackHoleOwner (StgClosure *bh)
+{
+    const StgInfoTable *info;
+    StgClosure *p;
+
+    info = bh->header.info;
+
+    if (info != &stg_BLACKHOLE_info &&
+        info != &stg_CAF_BLACKHOLE_info &&
+        info != &__stg_EAGER_BLACKHOLE_info &&
+        info != &stg_WHITEHOLE_info) {
+        return NULL;
+    }
+
+    // The blackhole must indirect to a TSO, a BLOCKING_QUEUE, an IND,
+    // or a value.
+loop:
+    // NB. VOLATILE_LOAD(), because otherwise gcc hoists the load
+    // and turns this into an infinite loop.
+    p = UNTAG_CLOSURE((StgClosure*)VOLATILE_LOAD(&((StgInd*)bh)->indirectee));
+    info = p->header.info;
+
+    if (info == &stg_IND_info) goto loop;
+
+    else if (info == &stg_TSO_info)
+    {
+        return (StgTSO*)p;
+    }
+    else if (info == &stg_BLOCKING_QUEUE_CLEAN_info ||
+             info == &stg_BLOCKING_QUEUE_DIRTY_info)
+    {
+        StgBlockingQueue *bq = (StgBlockingQueue *)p;
+        return bq->owner;
+    }
+
+    return NULL; // not blocked
+}