[m-rev.] for review: Support dynamic creation of Mercury engines in low-level C parallel grades.

Peter Wang novalazy at gmail.com
Thu Jun 19 14:27:16 AEST 2014


On Wed, 18 Jun 2014 17:27:26 +1000, Peter Wang <novalazy at gmail.com> wrote:
> 
> 	Make MR_find_ready_context respect MR_ctxt_exclusive_engine.
> 
> 	Make MR_schedule_context respect MR_ctxt_exclusive_engine.

These changes were buggy.  MR_ctxt_resume_c_depth should only be taken
into account when MR_ctxt_resume_engine_required = TRUE, not merely
because MR_ctxt_exclusive_engine is set.

Peter

diff --git a/runtime/mercury_context.c b/runtime/mercury_context.c
index 382aa7d..f695c36 100644
--- a/runtime/mercury_context.c
+++ b/runtime/mercury_context.c
@@ -1263,9 +1263,6 @@ MR_find_ready_context(void)
     preferred_context = NULL;
     preferred_context_prev = NULL;
     while (cur != NULL) {
-        MR_bool     specific_engine_required;
-        MR_EngineId specific_engine;
-
       #ifdef MR_DEBUG_THREADS
         if (MR_debug_threads) {
             fprintf(stderr,
@@ -1275,29 +1272,18 @@ MR_find_ready_context(void)
         }
       #endif
 
-        if (cur->MR_ctxt_exclusive_engine != MR_ENGINE_ID_NONE) {
-            specific_engine_required = MR_TRUE;
-            specific_engine = cur->MR_ctxt_exclusive_engine;
-        } else if (cur->MR_ctxt_resume_engine_required) {
-            specific_engine_required = MR_TRUE;
-            specific_engine = cur->MR_ctxt_resume_engine;
-        } else {
-            specific_engine_required = MR_FALSE;
-            specific_engine = MR_ENGINE_ID_NONE;
-        }
-
-        if (specific_engine_required) {
+        if (cur->MR_ctxt_resume_engine_required == MR_TRUE) {
           #ifdef MR_DEBUG_THREADS
             if (MR_debug_threads) {
                 fprintf(stderr,
                     "%ld Context %p requires engine %d and c_depth %"
                     MR_INTEGER_LENGTH_MODIFIER "u\n",
-                    MR_SELF_THREAD_ID, cur, specific_engine,
+                    MR_SELF_THREAD_ID, cur, cur->MR_ctxt_resume_engine,
                     cur->MR_ctxt_resume_c_depth);
             }
           #endif
-            if (specific_engine == engine_id &&
-                cur->MR_ctxt_resume_c_depth == depth)
+            if ((cur->MR_ctxt_resume_engine == engine_id) &&
+                (cur->MR_ctxt_resume_c_depth == depth))
             {
                 preferred_context = cur;
                 preferred_context_prev = prev;
@@ -1307,6 +1293,22 @@ MR_find_ready_context(void)
                 */
                 break;
             }
+        } else if (cur->MR_ctxt_exclusive_engine != MR_ENGINE_ID_NONE) {
+          #ifdef MR_DEBUG_THREADS
+            if (MR_debug_threads) {
+                fprintf(stderr,
+                    "%ld Context %p requires exclusive engine %d\n",
+                    MR_SELF_THREAD_ID, cur, cur->MR_ctxt_exclusive_engine);
+            }
+          #endif
+            if (cur->MR_ctxt_exclusive_engine == engine_id) {
+                /*
+                ** This context is exclusive to this engine.
+                */
+                preferred_context = cur;
+                preferred_context_prev = prev;
+                break;
+            }
         } else {
           #ifdef MR_DEBUG_THREADS
             if (MR_debug_threads) {
@@ -1602,13 +1604,17 @@ MR_schedule_context(MR_Context *ctxt)
     /*
     ** Try to give this context straight to the engine that would execute it.
     */
-    if (ctxt->MR_ctxt_exclusive_engine != MR_ENGINE_ID_NONE) {
+    if (ctxt->MR_ctxt_resume_engine_required == MR_TRUE) {
+        engine_id = ctxt->MR_ctxt_resume_engine;
+        engine_required = MR_TRUE;
+    } else if (ctxt->MR_ctxt_exclusive_engine != MR_ENGINE_ID_NONE) {
         engine_id = ctxt->MR_ctxt_exclusive_engine;
         engine_required = MR_TRUE;
     } else {
         engine_id = ctxt->MR_ctxt_resume_engine;
-        engine_required = ctxt->MR_ctxt_resume_engine_required;
+        engine_required = MR_FALSE;
     }
+
     esync = get_engine_sleep_sync_data(engine_id);
 #ifdef MR_DEBUG_THREADS
     if (MR_debug_threads) {

diff --git a/runtime/mercury_context.h b/runtime/mercury_context.h
index e033ef4..f8ce05b 100644
--- a/runtime/mercury_context.h
+++ b/runtime/mercury_context.h
@@ -133,15 +133,16 @@
 **
 ** resume_engine_required
 ** resume_c_depth
-**                  These fields are used to ensure that when we enter a
-**                  Mercury engine from C, we return to the same engine. If
-**                  resume_engine_required is MR_FALSE then resume_engine is
-**                  simply a preference. Otherwise the resume_engine and
-**                  resume_c_depth must match the engine's id and c_depth. See
-**                  the comments in mercury_engine.h.  (Both accessed only when
-**                  directly specifying the context.)
-**
-** saved_owners
+**                  If resume_engine_required is MR_FALSE then resume_engine is
+**                  simply a preference, and the resume_c_depth field has no
+**                  meaning. If resume_engine_required is MR_TRUE then
+**                  resume_engine and resume_c_depth must match the engine's id
+**                  and c_depth, to ensure that when we enter a Mercury engine
+**                  from C we return to the same engine. See the comments in
+**                  mercury_engine.h.
+**                  (Both accessed only when directly specifying the context.)
+**
+** resume_stack
 **                  A stack used to record the Mercury engines on which this
 **                  context executed some C calls that called back into
 **                  Mercury. We must execute this context in the correct



More information about the reviews mailing list