3 /* timer - Delayed and repetitive actions */
5 /* Copyright (c) 2005 Martin Quinson. All rights reserved. */
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. */
11 #include "gras/Msg/msg_private.h"
12 #include "gras/timer.h"
13 #include "gras/Virtu/virtu_interface.h"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_timer,gras,
17 "Delayed and repetitive actions");
19 /** @brief Request \a action to be called once in \a delay seconds */
20 void gras_timer_delay(double delay, void_f_void_t action) {
21 gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
23 gras_timer_t timer = xbt_dynar_push_ptr(pd->timers);
25 VERB1("Register delayed action %p", action);
26 timer->period = delay;
27 timer->expiry = delay+gras_os_time();
28 timer->action = action;
29 timer->repeat = FALSE;
32 /** @brief Request \a action to be called every \a interval seconds */
33 void gras_timer_repeat(double interval, void_f_void_t action) {
34 gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
36 gras_timer_t timer = xbt_dynar_push_ptr(pd->timers);
38 VERB1("Register repetitive action %p", action);
39 timer->period = interval;
40 timer->expiry = interval+gras_os_time();
41 timer->action = action;
45 /** @brief Cancel a delayed task */
46 void gras_timer_cancel_delay(double interval, void_f_void_t action) {
47 gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
53 xbt_dynar_foreach(pd->timers,cursor,timer){
54 if (timer.repeat == FALSE &&
55 timer.period == interval &&
56 timer.action == action) {
59 xbt_dynar_cursor_rm(pd->timers, &cursor);
64 THROW2(mismatch_error,0,"Cannot remove the action %p delayed of %f second: not found",
69 /** @brief Cancel a repetitive task */
70 void gras_timer_cancel_repeat(double interval, void_f_void_t action) {
71 gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
77 xbt_dynar_foreach(pd->timers,cursor,timer){
78 if (timer.repeat == TRUE &&
79 timer.period == interval &&
80 timer.action == action) {
83 xbt_dynar_cursor_rm(pd->timers, &cursor);
88 THROW2(mismatch_error,0,"Cannot remove the action %p delayed of %f second: not found",
92 /** @brief Cancel all delayed tasks */
93 void gras_timer_cancel_delay_all(void) {
94 gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
100 xbt_dynar_foreach(pd->timers,cursor,timer){
101 if (timer.repeat == FALSE) {
104 xbt_dynar_cursor_rm(pd->timers, &cursor);
109 THROW0(mismatch_error,0,"No delayed action to remove");
113 /** @brief Cancel all repetitive tasks */
114 void gras_timer_cancel_repeat_all(void){
115 gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
118 s_gras_timer_t timer;
121 xbt_dynar_foreach(pd->timers,cursor,timer){
122 if (timer.repeat == FALSE) {
125 xbt_dynar_cursor_rm(pd->timers, &cursor);
130 THROW0(mismatch_error,0,"No repetitive action to remove");
133 /** @brief Cancel all delayed and repetitive tasks */
134 void gras_timer_cancel_all(void) {
135 gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
136 xbt_dynar_reset( pd->timers );
140 /* returns 0 if it handled a timer, or the delay until next timer, or -1 if no armed timer */
141 double gras_msg_timer_handle(void) {
142 gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_by_id(gras_msg_libdata_id);
145 double now=gras_os_time();
146 double untilnext = -1.0;
148 for (cursor=0; cursor < xbt_dynar_length(pd->timers); cursor++) {
151 timer = xbt_dynar_get_ptr (pd->timers, cursor);
152 untilthis = timer->expiry - now;
154 DEBUG2("Action %p expires in %f", timer->action, untilthis);
156 if (untilthis <= 0.0) {
157 void_f_void_t action = timer->action;
159 DEBUG5("[%.0f] Serve %s action %p (%f<%f)",gras_os_time(),
160 timer->repeat ? "repetitive" : "delayed", timer->action,
164 timer->expiry = now + timer->period;
165 DEBUG4("[%.0f] Re-arm repetitive action %p for %f (period=%f)",
167 timer->action, timer->expiry, timer->period);
169 DEBUG2("[%.0f] Remove %p now that it's done", gras_os_time(), timer->action);
170 xbt_dynar_cursor_rm(pd->timers, &cursor);
174 } else if (untilthis < untilnext || untilnext == -1) {
175 untilnext = untilthis;