Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8de543f6fae4cdbc19743a7b1b929156246f806f
[simgrid.git] / examples / gras / timer / timer.c
1 /* $Id$ */
2
3 /* timer: repetitive and delayed actions                                    */
4
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 int client(int argc,char *argv[]); /* Placed here to not bother doxygen inclusion */
11
12 #include "gras.h"
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
15
16 #define REPEAT_INTERVAL 1.0
17 #define DELAY_INTERVAL 2.0
18 #define LOOP_COUNT 5
19
20 typedef struct {
21    int still_to_do;
22 } my_globals;
23
24 static void repetitive_action(void) {
25    my_globals *globals=(my_globals*)gras_userdata_get();
26    
27    /* Stop if nothing to do yet */
28    if (globals->still_to_do <= 0) {
29      INFO1("[%.0f] Repetitive_action has nothing to do yet",gras_os_time());
30      return;
31    }
32    
33    if (globals->still_to_do == 1) {
34       /* Unregister myself if I'm done */
35       gras_timer_cancel_repeat(REPEAT_INTERVAL,repetitive_action);
36    }
37
38    INFO2("[%.0f] repetitive_action decrementing globals->still_to_do. New value: %d",gras_os_time(),
39          globals->still_to_do-1);
40    
41    globals->still_to_do--; /* should be the last line of the action since value=0 stops the program */
42 } /* end_of_repetitive_action */
43
44 static void delayed_action(void) {
45    my_globals *globals=(my_globals*)gras_userdata_get();
46    
47    INFO2("[%.0f] delayed_action setting globals->still_to_do to %d",
48          gras_os_time(),LOOP_COUNT);
49
50    globals->still_to_do = LOOP_COUNT;
51 } /* end_of_delayed_action */
52
53 int client(int argc,char *argv[]) {
54   xbt_error_t errcode;
55   int cpt;
56   my_globals *globals;
57
58   gras_init(&argc,argv);
59   globals=gras_userdata_new(my_globals);
60   globals->still_to_do = -1;
61
62   INFO2("[%.0f] Programming the repetitive_action with a frequency of %f sec", gras_os_time(), REPEAT_INTERVAL);
63   gras_timer_repeat(REPEAT_INTERVAL,repetitive_action);
64    
65   INFO2("[%.0f] Programming the delayed_action for after %f sec", gras_os_time(), DELAY_INTERVAL);
66   gras_timer_delay(REPEAT_INTERVAL,delayed_action);
67
68   INFO1("[%.0f] Have a rest", gras_os_time());  
69   gras_os_sleep(DELAY_INTERVAL / 2.0);
70    
71   INFO1("[%.0f] Canceling the delayed_action.",gras_os_time());
72   gras_timer_cancel_delay(REPEAT_INTERVAL,delayed_action);
73
74   INFO2("[%.0f] Re-programming the delayed_action for after %f sec", gras_os_time(),DELAY_INTERVAL);
75   gras_timer_delay(REPEAT_INTERVAL,delayed_action);
76
77   while (globals->still_to_do == -1 ||  /* Before delayed action runs */
78          globals->still_to_do > 0 /* after delayed_action, and not enough repetitive_action */) {
79
80      DEBUG2("[%.0f] Prepare to handle messages for 5 sec (still_to_do=%d)",
81             gras_os_time(), globals->still_to_do);
82      gras_msg_handle(5.0);
83   }
84   gras_exit();
85   return 0;
86 } /* end_of_client */
87