*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
+#include <config.h>
#include "expirelist.h"
#include "test.h"
#include <dbus/dbus-internals.h>
static dbus_bool_t expire_timeout_handler (void *data);
-static void
-call_timeout_callback (DBusTimeout *timeout,
- void *data)
-{
- /* can return FALSE on OOM but we just let it fire again later */
- dbus_timeout_handle (timeout);
-}
-
BusExpireList*
bus_expire_list_new (DBusLoop *loop,
int expire_after,
_dbus_timeout_set_enabled (list->timeout, FALSE);
- if (!_dbus_loop_add_timeout (list->loop,
- list->timeout,
- call_timeout_callback, NULL, NULL))
+ if (!_dbus_loop_add_timeout (list->loop, list->timeout))
goto failed;
return list;
{
_dbus_assert (list->items == NULL);
- _dbus_loop_remove_timeout (list->loop, list->timeout,
- call_timeout_callback, NULL);
+ _dbus_loop_remove_timeout (list->loop, list->timeout);
_dbus_timeout_unref (list->timeout);
}
static int
-do_expiration_with_current_time (BusExpireList *list,
- long tv_sec,
- long tv_usec)
+do_expiration_with_monotonic_time (BusExpireList *list,
+ long tv_sec,
+ long tv_usec)
{
DBusList *link;
- int next_interval;
+ int next_interval, min_wait_time, items_to_expire;
next_interval = -1;
+ min_wait_time = 3600 * 1000; /* this is reset anyway if used */
+ items_to_expire = 0;
link = _dbus_list_get_first_link (&list->items);
while (link != NULL)
item->added_tv_usec,
tv_sec, tv_usec);
- if (elapsed >= (double) list->expire_after)
+ if (((item->added_tv_sec == 0) && (item->added_tv_usec == 0)) ||
+ ((list->expire_after > 0) && (elapsed >= (double) list->expire_after)))
{
_dbus_verbose ("Expiring an item %p\n", item);
break;
}
}
- else
+ else if (list->expire_after > 0)
{
- /* We can end the loop, since the connections are in oldest-first order */
- next_interval = ((double)list->expire_after) - elapsed;
- _dbus_verbose ("Item %p expires in %d milliseconds\n",
- item, next_interval);
+ double to_wait;
- break;
+ items_to_expire = 1;
+ to_wait = (double) list->expire_after - elapsed;
+ if (min_wait_time > to_wait)
+ min_wait_time = to_wait;
}
link = next;
}
+ if (next_interval < 0 && items_to_expire)
+ next_interval = min_wait_time;
+
return next_interval;
}
{
long tv_sec, tv_usec;
- _dbus_get_current_time (&tv_sec, &tv_usec);
+ _dbus_get_monotonic_time (&tv_sec, &tv_usec);
- next_interval = do_expiration_with_current_time (list, tv_sec, tv_usec);
+ next_interval = do_expiration_with_monotonic_time (list, tv_sec, tv_usec);
}
bus_expire_timeout_set_interval (list->timeout, next_interval);
{
BusExpireList *list = data;
- _dbus_verbose ("Running %s\n", _DBUS_FUNCTION_NAME);
+ _dbus_verbose ("Running\n");
/* note that this may remove the timeout */
bus_expirelist_expire (list);
bus_expire_list_remove_link (BusExpireList *list,
DBusList *link)
{
- _dbus_list_remove_link (&list->items,
- link);
+ _dbus_list_remove_link (&list->items, link);
}
dbus_bool_t
bus_expire_list_remove (BusExpireList *list,
BusExpireItem *item)
{
- return _dbus_list_remove (&list->items,
- item);
+ return _dbus_list_remove (&list->items, item);
}
void
bus_expire_list_add (BusExpireList *list,
BusExpireItem *item)
{
- return _dbus_list_prepend (&list->items,
- item);
+ dbus_bool_t ret;
+
+ ret = _dbus_list_prepend (&list->items, item);
+ if (ret && !dbus_timeout_get_enabled (list->timeout))
+ bus_expire_timeout_set_interval (list->timeout, 0);
+
+ return ret;
}
void
{
_dbus_assert (link->data != NULL);
- _dbus_list_prepend_link (&list->items,
- link);
+ _dbus_list_prepend_link (&list->items, link);
+
+ if (!dbus_timeout_get_enabled (list->timeout))
+ bus_expire_timeout_set_interval (list->timeout, 0);
}
DBusList*
bus_expire_list_get_next_link (BusExpireList *list,
DBusList *link)
{
- return _dbus_list_get_next_link (&list->items,
- link);
+ return _dbus_list_get_next_link (&list->items, link);
}
dbus_bool_t
return _dbus_list_find_last (&list->items, item) != NULL;
}
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
typedef struct
{
test_expire_func, NULL);
_dbus_assert (list != NULL);
- _dbus_get_current_time (&tv_sec, &tv_usec);
+ _dbus_get_monotonic_time (&tv_sec, &tv_usec);
tv_sec_not_expired = tv_sec;
tv_usec_not_expired = tv_usec;
_dbus_assert_not_reached ("out of memory");
next_interval =
- do_expiration_with_current_time (list, tv_sec_not_expired,
- tv_usec_not_expired);
+ do_expiration_with_monotonic_time (list, tv_sec_not_expired,
+ tv_usec_not_expired);
_dbus_assert (item->expire_count == 0);
_dbus_verbose ("next_interval = %d\n", next_interval);
_dbus_assert (next_interval == 1);
next_interval =
- do_expiration_with_current_time (list, tv_sec_expired,
- tv_usec_expired);
+ do_expiration_with_monotonic_time (list, tv_sec_expired,
+ tv_usec_expired);
_dbus_assert (item->expire_count == 1);
_dbus_verbose ("next_interval = %d\n", next_interval);
_dbus_assert (next_interval == -1);
next_interval =
- do_expiration_with_current_time (list, tv_sec_past,
- tv_usec_past);
+ do_expiration_with_monotonic_time (list, tv_sec_past,
+ tv_usec_past);
_dbus_assert (item->expire_count == 1);
_dbus_verbose ("next_interval = %d\n", next_interval);
_dbus_assert (next_interval == 1000 + EXPIRE_AFTER);
return result;
}
-#endif /* DBUS_BUILD_TESTS */
+#endif /* DBUS_ENABLE_EMBEDDED_TESTS */