Merge branch 'fd-passing'
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  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 (((item->added_tv_sec == 0) && (item->added_tv_usec == 0)) ||
161           ((list->expire_after > 0) && (elapsed >= (double) list->expire_after)))
162         {
163           _dbus_verbose ("Expiring an item %p\n", item);
164
165           /* If the expire function fails, we just end up expiring
166            * this item next time we walk through the list. This would
167            * be an indeterminate time normally, so we set up the
168            * next_interval to be "shortly" (just enough to avoid
169            * a busy loop)
170            */
171           if (!(* list->expire_func) (list, link, list->data))
172             {
173               next_interval = _dbus_get_oom_wait ();
174               break;
175             }
176         }
177       else if (list->expire_after > 0)
178         {
179           double to_wait;
180
181           items_to_expire = 1;
182           to_wait = (double) list->expire_after - elapsed;
183           if (min_wait_time > to_wait)
184             min_wait_time = to_wait;
185         }
186
187       link = next;
188     }
189
190   if (next_interval < 0 && items_to_expire)
191     next_interval = min_wait_time;
192
193   return next_interval;
194 }
195
196 static void
197 bus_expirelist_expire (BusExpireList *list)
198 {
199   int next_interval;
200
201   next_interval = -1;
202
203   if (list->items != NULL)
204     {
205       long tv_sec, tv_usec;
206
207       _dbus_get_current_time (&tv_sec, &tv_usec);
208
209       next_interval = do_expiration_with_current_time (list, tv_sec, tv_usec);
210     }
211
212   bus_expire_timeout_set_interval (list->timeout, next_interval);
213 }
214
215 static dbus_bool_t
216 expire_timeout_handler (void *data)
217 {
218   BusExpireList *list = data;
219
220   _dbus_verbose ("Running %s\n", _DBUS_FUNCTION_NAME);
221
222   /* note that this may remove the timeout */
223   bus_expirelist_expire (list);
224
225   return TRUE;
226 }
227
228 void
229 bus_expire_list_remove_link (BusExpireList *list,
230                              DBusList      *link)
231 {
232   _dbus_list_remove_link (&list->items, link);
233 }
234
235 dbus_bool_t
236 bus_expire_list_remove (BusExpireList *list,
237                         BusExpireItem *item)
238 {
239   return _dbus_list_remove (&list->items, item);
240 }
241
242 void
243 bus_expire_list_unlink (BusExpireList *list,
244                         DBusList      *link)
245 {
246   _dbus_list_unlink (&list->items, link);
247 }
248
249 dbus_bool_t
250 bus_expire_list_add (BusExpireList *list,
251                      BusExpireItem *item)
252 {
253   dbus_bool_t ret;
254
255   ret = _dbus_list_prepend (&list->items, item);
256   if (ret && !dbus_timeout_get_enabled (list->timeout))
257     bus_expire_timeout_set_interval (list->timeout, 0);
258
259   return ret;
260 }
261
262 void
263 bus_expire_list_add_link (BusExpireList *list,
264                           DBusList      *link)
265 {
266   _dbus_assert (link->data != NULL);
267   
268   _dbus_list_prepend_link (&list->items, link);
269
270   if (!dbus_timeout_get_enabled (list->timeout))
271     bus_expire_timeout_set_interval (list->timeout, 0);
272 }
273
274 DBusList*
275 bus_expire_list_get_first_link (BusExpireList *list)
276 {
277   return _dbus_list_get_first_link (&list->items);
278 }
279
280 DBusList*
281 bus_expire_list_get_next_link (BusExpireList *list,
282                                DBusList      *link)
283 {
284   return _dbus_list_get_next_link (&list->items, link);
285 }
286
287 dbus_bool_t
288 bus_expire_list_contains_item (BusExpireList *list,
289                                BusExpireItem *item)
290 {
291   return _dbus_list_find_last (&list->items, item) != NULL;
292 }
293
294 #ifdef DBUS_BUILD_TESTS
295
296 typedef struct
297 {
298   BusExpireItem item;
299   int expire_count;
300 } TestExpireItem;
301
302 static dbus_bool_t
303 test_expire_func (BusExpireList *list,
304                   DBusList      *link,
305                   void          *data)
306 {
307   TestExpireItem *t;
308
309   t = (TestExpireItem*) link->data;
310
311   t->expire_count += 1;
312
313   return TRUE;
314 }
315
316 static void
317 time_add_milliseconds (long *tv_sec,
318                        long *tv_usec,
319                        int   milliseconds)
320 {
321   *tv_sec = *tv_sec + milliseconds / 1000;
322   *tv_usec = *tv_usec + milliseconds * 1000;
323   if (*tv_usec >= 1000000)
324     {
325       *tv_usec -= 1000000;
326       *tv_sec += 1;
327     }
328 }
329
330 dbus_bool_t
331 bus_expire_list_test (const DBusString *test_data_dir)
332 {
333   DBusLoop *loop;
334   BusExpireList *list;
335   long tv_sec, tv_usec;
336   long tv_sec_not_expired, tv_usec_not_expired;
337   long tv_sec_expired, tv_usec_expired;
338   long tv_sec_past, tv_usec_past;
339   TestExpireItem *item;
340   int next_interval;
341   dbus_bool_t result = FALSE;
342
343
344   loop = _dbus_loop_new ();
345   _dbus_assert (loop != NULL);
346
347 #define EXPIRE_AFTER 100
348   
349   list = bus_expire_list_new (loop, EXPIRE_AFTER,
350                               test_expire_func, NULL);
351   _dbus_assert (list != NULL);
352
353   _dbus_get_current_time (&tv_sec, &tv_usec);
354
355   tv_sec_not_expired = tv_sec;
356   tv_usec_not_expired = tv_usec;
357   time_add_milliseconds (&tv_sec_not_expired,
358                          &tv_usec_not_expired, EXPIRE_AFTER - 1);
359
360   tv_sec_expired = tv_sec;
361   tv_usec_expired = tv_usec;
362   time_add_milliseconds (&tv_sec_expired,
363                          &tv_usec_expired, EXPIRE_AFTER);
364   
365
366   tv_sec_past = tv_sec - 1;
367   tv_usec_past = tv_usec;
368
369   item = dbus_new0 (TestExpireItem, 1);
370
371   if (item == NULL)
372     goto oom;
373
374   item->item.added_tv_sec = tv_sec;
375   item->item.added_tv_usec = tv_usec;
376   if (!bus_expire_list_add (list, &item->item))
377     _dbus_assert_not_reached ("out of memory");
378
379   next_interval =
380     do_expiration_with_current_time (list, tv_sec_not_expired,
381                                      tv_usec_not_expired);
382   _dbus_assert (item->expire_count == 0);
383   _dbus_verbose ("next_interval = %d\n", next_interval);
384   _dbus_assert (next_interval == 1);
385   
386   next_interval =
387     do_expiration_with_current_time (list, tv_sec_expired,
388                                      tv_usec_expired);
389   _dbus_assert (item->expire_count == 1);
390   _dbus_verbose ("next_interval = %d\n", next_interval);
391   _dbus_assert (next_interval == -1);
392
393   next_interval =
394     do_expiration_with_current_time (list, tv_sec_past,
395                                      tv_usec_past);
396   _dbus_assert (item->expire_count == 1);
397   _dbus_verbose ("next_interval = %d\n", next_interval);
398   _dbus_assert (next_interval == 1000 + EXPIRE_AFTER);
399
400   bus_expire_list_remove (list, &item->item);
401   dbus_free (item);
402   
403   bus_expire_list_free (list);
404   _dbus_loop_unref (loop);
405   
406   result = TRUE;
407
408  oom:
409   return result;
410 }
411
412 #endif /* DBUS_BUILD_TESTS */