Logo AND Algorithmique Numérique Distribuée

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