fix expiration of pending replies
[platform/upstream/dbus.git] / bus / expirelist.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* expirelist.c  List of items that expire
3  *
4  * Copyright (C) 2003  Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #include "expirelist.h"
25 #include "test.h"
26 #include <dbus/dbus-internals.h>
27 #include <dbus/dbus-mainloop.h>
28 #include <dbus/dbus-timeout.h>
29
30 struct BusExpireList
31 {
32   DBusList      *items; /**< List of BusExpireItem */
33   DBusTimeout   *timeout;
34   DBusLoop      *loop;
35   BusExpireFunc  expire_func;
36   void          *data;
37   int            expire_after; /**< Expire after milliseconds (thousandths) */
38 };
39
40 static dbus_bool_t expire_timeout_handler (void *data);
41
42 static void
43 call_timeout_callback (DBusTimeout   *timeout,
44                        void          *data)
45 {
46   /* can return FALSE on OOM but we just let it fire again later */
47   dbus_timeout_handle (timeout);
48 }
49
50 BusExpireList*
51 bus_expire_list_new (DBusLoop      *loop,
52                      int            expire_after,
53                      BusExpireFunc  expire_func,
54                      void          *data)
55 {
56   BusExpireList *list;
57
58   list = dbus_new0 (BusExpireList, 1);
59   if (list == NULL)
60     return NULL;
61
62   list->expire_func = expire_func;
63   list->data = data;
64   list->loop = loop;
65   list->expire_after = expire_after;
66
67   list->timeout = _dbus_timeout_new (100, /* irrelevant */
68                                      expire_timeout_handler,
69                                      list, NULL);
70   if (list->timeout == NULL)
71     goto failed;
72
73   _dbus_timeout_set_enabled (list->timeout, FALSE);
74
75   if (!_dbus_loop_add_timeout (list->loop,
76                                list->timeout,
77                                call_timeout_callback, NULL, NULL))
78     goto failed;
79
80   return list;
81
82  failed:
83   if (list->timeout)
84     _dbus_timeout_unref (list->timeout);
85
86   dbus_free (list);
87
88   return NULL;
89 }
90
91 void
92 bus_expire_list_free (BusExpireList *list)
93 {
94   _dbus_assert (list->items == NULL);
95
96   _dbus_loop_remove_timeout (list->loop, list->timeout,
97                              call_timeout_callback, NULL);
98
99   _dbus_timeout_unref (list->timeout);
100
101   dbus_free (list);
102 }
103
104 void
105 bus_expire_timeout_set_interval (DBusTimeout   *timeout,
106                                  int            next_interval)
107 {
108   if (next_interval >= 0)
109     {
110       _dbus_timeout_set_interval (timeout,
111                                   next_interval);
112       _dbus_timeout_set_enabled (timeout, TRUE);
113
114       _dbus_verbose ("Enabled an expire timeout with interval %d\n",
115                      next_interval);
116     }
117   else if (dbus_timeout_get_enabled (timeout))
118     {
119       _dbus_timeout_set_enabled (timeout, FALSE);
120
121       _dbus_verbose ("Disabled an expire timeout\n");
122     }
123   else
124     _dbus_verbose ("No need to disable this expire timeout\n");
125 }
126
127 void
128 bus_expire_list_recheck_immediately (BusExpireList *list)
129 {
130   _dbus_verbose ("setting interval on expire list to 0 for immediate recheck\n");
131
132   bus_expire_timeout_set_interval (list->timeout, 0);
133 }
134
135 static int
136 do_expiration_with_current_time (BusExpireList *list,
137                                  long           tv_sec,
138                                  long           tv_usec)
139 {
140   DBusList *link;
141   int next_interval, min_wait_time, items_to_expire;
142
143   next_interval = -1;
144   min_wait_time = 3600 * 1000; /* this is reset anyway if used */
145   items_to_expire = 0;
146   
147   link = _dbus_list_get_first_link (&list->items);
148   while (link != NULL)
149     {
150       DBusList *next = _dbus_list_get_next_link (&list->items, link);
151       double elapsed;
152       BusExpireItem *item;
153
154       item = link->data;
155
156       elapsed = ELAPSED_MILLISECONDS_SINCE (item->added_tv_sec,
157                                             item->added_tv_usec,
158                                             tv_sec, tv_usec);
159
160       if (elapsed >= (double) list->expire_after)
161         {
162           _dbus_verbose ("Expiring an item %p\n", item);
163
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
168            * a busy loop)
169            */
170           if (!(* list->expire_func) (list, link, list->data))
171             {
172               next_interval = _dbus_get_oom_wait ();
173               break;
174             }
175         }
176       else
177         {
178           double to_wait;
179
180           items_to_expire = 1;
181           to_wait = (double) list->expire_after - elapsed;
182           if (min_wait_time > to_wait)
183             min_wait_time = to_wait;
184         }
185
186       link = next;
187     }
188
189   if (next_interval < 0 && items_to_expire)
190     next_interval = min_wait_time;
191
192   return next_interval;
193 }
194
195 static void
196 bus_expirelist_expire (BusExpireList *list)
197 {
198   int next_interval;
199
200   next_interval = -1;
201
202   if (list->items != NULL)
203     {
204       long tv_sec, tv_usec;
205
206       _dbus_get_current_time (&tv_sec, &tv_usec);
207
208       next_interval = do_expiration_with_current_time (list, tv_sec, tv_usec);
209     }
210
211   bus_expire_timeout_set_interval (list->timeout, next_interval);
212 }
213
214 static dbus_bool_t
215 expire_timeout_handler (void *data)
216 {
217   BusExpireList *list = data;
218
219   _dbus_verbose ("Running %s\n", _DBUS_FUNCTION_NAME);
220
221   /* note that this may remove the timeout */
222   bus_expirelist_expire (list);
223
224   return TRUE;
225 }
226
227 void
228 bus_expire_list_remove_link (BusExpireList *list,
229                              DBusList      *link)
230 {
231   _dbus_list_remove_link (&list->items, link);
232 }
233
234 dbus_bool_t
235 bus_expire_list_remove (BusExpireList *list,
236                         BusExpireItem *item)
237 {
238   return _dbus_list_remove (&list->items, item);
239 }
240
241 void
242 bus_expire_list_unlink (BusExpireList *list,
243                         DBusList      *link)
244 {
245   _dbus_list_unlink (&list->items, link);
246 }
247
248 dbus_bool_t
249 bus_expire_list_add (BusExpireList *list,
250                      BusExpireItem *item)
251 {
252   dbus_bool_t ret;
253
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);
257
258   return ret;
259 }
260
261 void
262 bus_expire_list_add_link (BusExpireList *list,
263                           DBusList      *link)
264 {
265   _dbus_assert (link->data != NULL);
266   
267   _dbus_list_prepend_link (&list->items, link);
268
269   if (!dbus_timeout_get_enabled (list->timeout))
270     bus_expire_timeout_set_interval (list->timeout, 0);
271 }
272
273 DBusList*
274 bus_expire_list_get_first_link (BusExpireList *list)
275 {
276   return _dbus_list_get_first_link (&list->items);
277 }
278
279 DBusList*
280 bus_expire_list_get_next_link (BusExpireList *list,
281                                DBusList      *link)
282 {
283   return _dbus_list_get_next_link (&list->items, link);
284 }
285
286 dbus_bool_t
287 bus_expire_list_contains_item (BusExpireList *list,
288                                BusExpireItem *item)
289 {
290   return _dbus_list_find_last (&list->items, item) != NULL;
291 }
292
293 #ifdef DBUS_BUILD_TESTS
294
295 typedef struct
296 {
297   BusExpireItem item;
298   int expire_count;
299 } TestExpireItem;
300
301 static dbus_bool_t
302 test_expire_func (BusExpireList *list,
303                   DBusList      *link,
304                   void          *data)
305 {
306   TestExpireItem *t;
307
308   t = (TestExpireItem*) link->data;
309
310   t->expire_count += 1;
311
312   return TRUE;
313 }
314
315 static void
316 time_add_milliseconds (long *tv_sec,
317                        long *tv_usec,
318                        int   milliseconds)
319 {
320   *tv_sec = *tv_sec + milliseconds / 1000;
321   *tv_usec = *tv_usec + milliseconds * 1000;
322   if (*tv_usec >= 1000000)
323     {
324       *tv_usec -= 1000000;
325       *tv_sec += 1;
326     }
327 }
328
329 dbus_bool_t
330 bus_expire_list_test (const DBusString *test_data_dir)
331 {
332   DBusLoop *loop;
333   BusExpireList *list;
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;
339   int next_interval;
340   dbus_bool_t result = FALSE;
341
342
343   loop = _dbus_loop_new ();
344   _dbus_assert (loop != NULL);
345
346 #define EXPIRE_AFTER 100
347   
348   list = bus_expire_list_new (loop, EXPIRE_AFTER,
349                               test_expire_func, NULL);
350   _dbus_assert (list != NULL);
351
352   _dbus_get_current_time (&tv_sec, &tv_usec);
353
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);
358
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);
363   
364
365   tv_sec_past = tv_sec - 1;
366   tv_usec_past = tv_usec;
367
368   item = dbus_new0 (TestExpireItem, 1);
369
370   if (item == NULL)
371     goto oom;
372
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");
377
378   next_interval =
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);
384   
385   next_interval =
386     do_expiration_with_current_time (list, tv_sec_expired,
387                                      tv_usec_expired);
388   _dbus_assert (item->expire_count == 1);
389   _dbus_verbose ("next_interval = %d\n", next_interval);
390   _dbus_assert (next_interval == -1);
391
392   next_interval =
393     do_expiration_with_current_time (list, tv_sec_past,
394                                      tv_usec_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);
398
399   bus_expire_list_remove (list, &item->item);
400   dbus_free (item);
401   
402   bus_expire_list_free (list);
403   _dbus_loop_unref (loop);
404   
405   result = TRUE;
406
407  oom:
408   return result;
409 }
410
411 #endif /* DBUS_BUILD_TESTS */