Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
044ce4107f59843c21e96efc36d11af59a5476b7
[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 "portable.h" /* To know whether we're using threads or context */
8 #include "xbt/fifo.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(); // FIXME: yielding to itself fails, no idea why */
34    
35   printf("\tContext A: Push context B\n");
36   xbt_fifo_push(fifo,cB);
37
38   printf("\tContext A: Yield\n");
39   xbt_context_yield();
40
41   printf("\tContext A: bye\n");
42
43   return 0;
44 }
45
46 int fB(int argc, char** argv);
47 int fB(int argc, char** argv)
48 {
49   printf("Here is fB: ");
50   print_args(argc,argv);
51
52   printf("\tContext B: Yield\n");
53   xbt_context_yield();
54
55   printf("\tContext B: Push context A\n");
56   xbt_fifo_push(fifo,cA);
57
58   printf("\tContext B: Yield\n");
59   xbt_context_yield();
60
61   printf("\tContext B: bye\n");
62
63   return 0;
64 }
65
66 int fC(int argc, char** argv);
67 int fC(int argc, char** argv)
68 {
69   printf("Here is fC: ");
70   print_args(argc,argv);
71
72   printf("\tContext C: Yield\n");
73   xbt_context_yield();
74
75   printf("\tContext C: bye\n");
76
77   return 0;
78 }
79
80 #ifdef __BORLANDC__
81 #pragma argsused
82 #endif
83
84 int main(int argc, char** argv)
85 {
86   xbt_context_t context = NULL;
87
88   printf("XXX Test the simgrid context API\n");
89 #if CONTEXT_THREADS
90   printf("XXX Using threads as a backend.\n");
91 #else /* use SUSv2 contexts */
92   printf("XXX Using SUSv2 contexts as a backend.\n");
93   printf("    If it fails, try another context backend.\n    For example, to force the pthread backend, use:\n       ./configure --with-context=pthread\n\n");
94 #endif
95    
96   xbt_init(&argc, argv);
97
98   cA = xbt_context_new(fA, NULL, NULL, NULL, NULL, 0, NULL);
99   cB = xbt_context_new(fB, NULL, NULL, NULL, NULL, 0, NULL);
100   cC = xbt_context_new(fC, NULL, NULL, NULL, NULL, 0, NULL);
101
102   fifo = xbt_fifo_new();
103
104   printf("Here is context 'main'\n");
105   printf("\tPush context 'A' (%p) from context 'main'\n",cA);
106   xbt_fifo_push(fifo,cA); 
107   xbt_context_start(cA);
108
109   printf("\tPush context 'B' (%p) from context 'main'\n",cB);
110   xbt_fifo_push(fifo,cB);
111   xbt_context_start(cB);
112
113   printf("\tPush context 'C' (%p) from context 'main'\n",cC);
114   xbt_fifo_push(fifo,cC); 
115   xbt_context_start(cC);
116
117   while((context=xbt_fifo_shift(fifo))) {
118     printf("Context main: schedule\n");
119     xbt_context_schedule(context);
120   }
121
122   printf("\tFreeing Fifo\n");
123   xbt_fifo_free(fifo);
124   printf("\tExit & cleaning living threads\n");
125   xbt_exit();
126   
127   cA=cB=cC=NULL;
128   printf("Context main: Bye\n");
129   return 0;
130 }