summaryrefslogtreecommitdiff
path: root/src/schedule/ScheduleTraverse.c
blob: 0a3afb6bb40b0b5e0c717e70396272698a82c70c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
 /*@@
   @file      ScheduleTraverse.c
   @date      Thu Sep 16 08:58:37 1999
   @author    Tom Goodale
   @desc 
   Routins to traverse schedule groups.
   @enddesc 
 @@*/

#include <stdio.h>
#include <stdlib.h>

#include "StoreHandledData.h"
#include "Schedule.h"

static char *rcsid = "$Header$";

/* Local routine prototypes */
static int ScheduleTraverseGroup(cHandledData *schedule_groups, 
                                 t_sched_group *group,
                                 void *attributes,
                                 int n_whiles,
                                 char **whiles,
                                 int (*item_entry)(void *, void *),
                                 int (*item_exit)(void *, void *),
                                 int  (*while_check)(int, char **, void *, void *),
                                 int (*function_process)(void *, void *, void *),
                                 void *data);

static int ScheduleTraverseFunction(void *function,
                                    void *attributes,
                                    int n_whiles,
                                    char **whiles,
                                    int (*item_entry)(void *, void *),
                                    int (*item_exit)(void *, void *),
                                    int  (*while_check)(int, char **, void *, void *),
                                    int (*function_process)(void *, void *, void *),
                                    void *data);

/********************************************************************
 ********************    External Routines   ************************
 ********************************************************************/

 /*@@
   @routine    CCTKi_ScheduleTraverse
   @date       Thu Sep 16 09:05:23 1999
   @author     Tom Goodale
   @desc 
   Traverses the group with the given name.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int CCTKi_ScheduleTraverse(const char *group_name,
                           int (*item_entry)(void *, void *),
                           int (*item_exit)(void *, void *),
                           int  (*while_check)(int, char **, void *, void *),
                           int (*function_process)(void *, void *, void *),
                           void *data)
{
  cHandledData *schedule_groups;
  t_sched_group *group;
  int handle;
  int retcode;

  schedule_groups = CCTKi_ScheduleGetGroups();

  handle = Util_GetHandle(schedule_groups, group_name, (void *)&group);

  if(handle >= 0)
  {
    retcode = ScheduleTraverseGroup(schedule_groups, 
                                    group, 
                                    NULL,
                                    0,
                                    NULL,
                                    item_entry, 
                                    item_exit, 
                                    while_check, 
                                    function_process,
                                    data);
  }
  else
  {
    retcode = handle;
  }

  return retcode;
}

/********************************************************************
 *********************     Local Routines   *************************
 ********************************************************************/

 /*@@
   @routine    ScheduleTraverseGroup
   @date       Thu Sep 16 09:07:44 1999
   @author     Tom Goodale
   @desc 
   Traverses the given schedule group.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
static int ScheduleTraverseGroup(cHandledData *schedule_groups, 
                                 t_sched_group *group,
                                 void *attributes,
                                 int n_whiles,
                                 char **whiles,
                                 int (*item_entry)(void *, void *),
                                 int (*item_exit)(void *, void *),
                                 int  (*while_check)(int, char **, void *, void *),
                                 int (*function_process)(void *, void *, void *),
                                 void *data)
{
  int item;
  int doit;
  int called_item_entry;
  t_sched_group *newgroup;

  /* If there is a while-list associated with this item, check if the group should be
   * exectuted at all.
   */

  if(n_whiles > 0 && while_check)
  {
    doit = while_check(n_whiles, whiles, attributes, data);
  }
  else
  {
    doit = 1;
  }

  /* Call a item entry function if it is defined. */
  if(doit)
  {
    called_item_entry = 1;

    if(item_entry)
    {
      doit = item_entry(attributes, data);
    }
  }
  else
  {
    called_item_entry = 0;
  }

  /* Now traverse the group. */
  while(doit )
  {
      
    /* Traverse in the sorted order - assumes group has been sorted ! */
    for(item = 0 ; item < group->n_scheditems; item++)
    {
      switch(group->scheditems[group->order[item]].type)
      {
        case sched_function :
          ScheduleTraverseFunction(group->scheditems[group->order[item]].function, 
                                   group->scheditems[group->order[item]].attributes,
                                   group->scheditems[group->order[item]].n_whiles,
                                   group->scheditems[group->order[item]].whiles,
                                   item_entry,
                                   item_exit,                                  
                                   while_check,
                                   function_process,
                                   data);
          break;
        case sched_group :
          newgroup = (t_sched_group *)Util_GetHandledData(schedule_groups, 
                                                          group->scheditems[group->order[item]].group);
          ScheduleTraverseGroup(schedule_groups,
                                newgroup, 
                                group->scheditems[group->order[item]].attributes,
                                group->scheditems[group->order[item]].n_whiles,
                                group->scheditems[group->order[item]].whiles,
                                item_entry, 
                                item_exit, 
                                while_check, 
                                function_process,
                                data);
          break;
        default :
          fprintf(stderr, "Unknown schedule item type %d\n", group->scheditems[group->order[item]].type);
      }
    }

    /* Check the while_list again. */
    if(n_whiles > 0 && while_check)
    {
      doit = while_check(n_whiles, whiles, attributes, data) ;
    }
    else
    {
      doit = 0;
    }
  }

  /* Call the group_exit function if it's defined. */
  if(called_item_entry)
  {
    if(item_exit)
    {
      item_exit(attributes, data);
    }
  }

  return 0;
}

 /*@@
   @routine    ScheduleTraverseFunction
   @date       Thu Sep 16 11:51:58 1999
   @author     Tom Goodale
   @desc 
   Deals with a function in the schedule list
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
static int ScheduleTraverseFunction(void *function,
                                    void *attributes,
                                    int n_whiles,
                                    char **whiles,
                                    int (*item_entry)(void *, void *),
                                    int (*item_exit)(void *, void *),
                                    int  (*while_check)(int, char **, void *, void *),
                                    int (*function_process)(void *, void *, void *),
                                    void *data)
{
  int doit;
  int called_item_entry;

  /* If there is a while-list associated with this function, check if the function should be
   * executed at all.
   */

  if(n_whiles > 0 && while_check)
  {
    doit = while_check(n_whiles, whiles, attributes, data);
  }
  else
  {
    doit = 1;
  }

  /* Call a item entry function if it is defined. */
  if(doit)
  {
    called_item_entry = 1;

    if(item_entry)
    {
      doit = item_entry(attributes, data);
    }
  }
  else
  {
    called_item_entry = 0;
  }

  /* Now traverse the . */
  while(doit )
  {
    
    /* Now actually do something with the function. */
    function_process(function, attributes, data);

    /* Check the while_list again. */
    if(n_whiles > 0 && while_check)
    {
      doit = while_check(n_whiles, whiles, attributes, data) ;
    }
    else

    {
      doit = 0;
    }
  }

  /* Call the item_exit function if it's defined. */
  if(called_item_entry)
  {
    if(item_exit)
    {
      item_exit(attributes, data);
    }
  }

  return 0;
}

/********************************************************************
 ********************************************************************
 ********************************************************************/

#ifdef TEST_SCHEDULETRAVERSE

#define func_x(x) \
int func_ ## x (void) { return printf("I'm func " #x "\n"); }

func_x(a)
func_x(b)
func_x(c)

int fprocess(void *function, void *attributes, void *data)
{
  int (*func)(void);

  func = (int (*)(void)) function;

  func();

  return 1;
}

int main(int argc, char *argv[])
{
  t_sched_modifier *modifier;

  modifier = CCTKi_ScheduleAddModifer(NULL, "before", "c");
  modifier = CCTKi_ScheduleAddModifer(modifier, "after",  "a");

  CCTKi_ScheduleFunction("group_a", "c", func_c, NULL, NULL);
  CCTKi_ScheduleFunction("group_a", "b", func_b, modifier, NULL);
  CCTKi_ScheduleFunction("group_a", "a", func_a, NULL, NULL);
  CCTKi_ScheduleFunction("group_b", "a", func_a, NULL, NULL);
  CCTKi_ScheduleFunction("group_b", "b", func_b, NULL, NULL);
  CCTKi_ScheduleGroup("group_a", "group_b", modifier, NULL);

  CCTKi_ScheduleSortAllGroups();

  CCTKi_ScheduleTraverse("group_a", NULL, NULL, NULL, fprocess, NULL);

  return 0;
}
#endif