Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
33ff4e1443d47e213ae82ac7da6d374898321016
[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 {
26   my_globals *globals = (my_globals *) gras_userdata_get();
27
28   /* Stop if nothing to do yet */
29   if (globals->still_to_do <= 0) {
30     INFO0("Repetitive_action has nothing to do yet");
31     return;
32   }
33
34   if (globals->still_to_do == 1) {
35     /* Unregister myself if I'm done */
36     gras_timer_cancel_repeat(REPEAT_INTERVAL, repetitive_action);
37   }
38
39   INFO1("repetitive_action decrementing globals->still_to_do. New value: %d",
40         globals->still_to_do - 1);
41
42   globals->still_to_do--;       /* should be the last line of the action since value=0 stops the program */
43 }                               /* end_of_repetitive_action */
44
45 static void delayed_action(void)
46 {
47   my_globals *globals = (my_globals *) gras_userdata_get();
48
49   INFO1("delayed_action setting globals->still_to_do to %d", LOOP_COUNT);
50
51   globals->still_to_do = LOOP_COUNT;
52 }                               /* end_of_delayed_action */
53
54 int client(int argc, char *argv[])
55 {
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   INFO1("Programming the repetitive_action with a frequency of %f sec",
63         REPEAT_INTERVAL);
64   gras_timer_repeat(REPEAT_INTERVAL, repetitive_action);
65
66   INFO1("Programming the delayed_action for after %f sec", DELAY_INTERVAL);
67   gras_timer_delay(REPEAT_INTERVAL, delayed_action);
68
69   INFO0("Have a rest");
70   gras_os_sleep(DELAY_INTERVAL / 2.0);
71
72   INFO0("Canceling the delayed_action.");
73   gras_timer_cancel_delay(REPEAT_INTERVAL, delayed_action);
74
75   INFO1("Re-programming the delayed_action for after %f sec", DELAY_INTERVAL);
76   gras_timer_delay(REPEAT_INTERVAL, delayed_action);
77
78   while (globals->still_to_do == -1 ||  /* Before delayed action runs */
79          globals->still_to_do >
80          0 /* after delayed_action, and not enough repetitive_action */ ) {
81
82     DEBUG1("Prepare to handle messages for 5 sec (still_to_do=%d)",
83            globals->still_to_do);
84     gras_msg_handle(5.0);
85   }
86   gras_exit();
87   return 0;
88 }                               /* end_of_client */