expirelist.c 10.3 KB
Newer Older
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
Havoc Pennington's avatar
Havoc Pennington committed
2 3 4 5
/* expirelist.c  List of items that expire
 *
 * Copyright (C) 2003  Red Hat, Inc.
 *
6
 * Licensed under the Academic Free License version 2.1
Havoc Pennington's avatar
Havoc Pennington committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * 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
 *
 */

#include "expirelist.h"
#include "test.h"
#include <dbus/dbus-internals.h>
#include <dbus/dbus-mainloop.h>
#include <dbus/dbus-timeout.h>

30 31 32 33 34 35 36 37 38 39
struct BusExpireList
{
  DBusList      *items; /**< List of BusExpireItem */
  DBusTimeout   *timeout;
  DBusLoop      *loop;
  BusExpireFunc  expire_func;
  void          *data;
  int            expire_after; /**< Expire after milliseconds (thousandths) */
};

Havoc Pennington's avatar
Havoc Pennington committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
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,
                     BusExpireFunc  expire_func,
                     void          *data)
{
  BusExpireList *list;

  list = dbus_new0 (BusExpireList, 1);
  if (list == NULL)
    return NULL;

  list->expire_func = expire_func;
  list->data = data;
  list->loop = loop;
  list->expire_after = expire_after;

  list->timeout = _dbus_timeout_new (100, /* irrelevant */
                                     expire_timeout_handler,
                                     list, NULL);
  if (list->timeout == NULL)
    goto failed;

  _dbus_timeout_set_enabled (list->timeout, FALSE);

  if (!_dbus_loop_add_timeout (list->loop,
                               list->timeout,
                               call_timeout_callback, NULL, NULL))
    goto failed;

  return list;

 failed:
  if (list->timeout)
    _dbus_timeout_unref (list->timeout);

  dbus_free (list);

  return NULL;
}

void
bus_expire_list_free (BusExpireList *list)
{
  _dbus_assert (list->items == NULL);

  _dbus_loop_remove_timeout (list->loop, list->timeout,
                             call_timeout_callback, NULL);

  _dbus_timeout_unref (list->timeout);

  dbus_free (list);
}

void
105 106
bus_expire_timeout_set_interval (DBusTimeout   *timeout,
                                 int            next_interval)
Havoc Pennington's avatar
Havoc Pennington committed
107 108 109 110 111 112 113
{
  if (next_interval >= 0)
    {
      _dbus_timeout_set_interval (timeout,
                                  next_interval);
      _dbus_timeout_set_enabled (timeout, TRUE);

114
      _dbus_verbose ("Enabled an expire timeout with interval %d\n",
Havoc Pennington's avatar
Havoc Pennington committed
115 116 117 118 119 120
                     next_interval);
    }
  else if (dbus_timeout_get_enabled (timeout))
    {
      _dbus_timeout_set_enabled (timeout, FALSE);

121
      _dbus_verbose ("Disabled an expire timeout\n");
Havoc Pennington's avatar
Havoc Pennington committed
122 123
    }
  else
124 125 126 127 128 129 130 131 132
    _dbus_verbose ("No need to disable this expire timeout\n");
}

void
bus_expire_list_recheck_immediately (BusExpireList *list)
{
  _dbus_verbose ("setting interval on expire list to 0 for immediate recheck\n");

  bus_expire_timeout_set_interval (list->timeout, 0);
Havoc Pennington's avatar
Havoc Pennington committed
133 134 135 136 137 138 139 140
}

static int
do_expiration_with_current_time (BusExpireList *list,
                                 long           tv_sec,
                                 long           tv_usec)
{
  DBusList *link;
141
  int next_interval, min_wait_time, items_to_expire;
Havoc Pennington's avatar
Havoc Pennington committed
142 143

  next_interval = -1;
144 145
  min_wait_time = 3600 * 1000; /* this is reset anyway if used */
  items_to_expire = 0;
Havoc Pennington's avatar
Havoc Pennington committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  
  link = _dbus_list_get_first_link (&list->items);
  while (link != NULL)
    {
      DBusList *next = _dbus_list_get_next_link (&list->items, link);
      double elapsed;
      BusExpireItem *item;

      item = link->data;

      elapsed = ELAPSED_MILLISECONDS_SINCE (item->added_tv_sec,
                                            item->added_tv_usec,
                                            tv_sec, tv_usec);

      if (elapsed >= (double) list->expire_after)
        {
          _dbus_verbose ("Expiring an item %p\n", item);
163 164

          /* If the expire function fails, we just end up expiring
165 166 167 168
           * this item next time we walk through the list. This would
           * be an indeterminate time normally, so we set up the
           * next_interval to be "shortly" (just enough to avoid
           * a busy loop)
169
           */
170 171 172 173 174
          if (!(* list->expire_func) (list, link, list->data))
            {
              next_interval = _dbus_get_oom_wait ();
              break;
            }
Havoc Pennington's avatar
Havoc Pennington committed
175 176 177
        }
      else
        {
178
          double to_wait;
Havoc Pennington's avatar
Havoc Pennington committed
179

180 181 182 183
          items_to_expire = 1;
          to_wait = (double) list->expire_after - elapsed;
          if (min_wait_time > to_wait)
            min_wait_time = to_wait;
Havoc Pennington's avatar
Havoc Pennington committed
184 185 186 187 188
        }

      link = next;
    }

189 190 191
  if (next_interval < 0 && items_to_expire)
    next_interval = min_wait_time;

Havoc Pennington's avatar
Havoc Pennington committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  return next_interval;
}

static void
bus_expirelist_expire (BusExpireList *list)
{
  int next_interval;

  next_interval = -1;

  if (list->items != NULL)
    {
      long tv_sec, tv_usec;

      _dbus_get_current_time (&tv_sec, &tv_usec);

      next_interval = do_expiration_with_current_time (list, tv_sec, tv_usec);
    }

  bus_expire_timeout_set_interval (list->timeout, next_interval);
}

static dbus_bool_t
expire_timeout_handler (void *data)
{
  BusExpireList *list = data;

  _dbus_verbose ("Running %s\n", _DBUS_FUNCTION_NAME);

  /* note that this may remove the timeout */
  bus_expirelist_expire (list);

  return TRUE;
}

227 228 229 230
void
bus_expire_list_remove_link (BusExpireList *list,
                             DBusList      *link)
{
231
  _dbus_list_remove_link (&list->items, link);
232 233 234 235 236 237
}

dbus_bool_t
bus_expire_list_remove (BusExpireList *list,
                        BusExpireItem *item)
{
238
  return _dbus_list_remove (&list->items, item);
239 240 241 242 243 244 245 246 247 248 249 250 251
}

void
bus_expire_list_unlink (BusExpireList *list,
                        DBusList      *link)
{
  _dbus_list_unlink (&list->items, link);
}

dbus_bool_t
bus_expire_list_add (BusExpireList *list,
                     BusExpireItem *item)
{
252 253 254 255 256 257 258
  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;
259 260 261 262 263 264 265 266
}

void
bus_expire_list_add_link (BusExpireList *list,
                          DBusList      *link)
{
  _dbus_assert (link->data != NULL);
  
267 268 269 270
  _dbus_list_prepend_link (&list->items, link);

  if (!dbus_timeout_get_enabled (list->timeout))
    bus_expire_timeout_set_interval (list->timeout, 0);
271 272 273 274 275 276 277 278 279 280 281 282
}

DBusList*
bus_expire_list_get_first_link (BusExpireList *list)
{
  return _dbus_list_get_first_link (&list->items);
}

DBusList*
bus_expire_list_get_next_link (BusExpireList *list,
                               DBusList      *link)
{
283
  return _dbus_list_get_next_link (&list->items, link);
284 285 286 287 288 289 290 291 292
}

dbus_bool_t
bus_expire_list_contains_item (BusExpireList *list,
                               BusExpireItem *item)
{
  return _dbus_list_find_last (&list->items, item) != NULL;
}

Havoc Pennington's avatar
Havoc Pennington committed
293 294 295 296 297 298 299 300
#ifdef DBUS_BUILD_TESTS

typedef struct
{
  BusExpireItem item;
  int expire_count;
} TestExpireItem;

301
static dbus_bool_t
Havoc Pennington's avatar
Havoc Pennington committed
302
test_expire_func (BusExpireList *list,
303
                  DBusList      *link,
Havoc Pennington's avatar
Havoc Pennington committed
304 305 306 307
                  void          *data)
{
  TestExpireItem *t;

308
  t = (TestExpireItem*) link->data;
Havoc Pennington's avatar
Havoc Pennington committed
309 310

  t->expire_count += 1;
311 312

  return TRUE;
Havoc Pennington's avatar
Havoc Pennington committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
}

static void
time_add_milliseconds (long *tv_sec,
                       long *tv_usec,
                       int   milliseconds)
{
  *tv_sec = *tv_sec + milliseconds / 1000;
  *tv_usec = *tv_usec + milliseconds * 1000;
  if (*tv_usec >= 1000000)
    {
      *tv_usec -= 1000000;
      *tv_sec += 1;
    }
}

dbus_bool_t
bus_expire_list_test (const DBusString *test_data_dir)
{
  DBusLoop *loop;
  BusExpireList *list;
  long tv_sec, tv_usec;
  long tv_sec_not_expired, tv_usec_not_expired;
  long tv_sec_expired, tv_usec_expired;
  long tv_sec_past, tv_usec_past;
  TestExpireItem *item;
  int next_interval;
340 341 342
  dbus_bool_t result = FALSE;


Havoc Pennington's avatar
Havoc Pennington committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
  loop = _dbus_loop_new ();
  _dbus_assert (loop != NULL);

#define EXPIRE_AFTER 100
  
  list = bus_expire_list_new (loop, EXPIRE_AFTER,
                              test_expire_func, NULL);
  _dbus_assert (list != NULL);

  _dbus_get_current_time (&tv_sec, &tv_usec);

  tv_sec_not_expired = tv_sec;
  tv_usec_not_expired = tv_usec;
  time_add_milliseconds (&tv_sec_not_expired,
                         &tv_usec_not_expired, EXPIRE_AFTER - 1);

  tv_sec_expired = tv_sec;
  tv_usec_expired = tv_usec;
  time_add_milliseconds (&tv_sec_expired,
                         &tv_usec_expired, EXPIRE_AFTER);
  

  tv_sec_past = tv_sec - 1;
  tv_usec_past = tv_usec;

  item = dbus_new0 (TestExpireItem, 1);

370 371 372
  if (item == NULL)
    goto oom;

Havoc Pennington's avatar
Havoc Pennington committed
373 374
  item->item.added_tv_sec = tv_sec;
  item->item.added_tv_usec = tv_usec;
375
  if (!bus_expire_list_add (list, &item->item))
Havoc Pennington's avatar
Havoc Pennington committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    _dbus_assert_not_reached ("out of memory");

  next_interval =
    do_expiration_with_current_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);
  _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);
  _dbus_assert (item->expire_count == 1);
  _dbus_verbose ("next_interval = %d\n", next_interval);
  _dbus_assert (next_interval == 1000 + EXPIRE_AFTER);

399
  bus_expire_list_remove (list, &item->item);
Havoc Pennington's avatar
Havoc Pennington committed
400 401 402 403 404
  dbus_free (item);
  
  bus_expire_list_free (list);
  _dbus_loop_unref (loop);
  
405 406 407 408
  result = TRUE;

 oom:
  return result;
Havoc Pennington's avatar
Havoc Pennington committed
409 410 411
}

#endif /* DBUS_BUILD_TESTS */