1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* expirelist.c List of items that expire
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "expirelist.h"
27 #include <dbus/dbus-internals.h>
28 #include <dbus/dbus-mainloop.h>
29 #include <dbus/dbus-timeout.h>
33 DBusList *items; /**< List of BusExpireItem */
36 BusExpireFunc expire_func;
38 int expire_after; /**< Expire after milliseconds (thousandths) */
41 static dbus_bool_t expire_timeout_handler (void *data);
44 bus_expire_list_new (DBusLoop *loop,
46 BusExpireFunc expire_func,
51 list = dbus_new0 (BusExpireList, 1);
55 list->expire_func = expire_func;
58 list->expire_after = expire_after;
60 list->timeout = _dbus_timeout_new (100, /* irrelevant */
61 expire_timeout_handler,
63 if (list->timeout == NULL)
66 _dbus_timeout_set_enabled (list->timeout, FALSE);
68 if (!_dbus_loop_add_timeout (list->loop, list->timeout))
75 _dbus_timeout_unref (list->timeout);
83 bus_expire_list_free (BusExpireList *list)
85 _dbus_assert (list->items == NULL);
87 _dbus_loop_remove_timeout (list->loop, list->timeout);
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 an 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 an expire timeout\n");
114 _dbus_verbose ("No need to disable this expire timeout\n");
118 bus_expire_list_recheck_immediately (BusExpireList *list)
120 _dbus_verbose ("setting interval on expire list to 0 for immediate recheck\n");
122 bus_expire_timeout_set_interval (list->timeout, 0);
126 do_expiration_with_monotonic_time (BusExpireList *list,
131 int next_interval, min_wait_time, items_to_expire;
134 min_wait_time = 3600 * 1000; /* this is reset anyway if used */
137 link = _dbus_list_get_first_link (&list->items);
140 DBusList *next = _dbus_list_get_next_link (&list->items, link);
146 elapsed = ELAPSED_MILLISECONDS_SINCE (item->added_tv_sec,
150 if (((item->added_tv_sec == 0) && (item->added_tv_usec == 0)) ||
151 ((list->expire_after > 0) && (elapsed >= (double) list->expire_after)))
153 _dbus_verbose ("Expiring an item %p\n", item);
155 /* If the expire function fails, we just end up expiring
156 * this item next time we walk through the list. This would
157 * be an indeterminate time normally, so we set up the
158 * next_interval to be "shortly" (just enough to avoid
161 if (!(* list->expire_func) (list, link, list->data))
163 next_interval = _dbus_get_oom_wait ();
167 else if (list->expire_after > 0)
172 to_wait = (double) list->expire_after - elapsed;
173 if (min_wait_time > to_wait)
174 min_wait_time = to_wait;
180 if (next_interval < 0 && items_to_expire)
181 next_interval = min_wait_time;
183 return next_interval;
187 bus_expirelist_expire (BusExpireList *list)
193 if (list->items != NULL)
195 long tv_sec, tv_usec;
197 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
199 next_interval = do_expiration_with_monotonic_time (list, tv_sec, tv_usec);
202 bus_expire_timeout_set_interval (list->timeout, next_interval);
206 expire_timeout_handler (void *data)
208 BusExpireList *list = data;
210 _dbus_verbose ("Running\n");
212 /* note that this may remove the timeout */
213 bus_expirelist_expire (list);
219 bus_expire_list_remove_link (BusExpireList *list,
222 _dbus_list_remove_link (&list->items, link);
226 bus_expire_list_remove (BusExpireList *list,
229 return _dbus_list_remove (&list->items, item);
233 bus_expire_list_unlink (BusExpireList *list,
236 _dbus_list_unlink (&list->items, link);
240 bus_expire_list_add (BusExpireList *list,
245 ret = _dbus_list_prepend (&list->items, item);
246 if (ret && !dbus_timeout_get_enabled (list->timeout))
247 bus_expire_timeout_set_interval (list->timeout, 0);
253 bus_expire_list_add_link (BusExpireList *list,
256 _dbus_assert (link->data != NULL);
258 _dbus_list_prepend_link (&list->items, link);
260 if (!dbus_timeout_get_enabled (list->timeout))
261 bus_expire_timeout_set_interval (list->timeout, 0);
265 bus_expire_list_get_first_link (BusExpireList *list)
267 return _dbus_list_get_first_link (&list->items);
271 bus_expire_list_get_next_link (BusExpireList *list,
274 return _dbus_list_get_next_link (&list->items, link);
278 bus_expire_list_contains_item (BusExpireList *list,
281 return _dbus_list_find_last (&list->items, item) != NULL;
284 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
293 test_expire_func (BusExpireList *list,
299 t = (TestExpireItem*) link->data;
301 t->expire_count += 1;
307 time_add_milliseconds (long *tv_sec,
311 *tv_sec = *tv_sec + milliseconds / 1000;
312 *tv_usec = *tv_usec + milliseconds * 1000;
313 if (*tv_usec >= 1000000)
321 bus_expire_list_test (const DBusString *test_data_dir)
325 long tv_sec, tv_usec;
326 long tv_sec_not_expired, tv_usec_not_expired;
327 long tv_sec_expired, tv_usec_expired;
328 long tv_sec_past, tv_usec_past;
329 TestExpireItem *item;
331 dbus_bool_t result = FALSE;
334 loop = _dbus_loop_new ();
335 _dbus_assert (loop != NULL);
337 #define EXPIRE_AFTER 100
339 list = bus_expire_list_new (loop, EXPIRE_AFTER,
340 test_expire_func, NULL);
341 _dbus_assert (list != NULL);
343 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
345 tv_sec_not_expired = tv_sec;
346 tv_usec_not_expired = tv_usec;
347 time_add_milliseconds (&tv_sec_not_expired,
348 &tv_usec_not_expired, EXPIRE_AFTER - 1);
350 tv_sec_expired = tv_sec;
351 tv_usec_expired = tv_usec;
352 time_add_milliseconds (&tv_sec_expired,
353 &tv_usec_expired, EXPIRE_AFTER);
356 tv_sec_past = tv_sec - 1;
357 tv_usec_past = tv_usec;
359 item = dbus_new0 (TestExpireItem, 1);
364 item->item.added_tv_sec = tv_sec;
365 item->item.added_tv_usec = tv_usec;
366 if (!bus_expire_list_add (list, &item->item))
367 _dbus_assert_not_reached ("out of memory");
370 do_expiration_with_monotonic_time (list, tv_sec_not_expired,
371 tv_usec_not_expired);
372 _dbus_assert (item->expire_count == 0);
373 _dbus_verbose ("next_interval = %d\n", next_interval);
374 _dbus_assert (next_interval == 1);
377 do_expiration_with_monotonic_time (list, tv_sec_expired,
379 _dbus_assert (item->expire_count == 1);
380 _dbus_verbose ("next_interval = %d\n", next_interval);
381 _dbus_assert (next_interval == -1);
384 do_expiration_with_monotonic_time (list, tv_sec_past,
386 _dbus_assert (item->expire_count == 1);
387 _dbus_verbose ("next_interval = %d\n", next_interval);
388 _dbus_assert (next_interval == 1000 + EXPIRE_AFTER);
390 bus_expire_list_remove (list, &item->item);
393 bus_expire_list_free (list);
394 _dbus_loop_unref (loop);
402 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */