1 /* timer: repetitive and delayed actions */
3 /* Copyright (c) 2005, 2007, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
9 int client(int argc, char *argv[]); /* Placed here to not bother doxygen inclusion */
13 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Logging specific to this test");
15 #define REPEAT_INTERVAL 1.0
16 #define DELAY_INTERVAL 2.0
23 static void repetitive_action(void)
25 my_globals *globals = (my_globals *) gras_userdata_get();
27 /* Stop if nothing to do yet */
28 if (globals->still_to_do <= 0) {
29 XBT_INFO("Repetitive_action has nothing to do yet");
33 if (globals->still_to_do == 1) {
34 /* Unregister myself if I'm done */
35 gras_timer_cancel_repeat(REPEAT_INTERVAL, repetitive_action);
39 ("repetitive_action decrementing globals->still_to_do. New value: %d",
40 globals->still_to_do - 1);
42 globals->still_to_do--; /* should be the last line of the action since value=0 stops the program */
43 } /* end_of_repetitive_action */
45 static void delayed_action(void)
47 my_globals *globals = (my_globals *) gras_userdata_get();
49 XBT_INFO("delayed_action setting globals->still_to_do to %d", LOOP_COUNT);
51 globals->still_to_do = LOOP_COUNT;
52 } /* end_of_delayed_action */
54 int client(int argc, char *argv[])
58 gras_init(&argc, argv);
59 globals = gras_userdata_new(my_globals);
60 globals->still_to_do = -1;
62 XBT_INFO("Programming the repetitive_action with a frequency of %f sec",
64 gras_timer_repeat(REPEAT_INTERVAL, repetitive_action);
66 XBT_INFO("Programming the delayed_action for after %f sec", DELAY_INTERVAL);
67 gras_timer_delay(REPEAT_INTERVAL, delayed_action);
69 XBT_INFO("Have a rest");
70 gras_os_sleep(DELAY_INTERVAL / 2.0);
72 XBT_INFO("Canceling the delayed_action.");
73 gras_timer_cancel_delay(REPEAT_INTERVAL, delayed_action);
75 XBT_INFO("Re-programming the delayed_action for after %f sec",
77 gras_timer_delay(REPEAT_INTERVAL, delayed_action);
79 while (globals->still_to_do == -1 || /* Before delayed action runs */
80 globals->still_to_do >
81 0 /* after delayed_action, and not enough repetitive_action */ ) {
83 XBT_DEBUG("Prepare to handle messages for 5 sec (still_to_do=%d)",
84 globals->still_to_do);