Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SURF: Embeed every fields of common_public directly into s_surf_model_t
[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
94     ("    If it fails, try another context backend.\n    For example, to force the pthread backend, use:\n       ./configure --with-context=pthread\n\n");
95 #endif
96
97   xbt_init(&argc, argv);
98
99   cA = xbt_context_new("A", fA, NULL, NULL, NULL, NULL, 0, NULL);
100   cB = xbt_context_new("B", fB, NULL, NULL, NULL, NULL, 0, NULL);
101   cC = xbt_context_new("C", fC, NULL, NULL, NULL, NULL, 0, NULL);
102
103   fifo = xbt_fifo_new();
104
105   printf("Here is context 'main'\n");
106   printf("\tPush context 'A' (%p) from context 'main'\n", cA);
107   xbt_fifo_push(fifo, cA);
108   xbt_context_start(cA);
109
110   printf("\tPush context 'B' (%p) from context 'main'\n", cB);
111   xbt_fifo_push(fifo, cB);
112   xbt_context_start(cB);
113
114   printf("\tPush context 'C' (%p) from context 'main'\n", cC);
115   xbt_fifo_push(fifo, cC);
116   xbt_context_start(cC);
117
118   while ((context = xbt_fifo_shift(fifo))) {
119     printf("Context main: schedule\n");
120     xbt_context_schedule(context);
121   }
122
123   printf("\tFreeing Fifo\n");
124   xbt_fifo_free(fifo);
125   printf("\tExit & cleaning living threads\n");
126   xbt_exit();
127
128   cA = cB = cC = NULL;
129   printf("Context main: Bye\n");
130   return 0;
131 }