1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* expirelist.c List of items that expire
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.0
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "expirelist.h"
26 #include <dbus/dbus-internals.h>
27 #include <dbus/dbus-mainloop.h>
28 #include <dbus/dbus-timeout.h>
30 static dbus_bool_t expire_timeout_handler (void *data);
33 call_timeout_callback (DBusTimeout *timeout,
36 /* can return FALSE on OOM but we just let it fire again later */
37 dbus_timeout_handle (timeout);
41 bus_expire_list_new (DBusLoop *loop,
43 BusExpireFunc expire_func,
48 list = dbus_new0 (BusExpireList, 1);
52 list->expire_func = expire_func;
55 list->expire_after = expire_after;
57 list->timeout = _dbus_timeout_new (100, /* irrelevant */
58 expire_timeout_handler,
60 if (list->timeout == NULL)
63 _dbus_timeout_set_enabled (list->timeout, FALSE);
65 if (!_dbus_loop_add_timeout (list->loop,
67 call_timeout_callback, NULL, NULL))
74 _dbus_timeout_unref (list->timeout);
82 bus_expire_list_free (BusExpireList *list)
84 _dbus_assert (list->items == NULL);
86 _dbus_loop_remove_timeout (list->loop, list->timeout,
87 call_timeout_callback, NULL);
89 _dbus_timeout_unref (list->timeout);
95 bus_expire_timeout_set_interval (DBusTimeout *timeout,
98 if (next_interval >= 0)
100 _dbus_timeout_set_interval (timeout,
102 _dbus_timeout_set_enabled (timeout, TRUE);
104 _dbus_verbose ("Enabled expire timeout with interval %d\n",
107 else if (dbus_timeout_get_enabled (timeout))
109 _dbus_timeout_set_enabled (timeout, FALSE);
111 _dbus_verbose ("Disabled expire timeout\n");
114 _dbus_verbose ("No need to disable expire timeout\n");
118 do_expiration_with_current_time (BusExpireList *list,
127 link = _dbus_list_get_first_link (&list->items);
130 DBusList *next = _dbus_list_get_next_link (&list->items, link);
136 elapsed = ELAPSED_MILLISECONDS_SINCE (item->added_tv_sec,
140 if (elapsed >= (double) list->expire_after)
142 _dbus_verbose ("Expiring an item %p\n", item);
144 /* If the expire function fails, we just end up expiring
145 * this item next time we walk through the list. This would
146 * be an indeterminate time normally, so we set up the
147 * next_interval to be "shortly" (just enough to avoid
150 if (!(* list->expire_func) (list, link, list->data))
152 next_interval = _dbus_get_oom_wait ();
158 /* We can end the loop, since the connections are in oldest-first order */
159 next_interval = ((double)list->expire_after) - elapsed;
160 _dbus_verbose ("Item %p expires in %d milliseconds\n",
161 item, next_interval);
169 return next_interval;
173 bus_expirelist_expire (BusExpireList *list)
179 if (list->items != NULL)
181 long tv_sec, tv_usec;
183 _dbus_get_current_time (&tv_sec, &tv_usec);
185 next_interval = do_expiration_with_current_time (list, tv_sec, tv_usec);
188 bus_expire_timeout_set_interval (list->timeout, next_interval);
192 expire_timeout_handler (void *data)
194 BusExpireList *list = data;
196 _dbus_verbose ("Running %s\n", _DBUS_FUNCTION_NAME);
198 /* note that this may remove the timeout */
199 bus_expirelist_expire (list);
204 #ifdef DBUS_BUILD_TESTS
213 test_expire_func (BusExpireList *list,
219 t = (TestExpireItem*) link->data;
221 t->expire_count += 1;
227 time_add_milliseconds (long *tv_sec,
231 *tv_sec = *tv_sec + milliseconds / 1000;
232 *tv_usec = *tv_usec + milliseconds * 1000;
233 if (*tv_usec >= 1000000)
241 bus_expire_list_test (const DBusString *test_data_dir)
245 long tv_sec, tv_usec;
246 long tv_sec_not_expired, tv_usec_not_expired;
247 long tv_sec_expired, tv_usec_expired;
248 long tv_sec_past, tv_usec_past;
249 TestExpireItem *item;
252 loop = _dbus_loop_new ();
253 _dbus_assert (loop != NULL);
255 #define EXPIRE_AFTER 100
257 list = bus_expire_list_new (loop, EXPIRE_AFTER,
258 test_expire_func, NULL);
259 _dbus_assert (list != NULL);
261 _dbus_get_current_time (&tv_sec, &tv_usec);
263 tv_sec_not_expired = tv_sec;
264 tv_usec_not_expired = tv_usec;
265 time_add_milliseconds (&tv_sec_not_expired,
266 &tv_usec_not_expired, EXPIRE_AFTER - 1);
268 tv_sec_expired = tv_sec;
269 tv_usec_expired = tv_usec;
270 time_add_milliseconds (&tv_sec_expired,
271 &tv_usec_expired, EXPIRE_AFTER);
274 tv_sec_past = tv_sec - 1;
275 tv_usec_past = tv_usec;
277 item = dbus_new0 (TestExpireItem, 1);
279 item->item.added_tv_sec = tv_sec;
280 item->item.added_tv_usec = tv_usec;
281 if (!_dbus_list_append (&list->items, item))
282 _dbus_assert_not_reached ("out of memory");
285 do_expiration_with_current_time (list, tv_sec_not_expired,
286 tv_usec_not_expired);
287 _dbus_assert (item->expire_count == 0);
288 _dbus_verbose ("next_interval = %d\n", next_interval);
289 _dbus_assert (next_interval == 1);
292 do_expiration_with_current_time (list, tv_sec_expired,
294 _dbus_assert (item->expire_count == 1);
295 _dbus_verbose ("next_interval = %d\n", next_interval);
296 _dbus_assert (next_interval == -1);
299 do_expiration_with_current_time (list, tv_sec_past,
301 _dbus_assert (item->expire_count == 1);
302 _dbus_verbose ("next_interval = %d\n", next_interval);
303 _dbus_assert (next_interval == 1000 + EXPIRE_AFTER);
305 _dbus_list_clear (&list->items);
308 bus_expire_list_free (list);
309 _dbus_loop_unref (loop);
314 #endif /* DBUS_BUILD_TESTS */