Logo AND Algorithmique Numérique Distribuée

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