Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
last version of tesh
[simgrid.git] / tools / tesh2 / src / units.c
1 #include <units.h>
2 #include <unit.h>
3 #include <fstreams.h>
4
5 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
6
7 units_t
8 units_new(runner_t runner, fstreams_t fstreams)
9 {
10         register fstream_t fstream;
11         
12         units_t units =  xbt_new0(s_units_t, 1);
13         
14         if(!(units->items = vector_new(fstreams_get_size(fstreams), unit_free)))
15         {
16                 free(units);
17                 return NULL;
18         }
19         
20         
21         vector_rewind(fstreams->items);
22         
23         while((fstream = vector_get(fstreams->items)))
24         {
25                 if(vector_push_back(units->items, unit_new(runner, NULL, NULL, fstream)))
26                 {
27                         vector_free(&(units->items));
28                         free(units);
29                         return NULL;    
30                 }
31                 
32                 vector_move_next(fstreams->items);
33         }
34         
35         return units;
36 }
37
38
39 int
40 units_is_empty(units_t units)
41 {
42         if(!units)
43         {
44                 errno = EINVAL;
45                 return 0;
46         }
47         
48         return vector_is_empty(units->items);
49 }
50
51 int
52 units_get_size(units_t units)
53 {
54         if(!units)
55         {
56                 errno = EINVAL;
57                 return -1;
58         }
59         
60         return vector_get_size(units->items);
61 }
62
63
64 int
65 units_run_all(units_t units, xbt_os_mutex_t mutex)
66 {
67         register unit_t unit;
68         
69         if(!units)
70                 return EINVAL;
71                 
72         if(vector_is_empty(units->items))
73                 return EAGAIN;
74                 
75         /* move the cursor at the begining of the vector */
76         vector_rewind(units->items);
77         
78         /* run all the units */
79         while((unit = vector_get(units->items)))
80         {
81                 unit_run(unit, mutex);
82                 vector_move_next(units->items);
83                 
84         }
85         
86         return 0;
87         
88 }
89
90 int
91 units_join_all(units_t units)
92 {
93         register unit_t unit;
94         
95         if(!units)
96                 return EINVAL;
97                 
98         if(vector_is_empty(units->items))
99                 return EAGAIN;
100                 
101         /* move the cursor at the begining of the vector */
102         vector_rewind(units->items);
103         
104         /* run all the units */
105         while((unit = vector_get(units->items)))
106         {
107                 if(unit->thread)
108                         xbt_os_thread_join(unit->thread, NULL);
109                         
110                 vector_move_next(units->items);
111         }
112         
113         return 0;
114 }
115
116 int
117 units_interrupt_all(units_t units)
118 {
119         register unit_t unit;
120         
121         if(!units)
122                 return EINVAL;
123                 
124         if(vector_is_empty(units->items))
125                 return EAGAIN;
126                 
127         /* move the cursor at the begining of the vector */
128         vector_rewind(units->items);
129         
130         /* run all the units */
131         while((unit = vector_get(units->items)))
132         {
133                 if(!(unit->successeded) && !(unit->interrupted))
134                         unit_interrupt(unit);
135                         
136                 vector_move_next(units->items);
137         }
138         
139         return 0;
140 }
141
142 int
143 units_verbose(units_t units)
144 {
145         register unit_t unit;
146         
147         if(!units)
148                 return EINVAL;
149                 
150         if(vector_is_empty(units->items))
151                 return EAGAIN;
152                 
153         /* move the cursor at the begining of the vector */
154         vector_rewind(units->items);
155         
156         /* run all the units */
157         while((unit = vector_get(units->items)))
158         {
159                 unit_verbose(unit);
160                 vector_move_next(units->items);
161         }
162         
163         return 0;
164         
165 }
166
167 int
168 units_reset_all(units_t units)
169 {
170         register unit_t unit;
171         
172         if(!units)
173                 return EINVAL;
174                 
175         if(vector_is_empty(units->items))
176                 return EAGAIN;
177                 
178         /* move the cursor at the begining of the vector */
179         vector_rewind(units->items);
180         
181         /* run all the units */
182         while((unit = vector_get(units->items)))
183         {
184                 unit_reset(unit);
185                 vector_move_next(units->items);
186         }
187         
188         return 0;
189 }
190
191 int
192 units_free(void** unitsptr)
193 {
194         int rv;
195         
196         if(!(*unitsptr))
197                 return EINVAL;
198         
199         if((rv = vector_free(&((*((units_t*)unitsptr))->items))))
200                 return rv;
201                 
202         free(*unitsptr);
203         *unitsptr = NULL;
204         
205         return 0;
206 }
207