Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix copyright headers
[simgrid.git] / examples / gras / timer / timer.c
1 /* timer: repetitive and delayed actions                                    */
2
3 /* Copyright (c) 2005, 2007, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 int client(int argc, char *argv[]);     /* Placed here to not bother doxygen inclusion */
10
11 #include "gras.h"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Logging specific to this test");
14
15 #define REPEAT_INTERVAL 1.0
16 #define DELAY_INTERVAL 2.0
17 #define LOOP_COUNT 5
18
19 typedef struct {
20   int still_to_do;
21 } my_globals;
22
23 static void repetitive_action(void)
24 {
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     INFO0("Repetitive_action has nothing to do yet");
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   INFO1("repetitive_action decrementing globals->still_to_do. New value: %d",
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 {
46   my_globals *globals = (my_globals *) gras_userdata_get();
47
48   INFO1("delayed_action setting globals->still_to_do to %d", LOOP_COUNT);
49
50   globals->still_to_do = LOOP_COUNT;
51 }                               /* end_of_delayed_action */
52
53 int client(int argc, char *argv[])
54 {
55   my_globals *globals;
56
57   gras_init(&argc, argv);
58   globals = gras_userdata_new(my_globals);
59   globals->still_to_do = -1;
60
61   INFO1("Programming the repetitive_action with a frequency of %f sec",
62         REPEAT_INTERVAL);
63   gras_timer_repeat(REPEAT_INTERVAL, repetitive_action);
64
65   INFO1("Programming the delayed_action for after %f sec", DELAY_INTERVAL);
66   gras_timer_delay(REPEAT_INTERVAL, delayed_action);
67
68   INFO0("Have a rest");
69   gras_os_sleep(DELAY_INTERVAL / 2.0);
70
71   INFO0("Canceling the delayed_action.");
72   gras_timer_cancel_delay(REPEAT_INTERVAL, delayed_action);
73
74   INFO1("Re-programming the delayed_action for after %f sec", 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 >
79          0 /* after delayed_action, and not enough repetitive_action */ ) {
80
81     DEBUG1("Prepare to handle messages for 5 sec (still_to_do=%d)",
82            globals->still_to_do);
83     gras_msg_handle(5.0);
84   }
85   gras_exit();
86   return 0;
87 }                               /* end_of_client */