From c72e7f1fbeeaac356176691227f36c65f399a94e Mon Sep 17 00:00:00 2001
From: Thierno-Souleymane BAH <thiernosouleymane.bah.etu@univ-lille.fr>
Date: Sat, 23 Jan 2021 01:56:03 +0100
Subject: [PATCH] fix(issue concerning ctx fixed, now finish correctly)

---
 pingpongpang.c |  2 +-
 try.c          | 24 +++++++++++++++++++++++-
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/pingpongpang.c b/pingpongpang.c
index 980f391..2942799 100644
--- a/pingpongpang.c
+++ b/pingpongpang.c
@@ -16,7 +16,7 @@ int main(int argc, char *argv[])
     printf("Begin \n");
     yield();
     printf("End \n");
-    exit(EXIT_SUCCESS);
+    return EXIT_SUCCESS;
 }
 
 void f_ping(void *args)
diff --git a/try.c b/try.c
index 46c1825..dc7c6a8 100644
--- a/try.c
+++ b/try.c
@@ -1,4 +1,5 @@
 #include <stdlib.h>
+#include <stdio.h>
 #include <assert.h>
 #include "try.h"
 
@@ -43,6 +44,21 @@ int init_ctx(ctx_t *pctx, int stack_size, funct_t f, void *args)
 
 void switch_to_ctx(ctx_t *pctx)
 {
+
+    if (pctx->status == CTX_END)
+    {
+        // Si c'est l'unique ctx
+        if (pctx == pctx->next_ctx)
+            ring_ctx = NULL;
+        else
+            current_ctx->next_ctx = pctx->next_ctx;
+
+        free(pctx->stack);
+        free(pctx);
+        yield();
+        return;
+    }
+
     assert(pctx->status != CTX_END);
     if (current_ctx != NULL)
     {
@@ -93,7 +109,13 @@ ctx_t *create_ctx(int stack_size, funct_t f, void *args)
 
 void yield()
 {
-    assert(ring_ctx != NULL || current_ctx != NULL);
+    //printf("%p, %p\n", current_ctx, ring_ctx);
+    if (ring_ctx == NULL)
+    {
+        printf("\nEnd \n");
+        exit(EXIT_SUCCESS);
+    }
+
     if (current_ctx == NULL)
         switch_to_ctx(ring_ctx->next_ctx);
     else
-- 
GitLab