From 2e84ce54bbf86ec43020db5c5fd7701dc6adb61c Mon Sep 17 00:00:00 2001
From: Mathieu Faverge <mathieu.faverge@inria.fr>
Date: Wed, 16 Feb 2022 21:59:28 +0100
Subject: [PATCH] Add a pause field in the chamctxt structure to check if the
 runtime is paused when syncrhonizing the system

---
 control/async.c            |  7 +++++++
 control/context.c          |  2 ++
 control/control.c          | 10 ++++++++++
 include/chameleon/struct.h |  1 +
 4 files changed, 20 insertions(+)

diff --git a/control/async.c b/control/async.c
index 5eea9670b..1e33237a8 100644
--- a/control/async.c
+++ b/control/async.c
@@ -69,7 +69,14 @@ int chameleon_sequence_destroy(CHAM_context_t *chamctxt, RUNTIME_sequence_t *seq
  */
 int chameleon_sequence_wait(CHAM_context_t *chamctxt, RUNTIME_sequence_t *sequence)
 {
+    if ( chamctxt->runtime_paused ) {
+        chameleon_warning("chameleon_sequence_wait()", "CHAMELEON was paused, so it is resumed and it will be paused after the wait");
+        RUNTIME_resume( chamctxt );
+    }
     RUNTIME_sequence_wait( chamctxt, sequence );
+    if ( chamctxt->runtime_paused ) {
+        RUNTIME_pause( chamctxt );
+    }
     return CHAMELEON_SUCCESS;
 }
 
diff --git a/control/context.c b/control/context.c
index a633ecc79..cda18348a 100644
--- a/control/context.c
+++ b/control/context.c
@@ -77,6 +77,8 @@ CHAM_context_t *chameleon_context_create()
     chamctxt->progress_enabled   = CHAMELEON_FALSE;
     chamctxt->generic_enabled    = CHAMELEON_FALSE;
 
+    chamctxt->runtime_paused     = CHAMELEON_FALSE;
+
     chamctxt->householder        = ChamFlatHouseholder;
     chamctxt->translation        = ChamOutOfPlace;
 
diff --git a/control/control.c b/control/control.c
index f1688b2eb..778e4f2e6 100644
--- a/control/control.c
+++ b/control/control.c
@@ -194,6 +194,11 @@ int CHAMELEON_Pause(void)
         chameleon_error("CHAMELEON_Pause()", "CHAMELEON not initialized");
         return CHAMELEON_ERR_NOT_INITIALIZED;
     }
+    if ( chamctxt->runtime_paused ) {
+        chameleon_warning("CHAMELEON_Pause()", "CHAMELEON already paused");
+        return CHAMELEON_SUCCESS;
+    }
+    chamctxt->runtime_paused = CHAMELEON_TRUE;
     RUNTIME_pause(chamctxt);
     return CHAMELEON_SUCCESS;
 }
@@ -217,6 +222,11 @@ int CHAMELEON_Resume(void)
         chameleon_error("CHAMELEON_Resume()", "CHAMELEON not initialized");
         return CHAMELEON_ERR_NOT_INITIALIZED;
     }
+    if ( !chamctxt->runtime_paused ) {
+        chameleon_warning("CHAMELEON_Resume()", "CHAMELEON was already resumed");
+        return CHAMELEON_SUCCESS;
+    }
+    chamctxt->runtime_paused = CHAMELEON_FALSE;
     RUNTIME_resume(chamctxt);
     return CHAMELEON_SUCCESS;
 }
diff --git a/include/chameleon/struct.h b/include/chameleon/struct.h
index 33f19584e..6da47e1c9 100644
--- a/include/chameleon/struct.h
+++ b/include/chameleon/struct.h
@@ -137,6 +137,7 @@ typedef struct chameleon_context_s {
     cham_bool_t        profiling_enabled;
     cham_bool_t        progress_enabled;
     cham_bool_t        generic_enabled;
+    cham_bool_t        runtime_paused;
 
     cham_householder_t householder;        // "domino" (flat) or tree-based (reduction) Householder
     cham_translation_t translation;        // In place or Out of place layout conversion
-- 
GitLab