Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Substitution of the word "resource" by "model" in every surf related identifier
[simgrid.git] / testsuite / xbt / context_usage.c
index 063c7d3..1392e23 100644 (file)
@@ -1,7 +1,11 @@
+#ifdef __BORLANDC__
+#pragma hdrstop
+#endif
+
+#include "xbt.h"
 #include "xbt/context.h"
+#include "portable.h" /* To know whether we're using threads or context */
 #include "xbt/fifo.h"
-#include "stdlib.h"
-#include "stdio.h"
 
 xbt_context_t cA = NULL;
 xbt_context_t cB = NULL;
@@ -13,7 +17,7 @@ void print_args(int argc, char** argv)
 {
   int i ; 
 
-  printf("<");
+  printf("args=<");
   for(i=0; i<argc; i++) 
     printf("%s ",argv[i]);
   printf(">\n");
@@ -22,14 +26,19 @@ void print_args(int argc, char** argv)
 int fA(int argc, char** argv);
 int fA(int argc, char** argv)
 {
-  printf("fA: ");
+  printf("Here is fA: ");
   print_args(argc,argv);
 
-  printf("\tA: Yield\n");
-  xbt_context_yield();
-  printf("\tA: Yield\n");
+/*   printf("\tContext A: Yield\n"); */
+/*   xbt_context_yield(); // FIXME: yielding to itself fails, no idea why */
+   
+  printf("\tContext A: Push context B\n");
+  xbt_fifo_push(fifo,cB);
+
+  printf("\tContext A: Yield\n");
   xbt_context_yield();
-  printf("\tA : bye\n");
+
+  printf("\tContext A: bye\n");
 
   return 0;
 }
@@ -37,16 +46,19 @@ int fA(int argc, char** argv)
 int fB(int argc, char** argv);
 int fB(int argc, char** argv)
 {
-  printf("fB: ");
+  printf("Here is fB: ");
   print_args(argc,argv);
 
-  printf("\tB: Yield\n");
+  printf("\tContext B: Yield\n");
   xbt_context_yield();
+
+  printf("\tContext B: Push context A\n");
   xbt_fifo_push(fifo,cA);
-  printf("\tB->A\n");
-  printf("\tB: Yield\n");
+
+  printf("\tContext B: Yield\n");
   xbt_context_yield();
-  printf("\tB : bye\n");
+
+  printf("\tContext B: bye\n");
 
   return 0;
 }
@@ -54,44 +66,65 @@ int fB(int argc, char** argv)
 int fC(int argc, char** argv);
 int fC(int argc, char** argv)
 {
-  printf("fC: ");
+  printf("Here is fC: ");
   print_args(argc,argv);
 
-
-  printf("\tC: Yield\n");
+  printf("\tContext C: Yield\n");
   xbt_context_yield();
 
+  printf("\tContext C: bye\n");
 
   return 0;
 }
 
+#ifdef __BORLANDC__
+#pragma argsused
+#endif
+
 int main(int argc, char** argv)
 {
   xbt_context_t context = NULL;
 
-  xbt_context_init();
+  printf("XXX Test the simgrid context API\n");
+#if CONTEXT_THREADS
+  printf("XXX Using threads as a backend.\n");
+#else /* use SUSv2 contexts */
+  printf("XXX Using SUSv2 contexts as a backend.\n");
+  printf("    If it fails, try another context backend.\n    For example, to force the pthread backend, use:\n       ./configure --with-context=pthread\n\n");
+#endif
+   
+  xbt_init(&argc, argv);
 
-  cA = xbt_context_new(fA, NULL, NULL, NULL, NULL, 0, NULL);
-  cB = xbt_context_new(fB, NULL, NULL, NULL, NULL, 0, NULL);
-  cC = xbt_context_new(fC, NULL, NULL, NULL, NULL, 0, NULL);
+  cA = xbt_context_new("A",fA, NULL, NULL, NULL, NULL, 0, NULL);
+  cB = xbt_context_new("B",fB, NULL, NULL, NULL, NULL, 0, NULL);
+  cC = xbt_context_new("C",fC, NULL, NULL, NULL, NULL, 0, NULL);
 
   fifo = xbt_fifo_new();
 
+  printf("Here is context 'main'\n");
+  printf("\tPush context 'A' (%p) from context 'main'\n",cA);
+  xbt_fifo_push(fifo,cA); 
   xbt_context_start(cA);
-  printf("\tO->A\n");xbt_fifo_push(fifo,cA);
+
+  printf("\tPush context 'B' (%p) from context 'main'\n",cB);
+  xbt_fifo_push(fifo,cB);
   xbt_context_start(cB);
-  printf("\tO->B\n");xbt_fifo_push(fifo,cB);
-  xbt_context_start(cC);xbt_fifo_push(fifo,cC);
-  printf("\tO->C\n");xbt_fifo_push(fifo,cC);
+
+  printf("\tPush context 'C' (%p) from context 'main'\n",cC);
+  xbt_fifo_push(fifo,cC); 
+  xbt_context_start(cC);
 
   while((context=xbt_fifo_shift(fifo))) {
-    printf("\tO:Yield\n");
+    printf("Context main: schedule\n");
     xbt_context_schedule(context);
   }
 
+  printf("\tFreeing Fifo\n");
   xbt_fifo_free(fifo);
-  xbt_context_exit();
+  printf("\tExit & cleaning living threads\n");
+  xbt_exit();
   
   cA=cB=cC=NULL;
+  printf("Context main: Bye\n");
   return 0;
 }