Logo AND Algorithmique Numérique Distribuée

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