Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Typos : a lot of mecanism -> mechanism and functional -> functionnal
[simgrid.git] / src / gras / Msg / timer.c
1 /* $Id$ */
2
3 /* timer - Delayed and repetitive actions                                   */
4
5 /* Copyright (c) 2005 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
11 #include "gras/Msg/msg_private.h"
12 #include "gras/timer.h"
13 #include "gras/Virtu/virtu_interface.h"
14
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_timer,gras,
17                                 "Delayed and repetitive actions");
18
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_get("gras_msg");
22    
23   gras_timer_t timer = xbt_dynar_push_ptr(pd->timers);
24    
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;
30 }
31
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_get("gras_msg");
35    
36   gras_timer_t timer = xbt_dynar_push_ptr(pd->timers);
37
38   VERB1("Register repetitive action %p", action);
39   timer->period = interval;
40   timer->expiry = interval+gras_os_time();
41   timer->action = action;
42   timer->repeat = TRUE;
43 }
44
45 /** @brief Cancel a delayed task */
46 xbt_error_t gras_timer_cancel_delay(double interval, void_f_void_t action) {
47   gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_get("gras_msg");
48   int cursor,found;
49   s_gras_timer_t timer;
50
51   found = FALSE;
52   xbt_dynar_foreach(pd->timers,cursor,timer){
53      if (timer.repeat == FALSE    &&
54          timer.period == interval &&
55          timer.action == action) {
56         
57         found = TRUE;
58         xbt_dynar_cursor_rm(pd->timers, &cursor);
59      }
60   }
61   
62   if (!found)
63      RAISE2(mismatch_error,"Cannot remove the action %p delayed of %f second: not found",
64             action,interval);
65    
66   return no_error;
67 }
68
69 /** @brief Cancel a repetitive task */
70 xbt_error_t gras_timer_cancel_repeat(double interval, void_f_void_t action) {
71   gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_get("gras_msg");
72   int cursor,found;
73   s_gras_timer_t timer;
74
75   found = FALSE;
76   xbt_dynar_foreach(pd->timers,cursor,timer){
77      if (timer.repeat == TRUE     &&
78          timer.period == interval &&
79          timer.action == action) {
80         
81         found = TRUE;
82         xbt_dynar_cursor_rm(pd->timers, &cursor);
83      }
84   }
85   
86   if (!found)
87      RAISE2(mismatch_error,"Cannot remove the action %p delayed of %f second: not found",
88             action,interval);
89    
90   return no_error;
91 }
92
93 /** @brief Cancel all delayed tasks */
94 xbt_error_t gras_timer_cancel_delay_all(void) {
95   gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_get("gras_msg");
96   int cursor, found;
97   s_gras_timer_t timer;
98
99   found = FALSE;
100   xbt_dynar_foreach(pd->timers,cursor,timer){
101      if (timer.repeat   == FALSE) {
102         
103         found = TRUE;
104         xbt_dynar_cursor_rm(pd->timers, &cursor);
105      }
106   }
107   
108   if (!found)
109      RAISE0(mismatch_error,"No delayed action to remove");
110    
111   return no_error;
112 }
113
114 /** @brief Cancel all repetitive tasks */
115 xbt_error_t gras_timer_cancel_repeat_all(void){
116   gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_get("gras_msg");
117   int cursor, found;
118   s_gras_timer_t timer;
119
120   found = FALSE;
121   xbt_dynar_foreach(pd->timers,cursor,timer){
122      if (timer.repeat   == FALSE) {
123         
124         found = TRUE;
125         xbt_dynar_cursor_rm(pd->timers, &cursor);
126      }
127   }
128   
129   if (!found)
130      RAISE0(mismatch_error,"No repetitive action to remove");
131    
132   return no_error;
133 }
134
135 /** @brief Cancel all delayed and repetitive tasks */
136 void gras_timer_cancel_all(void) {
137   gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_get("gras_msg");
138   xbt_dynar_reset( pd->timers );
139 }
140
141    
142 /* returns 0 if it handled a timer, or the delay until next timer, or -1 if no armed timer */
143 double gras_msg_timer_handle(void) {
144   gras_msg_procdata_t pd=(gras_msg_procdata_t)gras_libdata_get("gras_msg");
145   int cursor;
146   gras_timer_t timer;
147   double now=gras_os_time();
148   double untilnext = -1.0;
149   
150   for (cursor=0; cursor < xbt_dynar_length(pd->timers); cursor++) {
151      double untilthis;
152      
153      timer = xbt_dynar_get_ptr (pd->timers, cursor);
154      untilthis = timer->expiry - now;
155      
156      DEBUG2("Action %p expires in %f", timer->action, untilthis);
157      
158      if (untilthis <= 0.0) {
159         
160         DEBUG5("[%.0f] Serve %s action %p (%f<%f)",gras_os_time(),
161               timer->repeat ? "repetitive" : "delayed", timer->action,
162               timer->expiry, now);
163         timer->action();
164         
165         if (timer->repeat) {    
166            timer->expiry = now + timer->period;
167            DEBUG4("[%.0f] Re-arm repetitive action %p for %f (period=%f)",
168                  gras_os_time(),
169                  timer->action, timer->expiry, timer->period);
170         } else {
171            DEBUG2("[%.0f] Remove %p now that it's done", gras_os_time(), timer->action);
172            xbt_dynar_cursor_rm(pd->timers, &cursor);
173         }
174         return 0.0;
175      } else if (untilthis < untilnext || untilnext == -1) {
176         untilnext = untilthis;
177      }
178   }
179   return untilnext;
180 }