Logo AND Algorithmique Numérique Distribuée

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