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., 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>
32 DBusList *items; /**< List of BusExpireItem */
35 BusExpireFunc expire_func;
37 int expire_after; /**< Expire after milliseconds (thousandths) */
40 static dbus_bool_t expire_timeout_handler (void *data);
43 call_timeout_callback (DBusTimeout *timeout,
46 /* can return FALSE on OOM but we just let it fire again later */
47 dbus_timeout_handle (timeout);
51 bus_expire_list_new (DBusLoop *loop,
53 BusExpireFunc expire_func,
58 list = dbus_new0 (BusExpireList, 1);
62 list->expire_func = expire_func;
65 list->expire_after = expire_after;
67 list->timeout = _dbus_timeout_new (100, /* irrelevant */
68 expire_timeout_handler,
70 if (list->timeout == NULL)
73 _dbus_timeout_set_enabled (list->timeout, FALSE);
75 if (!_dbus_loop_add_timeout (list->loop,
77 call_timeout_callback, NULL, NULL))
84 _dbus_timeout_unref (list->timeout);
92 bus_expire_list_free (BusExpireList *list)
94 _dbus_assert (list->items == NULL);
96 _dbus_loop_remove_timeout (list->loop, list->timeout,
97 call_timeout_callback, NULL);
99 _dbus_timeout_unref (list->timeout);
105 bus_expire_timeout_set_interval (DBusTimeout *timeout,
108 if (next_interval >= 0)
110 _dbus_timeout_set_interval (timeout,
112 _dbus_timeout_set_enabled (timeout, TRUE);
114 _dbus_verbose ("Enabled an expire timeout with interval %d\n",
117 else if (dbus_timeout_get_enabled (timeout))
119 _dbus_timeout_set_enabled (timeout, FALSE);
121 _dbus_verbose ("Disabled an expire timeout\n");
124 _dbus_verbose ("No need to disable this expire timeout\n");
128 bus_expire_list_recheck_immediately (BusExpireList *list)
130 _dbus_verbose ("setting interval on expire list to 0 for immediate recheck\n");
132 bus_expire_timeout_set_interval (list->timeout, 0);
136 do_expiration_with_current_time (BusExpireList *list,
141 int next_interval, min_wait_time, items_to_expire;
144 min_wait_time = 3600 * 1000; /* this is reset anyway if used */
147 link = _dbus_list_get_first_link (&list->items);
150 DBusList *next = _dbus_list_get_next_link (&list->items, link);
156 elapsed = ELAPSED_MILLISECONDS_SINCE (item->added_tv_sec,
160 if (elapsed >= (double) list->expire_after)
162 _dbus_verbose ("Expiring an item %p\n", item);
164 /* If the expire function fails, we just end up expiring
165 * this item next time we walk through the list. This would
166 * be an indeterminate time normally, so we set up the
167 * next_interval to be "shortly" (just enough to avoid
170 if (!(* list->expire_func) (list, link, list->data))
172 next_interval = _dbus_get_oom_wait ();
181 to_wait = (double) list->expire_after - elapsed;
182 if (min_wait_time > to_wait)
183 min_wait_time = to_wait;
189 if (next_interval < 0 && items_to_expire)
190 next_interval = min_wait_time;
192 return next_interval;
196 bus_expirelist_expire (BusExpireList *list)
202 if (list->items != NULL)
204 long tv_sec, tv_usec;
206 _dbus_get_current_time (&tv_sec, &tv_usec);
208 next_interval = do_expiration_with_current_time (list, tv_sec, tv_usec);
211 bus_expire_timeout_set_interval (list->timeout, next_interval);
215 expire_timeout_handler (void *data)
217 BusExpireList *list = data;
219 _dbus_verbose ("Running %s\n", _DBUS_FUNCTION_NAME);
221 /* note that this may remove the timeout */
222 bus_expirelist_expire (list);
228 bus_expire_list_remove_link (BusExpireList *list,
231 _dbus_list_remove_link (&list->items, link);
235 bus_expire_list_remove (BusExpireList *list,
238 return _dbus_list_remove (&list->items, item);
242 bus_expire_list_unlink (BusExpireList *list,
245 _dbus_list_unlink (&list->items, link);
249 bus_expire_list_add (BusExpireList *list,
254 ret = _dbus_list_prepend (&list->items, item);
255 if (ret && !dbus_timeout_get_enabled (list->timeout))
256 bus_expire_timeout_set_interval (list->timeout, 0);
262 bus_expire_list_add_link (BusExpireList *list,
265 _dbus_assert (link->data != NULL);
267 _dbus_list_prepend_link (&list->items, link);
269 if (!dbus_timeout_get_enabled (list->timeout))
270 bus_expire_timeout_set_interval (list->timeout, 0);
274 bus_expire_list_get_first_link (BusExpireList *list)
276 return _dbus_list_get_first_link (&list->items);
280 bus_expire_list_get_next_link (BusExpireList *list,
283 return _dbus_list_get_next_link (&list->items, link);
287 bus_expire_list_contains_item (BusExpireList *list,
290 return _dbus_list_find_last (&list->items, item) != NULL;
293 #ifdef DBUS_BUILD_TESTS
302 test_expire_func (BusExpireList *list,
308 t = (TestExpireItem*) link->data;
310 t->expire_count += 1;
316 time_add_milliseconds (long *tv_sec,
320 *tv_sec = *tv_sec + milliseconds / 1000;
321 *tv_usec = *tv_usec + milliseconds * 1000;
322 if (*tv_usec >= 1000000)
330 bus_expire_list_test (const DBusString *test_data_dir)
334 long tv_sec, tv_usec;
335 long tv_sec_not_expired, tv_usec_not_expired;
336 long tv_sec_expired, tv_usec_expired;
337 long tv_sec_past, tv_usec_past;
338 TestExpireItem *item;
340 dbus_bool_t result = FALSE;
343 loop = _dbus_loop_new ();
344 _dbus_assert (loop != NULL);
346 #define EXPIRE_AFTER 100
348 list = bus_expire_list_new (loop, EXPIRE_AFTER,
349 test_expire_func, NULL);
350 _dbus_assert (list != NULL);
352 _dbus_get_current_time (&tv_sec, &tv_usec);
354 tv_sec_not_expired = tv_sec;
355 tv_usec_not_expired = tv_usec;
356 time_add_milliseconds (&tv_sec_not_expired,
357 &tv_usec_not_expired, EXPIRE_AFTER - 1);
359 tv_sec_expired = tv_sec;
360 tv_usec_expired = tv_usec;
361 time_add_milliseconds (&tv_sec_expired,
362 &tv_usec_expired, EXPIRE_AFTER);
365 tv_sec_past = tv_sec - 1;
366 tv_usec_past = tv_usec;
368 item = dbus_new0 (TestExpireItem, 1);
373 item->item.added_tv_sec = tv_sec;
374 item->item.added_tv_usec = tv_usec;
375 if (!bus_expire_list_add (list, &item->item))
376 _dbus_assert_not_reached ("out of memory");
379 do_expiration_with_current_time (list, tv_sec_not_expired,
380 tv_usec_not_expired);
381 _dbus_assert (item->expire_count == 0);
382 _dbus_verbose ("next_interval = %d\n", next_interval);
383 _dbus_assert (next_interval == 1);
386 do_expiration_with_current_time (list, tv_sec_expired,
388 _dbus_assert (item->expire_count == 1);
389 _dbus_verbose ("next_interval = %d\n", next_interval);
390 _dbus_assert (next_interval == -1);
393 do_expiration_with_current_time (list, tv_sec_past,
395 _dbus_assert (item->expire_count == 1);
396 _dbus_verbose ("next_interval = %d\n", next_interval);
397 _dbus_assert (next_interval == 1000 + EXPIRE_AFTER);
399 bus_expire_list_remove (list, &item->item);
402 bus_expire_list_free (list);
403 _dbus_loop_unref (loop);
411 #endif /* DBUS_BUILD_TESTS */