lib/tevent: expose ev_timeval_zero() for internal usage
[jra/samba/.git] / lib / tevent / tevent_timed.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    common events code for timed events
5
6    Copyright (C) Andrew Tridgell        2003-2006
7    Copyright (C) Stefan Metzmacher      2005
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "replace.h"
24 #include "system/time.h"
25 #include "tevent.h"
26 #include "tevent_internal.h"
27 #include "tevent_util.h"
28
29 /**
30   compare two timeval structures. 
31   Return -1 if tv1 < tv2
32   Return 0 if tv1 == tv2
33   Return 1 if tv1 > tv2
34 */
35 static int ev_timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
36 {
37         if (tv1->tv_sec  > tv2->tv_sec)  return 1;
38         if (tv1->tv_sec  < tv2->tv_sec)  return -1;
39         if (tv1->tv_usec > tv2->tv_usec) return 1;
40         if (tv1->tv_usec < tv2->tv_usec) return -1;
41         return 0;
42 }
43
44 /**
45   return a zero timeval
46 */
47 struct timeval ev_timeval_zero(void)
48 {
49         struct timeval tv;
50         tv.tv_sec = 0;
51         tv.tv_usec = 0;
52         return tv;
53 }
54
55 /**
56   return a timeval for the current time
57 */
58 static struct timeval ev_timeval_current(void)
59 {
60         struct timeval tv;
61         gettimeofday(&tv, NULL);
62         return tv;
63 }
64
65 /**
66   return a timeval struct with the given elements
67 */
68 static struct timeval ev_timeval_set(uint32_t secs, uint32_t usecs)
69 {
70         struct timeval tv;
71         tv.tv_sec = secs;
72         tv.tv_usec = usecs;
73         return tv;
74 }
75
76 /**
77   return the difference between two timevals as a timeval
78   if tv1 comes after tv2, then return a zero timeval
79   (this is *tv2 - *tv1)
80 */
81 static struct timeval ev_timeval_until(const struct timeval *tv1,
82                                         const struct timeval *tv2)
83 {
84         struct timeval t;
85         if (ev_timeval_compare(tv1, tv2) >= 0) {
86                 return ev_timeval_zero();
87         }
88         t.tv_sec = tv2->tv_sec - tv1->tv_sec;
89         if (tv1->tv_usec > tv2->tv_usec) {
90                 t.tv_sec--;
91                 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
92         } else {
93                 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
94         }
95         return t;
96 }
97
98 /**
99   return true if a timeval is zero
100 */
101 bool ev_timeval_is_zero(const struct timeval *tv)
102 {
103         return tv->tv_sec == 0 && tv->tv_usec == 0;
104 }
105
106 /*
107   destroy a timed event
108 */
109 static int tevent_common_timed_destructor(struct tevent_timer *te)
110 {
111         tevent_debug(te->event_ctx, TEVENT_DEBUG_TRACE,
112                      "Destroying timer event %p \"%s\"\n",
113                      te, te->handler_name);
114
115         if (te->event_ctx) {
116                 DLIST_REMOVE(te->event_ctx->timer_events, te);
117         }
118
119         return 0;
120 }
121
122 static int tevent_common_timed_deny_destructor(struct tevent_timer *te)
123 {
124         return -1;
125 }
126
127 /*
128   add a timed event
129   return NULL on failure (memory allocation error)
130 */
131 struct tevent_timer *tevent_common_add_timer(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
132                                              struct timeval next_event,
133                                              tevent_timer_handler_t handler,
134                                              void *private_data,
135                                              const char *handler_name,
136                                              const char *location)
137 {
138         struct tevent_timer *te, *last_te, *cur_te;
139
140         te = talloc(mem_ctx?mem_ctx:ev, struct tevent_timer);
141         if (te == NULL) return NULL;
142
143         te->event_ctx           = ev;
144         te->next_event          = next_event;
145         te->handler             = handler;
146         te->private_data        = private_data;
147         te->handler_name        = handler_name;
148         te->location            = location;
149         te->additional_data     = NULL;
150
151         /* keep the list ordered */
152         last_te = NULL;
153         for (cur_te = ev->timer_events; cur_te; cur_te = cur_te->next) {
154                 /* if the new event comes before the current one break */
155                 if (ev_timeval_compare(&te->next_event, &cur_te->next_event) < 0) {
156                         break;
157                 }
158
159                 last_te = cur_te;
160         }
161
162         DLIST_ADD_AFTER(ev->timer_events, te, last_te);
163
164         talloc_set_destructor(te, tevent_common_timed_destructor);
165
166         tevent_debug(ev, TEVENT_DEBUG_TRACE,
167                      "Added timed event \"%s\": %p\n",
168                      handler_name, te);
169         return te;
170 }
171
172 /*
173   do a single event loop using the events defined in ev
174
175   return the delay untill the next timed event,
176   or zero if a timed event was triggered
177 */
178 struct timeval tevent_common_loop_timer_delay(struct tevent_context *ev)
179 {
180         struct timeval current_time = ev_timeval_zero();
181         struct tevent_timer *te = ev->timer_events;
182
183         if (!te) {
184                 /* have a default tick time of 30 seconds. This guarantees
185                    that code that uses its own timeout checking will be
186                    able to proceeed eventually */
187                 return ev_timeval_set(30, 0);
188         }
189
190         /*
191          * work out the right timeout for the next timed event
192          *
193          * avoid the syscall to gettimeofday() if the timed event should
194          * be triggered directly
195          *
196          * if there's a delay till the next timed event, we're done
197          * with just returning the delay
198          */
199         if (!ev_timeval_is_zero(&te->next_event)) {
200                 struct timeval delay;
201
202                 current_time = ev_timeval_current();
203
204                 delay = ev_timeval_until(&current_time, &te->next_event);
205                 if (!ev_timeval_is_zero(&delay)) {
206                         return delay;
207                 }
208         }
209
210         /*
211          * ok, we have a timed event that we'll process ...
212          */
213
214         /* deny the handler to free the event */
215         talloc_set_destructor(te, tevent_common_timed_deny_destructor);
216
217         /* We need to remove the timer from the list before calling the
218          * handler because in a semi-async inner event loop called from the
219          * handler we don't want to come across this event again -- vl */
220         DLIST_REMOVE(ev->timer_events, te);
221
222         /*
223          * If the timed event was registered for a zero current_time,
224          * then we pass a zero timeval here too! To avoid the
225          * overhead of gettimeofday() calls.
226          *
227          * otherwise we pass the current time
228          */
229         te->handler(ev, te, current_time, te->private_data);
230
231         /* The destructor isn't necessary anymore, we've already removed the
232          * event from the list. */
233         talloc_set_destructor(te, NULL);
234
235         tevent_debug(te->event_ctx, TEVENT_DEBUG_TRACE,
236                      "Ending timer event %p \"%s\"\n",
237                      te, te->handler_name);
238
239         talloc_free(te);
240
241         return ev_timeval_zero();
242 }
243