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