Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
.
[simgrid.git] / testsuite / xbt / context_usage.c
1 #include "xbt/context.h"
2 #include "xbt/fifo.h"
3 #include "stdlib.h"
4 #include "stdio.h"
5
6 xbt_context_t cA = NULL;
7 xbt_context_t cB = NULL;
8 xbt_context_t cC = NULL;
9 xbt_fifo_t fifo = NULL;
10
11 void print_args(int argc, char** argv);
12 void print_args(int argc, char** argv)
13 {
14   int i ; 
15
16   printf("args=<");
17   for(i=0; i<argc; i++) 
18     printf("%s ",argv[i]);
19   printf(">\n");
20 }
21
22 int fA(int argc, char** argv);
23 int fA(int argc, char** argv)
24 {
25   printf("Here is fA: ");
26   print_args(argc,argv);
27
28   printf("\tContext A: Yield\n");
29   xbt_context_yield();
30    
31   xbt_fifo_push(fifo,cB);
32   printf("\tPush context B from context A\n");
33
34    printf("\tContext A: Yield\n");
35   xbt_context_yield();
36   printf("\tContext A : bye\n");
37
38   return 0;
39 }
40
41 int fB(int argc, char** argv);
42 int fB(int argc, char** argv)
43 {
44   printf("Here is fB: ");
45   print_args(argc,argv);
46
47 //  printf("\tContext B: Yield\n");
48 //  xbt_context_yield();
49   xbt_fifo_push(fifo,cA);
50   printf("\tPush context A from context B\n");
51   printf("\tContext B: Yield\n");
52   xbt_context_yield();
53   printf("\tContext B : bye\n");
54
55   return 0;
56 }
57
58 int fC(int argc, char** argv);
59 int fC(int argc, char** argv)
60 {
61   printf("Here is fC: ");
62   print_args(argc,argv);
63
64
65   printf("\tContext C: Yield (and exit)\n");
66   xbt_context_yield();
67
68
69   return 0;
70 }
71
72 int main(int argc, char** argv)
73 {
74   xbt_context_t context = NULL;
75
76   printf("XXX Test the simgrid context API\n");
77   printf("    If it fails, try another context backend.\n    For example, to force the pthread backend, use:\n       ./configure --with-context=pthread\n\n");
78    
79   xbt_context_init();
80
81   cA = xbt_context_new(fA, NULL, NULL, NULL, NULL, 0, NULL);
82   cB = xbt_context_new(fB, NULL, NULL, NULL, NULL, 0, NULL);
83   cC = xbt_context_new(fC, NULL, NULL, NULL, NULL, 0, NULL);
84
85   fifo = xbt_fifo_new();
86
87   printf("Here is context 'main'\n");
88   xbt_context_start(cA);
89   printf("\tPush context 'A' from context 'main'\n");xbt_fifo_push(fifo,cA);
90   xbt_context_start(cB);
91   printf("\tPush context 'B' from context 'main'\n");xbt_fifo_push(fifo,cB);
92   xbt_context_start(cC);xbt_fifo_push(fifo,cC);
93   printf("\tPush context 'C' from context 'main'\n");xbt_fifo_push(fifo,cC);
94
95   while((context=xbt_fifo_shift(fifo))) {
96     printf("Context main: Yield\n");
97     xbt_context_schedule(context);
98   }
99
100   xbt_fifo_free(fifo);
101   xbt_context_exit();
102   
103   cA=cB=cC=NULL;
104   return 0;
105 }