2003-12-02 Richard Hult <richard@imendio.com>
[platform/upstream/dbus.git] / bus / expirelist.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
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.0
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 static dbus_bool_t expire_timeout_handler (void *data);
31
32 static void
33 call_timeout_callback (DBusTimeout   *timeout,
34                        void          *data)
35 {
36   /* can return FALSE on OOM but we just let it fire again later */
37   dbus_timeout_handle (timeout);
38 }
39
40 BusExpireList*
41 bus_expire_list_new (DBusLoop      *loop,
42                      int            expire_after,
43                      BusExpireFunc  expire_func,
44                      void          *data)
45 {
46   BusExpireList *list;
47
48   list = dbus_new0 (BusExpireList, 1);
49   if (list == NULL)
50     return NULL;
51
52   list->expire_func = expire_func;
53   list->data = data;
54   list->loop = loop;
55   list->expire_after = expire_after;
56
57   list->timeout = _dbus_timeout_new (100, /* irrelevant */
58                                      expire_timeout_handler,
59                                      list, NULL);
60   if (list->timeout == NULL)
61     goto failed;
62
63   _dbus_timeout_set_enabled (list->timeout, FALSE);
64
65   if (!_dbus_loop_add_timeout (list->loop,
66                                list->timeout,
67                                call_timeout_callback, NULL, NULL))
68     goto failed;
69
70   return list;
71
72  failed:
73   if (list->timeout)
74     _dbus_timeout_unref (list->timeout);
75
76   dbus_free (list);
77
78   return NULL;
79 }
80
81 void
82 bus_expire_list_free (BusExpireList *list)
83 {
84   _dbus_assert (list->n_items == 0);
85   _dbus_assert (list->items == NULL);
86
87   _dbus_loop_remove_timeout (list->loop, list->timeout,
88                              call_timeout_callback, NULL);
89
90   _dbus_timeout_unref (list->timeout);
91
92   dbus_free (list);
93 }
94
95 void
96 bus_expire_timeout_set_interval (DBusTimeout *timeout,
97                                  int          next_interval)
98 {
99   if (next_interval >= 0)
100     {
101       _dbus_timeout_set_interval (timeout,
102                                   next_interval);
103       _dbus_timeout_set_enabled (timeout, TRUE);
104
105       _dbus_verbose ("Enabled expire timeout with interval %d\n",
106                      next_interval);
107     }
108   else if (dbus_timeout_get_enabled (timeout))
109     {
110       _dbus_timeout_set_enabled (timeout, FALSE);
111
112       _dbus_verbose ("Disabled expire timeout\n");
113     }
114   else
115     _dbus_verbose ("No need to disable expire timeout\n");
116 }
117
118 static int
119 do_expiration_with_current_time (BusExpireList *list,
120                                  long           tv_sec,
121                                  long           tv_usec)
122 {
123   DBusList *link;
124   int next_interval;
125
126   next_interval = -1;
127   
128   link = _dbus_list_get_first_link (&list->items);
129   while (link != NULL)
130     {
131       DBusList *next = _dbus_list_get_next_link (&list->items, link);
132       double elapsed;
133       BusExpireItem *item;
134
135       item = link->data;
136
137       elapsed = ELAPSED_MILLISECONDS_SINCE (item->added_tv_sec,
138                                             item->added_tv_usec,
139                                             tv_sec, tv_usec);
140
141       if (elapsed >= (double) list->expire_after)
142         {
143           _dbus_verbose ("Expiring an item %p\n", item);
144
145           /* If the expire function fails, we just end up expiring
146            * this item next time we walk through the list. This would
147            * be an indeterminate time normally, so we set up the
148            * next_interval to be "shortly" (just enough to avoid
149            * a busy loop)
150            */
151           if (!(* list->expire_func) (list, link, list->data))
152             {
153               next_interval = _dbus_get_oom_wait ();
154               break;
155             }
156         }
157       else
158         {
159           /* We can end the loop, since the connections are in oldest-first order */
160           next_interval = ((double)list->expire_after) - elapsed;
161           _dbus_verbose ("Item %p expires in %d milliseconds\n",
162                          item, next_interval);
163
164           break;
165         }
166
167       link = next;
168     }
169
170   return next_interval;
171 }
172
173 static void
174 bus_expirelist_expire (BusExpireList *list)
175 {
176   int next_interval;
177
178   next_interval = -1;
179
180   if (list->items != NULL)
181     {
182       long tv_sec, tv_usec;
183
184       _dbus_get_current_time (&tv_sec, &tv_usec);
185
186       next_interval = do_expiration_with_current_time (list, tv_sec, tv_usec);
187     }
188
189   bus_expire_timeout_set_interval (list->timeout, next_interval);
190 }
191
192 static dbus_bool_t
193 expire_timeout_handler (void *data)
194 {
195   BusExpireList *list = data;
196
197   _dbus_verbose ("Running %s\n", _DBUS_FUNCTION_NAME);
198
199   /* note that this may remove the timeout */
200   bus_expirelist_expire (list);
201
202   return TRUE;
203 }
204
205 #ifdef DBUS_BUILD_TESTS
206
207 typedef struct
208 {
209   BusExpireItem item;
210   int expire_count;
211 } TestExpireItem;
212
213 static dbus_bool_t
214 test_expire_func (BusExpireList *list,
215                   DBusList      *link,
216                   void          *data)
217 {
218   TestExpireItem *t;
219
220   t = (TestExpireItem*) link->data;
221
222   t->expire_count += 1;
223
224   return TRUE;
225 }
226
227 static void
228 time_add_milliseconds (long *tv_sec,
229                        long *tv_usec,
230                        int   milliseconds)
231 {
232   *tv_sec = *tv_sec + milliseconds / 1000;
233   *tv_usec = *tv_usec + milliseconds * 1000;
234   if (*tv_usec >= 1000000)
235     {
236       *tv_usec -= 1000000;
237       *tv_sec += 1;
238     }
239 }
240
241 dbus_bool_t
242 bus_expire_list_test (const DBusString *test_data_dir)
243 {
244   DBusLoop *loop;
245   BusExpireList *list;
246   long tv_sec, tv_usec;
247   long tv_sec_not_expired, tv_usec_not_expired;
248   long tv_sec_expired, tv_usec_expired;
249   long tv_sec_past, tv_usec_past;
250   TestExpireItem *item;
251   int next_interval;
252   
253   loop = _dbus_loop_new ();
254   _dbus_assert (loop != NULL);
255
256 #define EXPIRE_AFTER 100
257   
258   list = bus_expire_list_new (loop, EXPIRE_AFTER,
259                               test_expire_func, NULL);
260   _dbus_assert (list != NULL);
261
262   _dbus_get_current_time (&tv_sec, &tv_usec);
263
264   tv_sec_not_expired = tv_sec;
265   tv_usec_not_expired = tv_usec;
266   time_add_milliseconds (&tv_sec_not_expired,
267                          &tv_usec_not_expired, EXPIRE_AFTER - 1);
268
269   tv_sec_expired = tv_sec;
270   tv_usec_expired = tv_usec;
271   time_add_milliseconds (&tv_sec_expired,
272                          &tv_usec_expired, EXPIRE_AFTER);
273   
274
275   tv_sec_past = tv_sec - 1;
276   tv_usec_past = tv_usec;
277
278   item = dbus_new0 (TestExpireItem, 1);
279
280   item->item.added_tv_sec = tv_sec;
281   item->item.added_tv_usec = tv_usec;
282   if (!_dbus_list_append (&list->items, item))
283     _dbus_assert_not_reached ("out of memory");
284
285   next_interval =
286     do_expiration_with_current_time (list, tv_sec_not_expired,
287                                      tv_usec_not_expired);
288   _dbus_assert (item->expire_count == 0);
289   _dbus_verbose ("next_interval = %d\n", next_interval);
290   _dbus_assert (next_interval == 1);
291   
292   next_interval =
293     do_expiration_with_current_time (list, tv_sec_expired,
294                                      tv_usec_expired);
295   _dbus_assert (item->expire_count == 1);
296   _dbus_verbose ("next_interval = %d\n", next_interval);
297   _dbus_assert (next_interval == -1);
298
299   next_interval =
300     do_expiration_with_current_time (list, tv_sec_past,
301                                      tv_usec_past);
302   _dbus_assert (item->expire_count == 1);
303   _dbus_verbose ("next_interval = %d\n", next_interval);
304   _dbus_assert (next_interval == 1000 + EXPIRE_AFTER);
305
306   _dbus_list_clear (&list->items);
307   dbus_free (item);
308   
309   bus_expire_list_free (list);
310   _dbus_loop_unref (loop);
311   
312   return TRUE;
313 }
314
315 #endif /* DBUS_BUILD_TESTS */