[m-rev.] diff: Fix a crash in loop-controlled code.

Paul Bone pbone at csse.unimelb.edu.au
Fri Oct 14 11:52:16 AEDT 2011


Fix a crash in loop-controlled code.

Loop controlled code would sometimes find, while signaling a future,
that the future had already been signaled and had a different value.

The problem was that the MR_lc_join_and_terminate operation would only
save the context after marking the loop control slot (that owned the
context) as free.  The save context code and the MR_lc_spawn_off code
both write the parent stack pointer field in the context structure, If
these (or other cases where the context is saved - such as when it
blocks on a future) race, then they can cause problems when the spawned
off computation uses its parent stack pointer.

runtime/mercury_par_builtin.h:
    MR_lc_join_and_terminate now saves the context before calling
    MR_lc_join.

Index: runtime/mercury_par_builtin.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_par_builtin.h,v
retrieving revision 1.12
diff -u -p -b -r1.12 mercury_par_builtin.h
--- runtime/mercury_par_builtin.h	9 Oct 2011 09:56:20 -0000	1.12
+++ runtime/mercury_par_builtin.h	14 Oct 2011 00:28:46 -0000
@@ -501,8 +501,6 @@ extern void MR_lc_spawn_off_func(MR_Loop
 */
 #define MR_lc_join_and_terminate(lc, lcs_idx)                               \
     do {                                                                    \
-        MR_lc_join((lc), (lcs_idx));                                        \
-                                                                            \
         /*                                                                  \
         ** Termination of this context must be handled in a macro so that   \
         ** C's stack pointer is set correctly. It might appear that we      \
@@ -511,9 +509,15 @@ extern void MR_lc_spawn_off_func(MR_Loop
         ** A similar mistake was the cause of a hard-to-diagnose bug in     \
         ** parallel stack segments grades.                                  \
         ** XXX give a pointer to a description of that bug.                 \
+        ** The context must be saved before we free the loop control slot,  \
+        ** otherwise another engine may begin using it before we've saved   \
+        ** it.                                                              \
         */                                                                  \
         MR_save_context(MR_ENGINE(MR_eng_this_context));                    \
         MR_ENGINE(MR_eng_this_context) = NULL;                              \
+                                                                            \
+        MR_lc_join((lc), (lcs_idx));                                        \
+                                                                            \
         MR_idle();                                                          \
     } while (0);
 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: Digital signature
URL: <http://lists.mercurylang.org/archives/reviews/attachments/20111014/dcd02e18/attachment.sig>


More information about the reviews mailing list