2 Unix SMB/CIFS implementation.
4 common events code for timed events
6 Copyright (C) Andrew Tridgell 2003-2006
7 Copyright (C) Stefan Metzmacher 2005-2009
9 ** NOTE! The following LGPL license applies to the tevent
10 ** library. This does NOT imply that all of Samba is released
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 #include <ccan/tevent/tevent.h>
28 #include <ccan/tevent/tevent_internal.h>
29 #include <ccan/tevent/tevent_util.h>
33 compare two timeval structures.
34 Return -1 if tv1 < tv2
35 Return 0 if tv1 == tv2
38 int tevent_timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
40 if (tv1->tv_sec > tv2->tv_sec) return 1;
41 if (tv1->tv_sec < tv2->tv_sec) return -1;
42 if (tv1->tv_usec > tv2->tv_usec) return 1;
43 if (tv1->tv_usec < tv2->tv_usec) return -1;
50 struct timeval tevent_timeval_zero(void)
59 return a timeval for the current time
61 struct timeval tevent_timeval_current(void)
64 gettimeofday(&tv, NULL);
69 return a timeval struct with the given elements
71 struct timeval tevent_timeval_set(uint32_t secs, uint32_t usecs)
80 return the difference between two timevals as a timeval
81 if tv1 comes after tv2, then return a zero timeval
84 struct timeval tevent_timeval_until(const struct timeval *tv1,
85 const struct timeval *tv2)
88 if (tevent_timeval_compare(tv1, tv2) >= 0) {
89 return tevent_timeval_zero();
91 t.tv_sec = tv2->tv_sec - tv1->tv_sec;
92 if (tv1->tv_usec > tv2->tv_usec) {
94 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
96 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
102 return true if a timeval is zero
104 bool tevent_timeval_is_zero(const struct timeval *tv)
106 return tv->tv_sec == 0 && tv->tv_usec == 0;
109 struct timeval tevent_timeval_add(const struct timeval *tv, uint32_t secs,
112 struct timeval tv2 = *tv;
114 tv2.tv_usec += usecs;
115 tv2.tv_sec += tv2.tv_usec / 1000000;
116 tv2.tv_usec = tv2.tv_usec % 1000000;
122 return a timeval in the future with a specified offset
124 struct timeval tevent_timeval_current_ofs(uint32_t secs, uint32_t usecs)
126 struct timeval tv = tevent_timeval_current();
127 return tevent_timeval_add(&tv, secs, usecs);
131 destroy a timed event
133 static int tevent_common_timed_destructor(struct tevent_timer *te)
135 tevent_debug(te->event_ctx, TEVENT_DEBUG_TRACE,
136 "Destroying timer event %p \"%s\"\n",
137 te, te->handler_name);
140 DLIST_REMOVE(te->event_ctx->timer_events, te);
146 static int tevent_common_timed_deny_destructor(struct tevent_timer *te)
153 return NULL on failure (memory allocation error)
155 struct tevent_timer *tevent_common_add_timer(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
156 struct timeval next_event,
157 tevent_timer_handler_t handler,
159 const char *handler_name,
160 const char *location)
162 struct tevent_timer *te, *last_te, *cur_te;
164 te = talloc(mem_ctx?mem_ctx:ev, struct tevent_timer);
165 if (te == NULL) return NULL;
168 te->next_event = next_event;
169 te->handler = handler;
170 te->private_data = private_data;
171 te->handler_name = handler_name;
172 te->location = location;
173 te->additional_data = NULL;
175 /* keep the list ordered */
177 for (cur_te = ev->timer_events; cur_te; cur_te = cur_te->next) {
178 /* if the new event comes before the current one break */
179 if (tevent_timeval_compare(&te->next_event, &cur_te->next_event) < 0) {
186 DLIST_ADD_AFTER(ev->timer_events, te, last_te);
188 talloc_set_destructor(te, tevent_common_timed_destructor);
190 tevent_debug(ev, TEVENT_DEBUG_TRACE,
191 "Added timed event \"%s\": %p\n",
197 do a single event loop using the events defined in ev
199 return the delay untill the next timed event,
200 or zero if a timed event was triggered
202 struct timeval tevent_common_loop_timer_delay(struct tevent_context *ev)
204 struct timeval current_time = tevent_timeval_zero();
205 struct tevent_timer *te = ev->timer_events;
208 /* have a default tick time of 30 seconds. This guarantees
209 that code that uses its own timeout checking will be
210 able to proceeed eventually */
211 return tevent_timeval_set(30, 0);
215 * work out the right timeout for the next timed event
217 * avoid the syscall to gettimeofday() if the timed event should
218 * be triggered directly
220 * if there's a delay till the next timed event, we're done
221 * with just returning the delay
223 if (!tevent_timeval_is_zero(&te->next_event)) {
224 struct timeval delay;
226 current_time = tevent_timeval_current();
228 delay = tevent_timeval_until(¤t_time, &te->next_event);
229 if (!tevent_timeval_is_zero(&delay)) {
235 * ok, we have a timed event that we'll process ...
238 /* deny the handler to free the event */
239 talloc_set_destructor(te, tevent_common_timed_deny_destructor);
241 /* We need to remove the timer from the list before calling the
242 * handler because in a semi-async inner event loop called from the
243 * handler we don't want to come across this event again -- vl */
244 DLIST_REMOVE(ev->timer_events, te);
247 * If the timed event was registered for a zero current_time,
248 * then we pass a zero timeval here too! To avoid the
249 * overhead of gettimeofday() calls.
251 * otherwise we pass the current time
253 te->handler(ev, te, current_time, te->private_data);
255 /* The destructor isn't necessary anymore, we've already removed the
256 * event from the list. */
257 talloc_set_destructor(te, NULL);
259 tevent_debug(te->event_ctx, TEVENT_DEBUG_TRACE,
260 "Ending timer event %p \"%s\"\n",
261 te, te->handler_name);
265 return tevent_timeval_zero();