Fix handling of multiple signal deliveries in a row

This fixes bug 136154.

Background:
The function stack - CLG_(current_fn_stack) - is a stack of
pointers to the fn_node structs of the currently active
functions. This stack is used for determining current context
from call chain to current function, and modified on entering
(via push_cxt) and leaving a function.
Entering a signal handler will push a 0 to the function stack
to make the context only dependend on the call chain inside of
the signal handler.
Thus, delivering two signals in a row should push two times a
0 value onto the function stack. However, the second 0-push was
incorrectly suppressed, leading to a failed assertion when
returning from the 2nd signal handler.

This also fixes a bug with incorrectly zeroing global cost
counters when changing the execution context, introduced with
r10386.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@10399 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/callgrind/threads.c b/callgrind/threads.c
index d08bb72..196fb87 100644
--- a/callgrind/threads.c
+++ b/callgrind/threads.c
@@ -208,17 +208,16 @@
     /* save current execution state */
     exec_state_save();
 
-    /* setup current state for a spontaneous call */
-    CLG_(init_exec_state)( &CLG_(current_state) );
-    CLG_(push_cxt)(0);
-
     /* setup new cxtinfo struct for this signal handler */
     es = push_exec_state(sigNum);
-    CLG_(init_cost)( CLG_(sets).full, es->cost);
+    // because of this, below call to init_exec_state will zero es->cost
     CLG_(current_state).cost = es->cost;
     es->call_stack_bottom = CLG_(current_call_stack).sp;
 
+    /* setup current state for a spontaneous call */
+    CLG_(init_exec_state)( &CLG_(current_state) );
     CLG_(current_state).sig = sigNum;
+    CLG_(push_cxt)(0);
 }
 
 /* Run post-signal if the stackpointer for call stack is at
@@ -418,6 +417,7 @@
   es->jmps_passed = CLG_(current_state).jmps_passed;
   es->bbcc        = CLG_(current_state).bbcc;
   es->nonskipped  = CLG_(current_state).nonskipped;
+  CLG_ASSERT(es->cost == CLG_(current_state).cost);
 
   CLG_DEBUGIF(1) {
     CLG_DEBUG(1, "  cxtinfo_save(sig %d): collect %s, jmps_passed %d\n",