sl@0
|
1 |
/* -*- mode: C; c-file-style: "gnu" -*- */
|
sl@0
|
2 |
/* expirelist.c List of items that expire
|
sl@0
|
3 |
*
|
sl@0
|
4 |
* Copyright (C) 2003 Red Hat, Inc.
|
sl@0
|
5 |
* Portion Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
|
sl@0
|
6 |
* Licensed under the Academic Free License version 2.1
|
sl@0
|
7 |
*
|
sl@0
|
8 |
* This program is free software; you can redistribute it and/or modify
|
sl@0
|
9 |
* it under the terms of the GNU General Public License as published by
|
sl@0
|
10 |
* the Free Software Foundation; either version 2 of the License, or
|
sl@0
|
11 |
* (at your option) any later version.
|
sl@0
|
12 |
*
|
sl@0
|
13 |
* This program is distributed in the hope that it will be useful,
|
sl@0
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
sl@0
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
sl@0
|
16 |
* GNU General Public License for more details.
|
sl@0
|
17 |
*
|
sl@0
|
18 |
* You should have received a copy of the GNU General Public License
|
sl@0
|
19 |
* along with this program; if not, write to the Free Software
|
sl@0
|
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
sl@0
|
21 |
*
|
sl@0
|
22 |
*/
|
sl@0
|
23 |
|
sl@0
|
24 |
#include "expirelist.h"
|
sl@0
|
25 |
#include "test.h"
|
sl@0
|
26 |
#ifndef __SYMBIAN32__
|
sl@0
|
27 |
#include <dbus/dbus-internals.h>
|
sl@0
|
28 |
#include <dbus/dbus-mainloop.h>
|
sl@0
|
29 |
#include <dbus/dbus-timeout.h>
|
sl@0
|
30 |
#else
|
sl@0
|
31 |
#include "dbus-internals.h"
|
sl@0
|
32 |
#include "dbus-mainloop.h"
|
sl@0
|
33 |
#include "dbus-timeout.h"
|
sl@0
|
34 |
#endif //__SYMBIAN32__
|
sl@0
|
35 |
|
sl@0
|
36 |
static dbus_bool_t expire_timeout_handler (void *data);
|
sl@0
|
37 |
|
sl@0
|
38 |
static void
|
sl@0
|
39 |
call_timeout_callback (DBusTimeout *timeout,
|
sl@0
|
40 |
void *data)
|
sl@0
|
41 |
{
|
sl@0
|
42 |
/* can return FALSE on OOM but we just let it fire again later */
|
sl@0
|
43 |
dbus_timeout_handle (timeout);
|
sl@0
|
44 |
}
|
sl@0
|
45 |
|
sl@0
|
46 |
BusExpireList*
|
sl@0
|
47 |
bus_expire_list_new (DBusLoop *loop,
|
sl@0
|
48 |
int expire_after,
|
sl@0
|
49 |
BusExpireFunc expire_func,
|
sl@0
|
50 |
void *data)
|
sl@0
|
51 |
{
|
sl@0
|
52 |
BusExpireList *list;
|
sl@0
|
53 |
|
sl@0
|
54 |
list = dbus_new0 (BusExpireList, 1);
|
sl@0
|
55 |
if (list == NULL)
|
sl@0
|
56 |
return NULL;
|
sl@0
|
57 |
|
sl@0
|
58 |
list->expire_func = expire_func;
|
sl@0
|
59 |
list->data = data;
|
sl@0
|
60 |
list->loop = loop;
|
sl@0
|
61 |
list->expire_after = expire_after;
|
sl@0
|
62 |
|
sl@0
|
63 |
list->timeout = _dbus_timeout_new (100, /* irrelevant */
|
sl@0
|
64 |
expire_timeout_handler,
|
sl@0
|
65 |
list, NULL);
|
sl@0
|
66 |
if (list->timeout == NULL)
|
sl@0
|
67 |
goto failed;
|
sl@0
|
68 |
|
sl@0
|
69 |
_dbus_timeout_set_enabled (list->timeout, FALSE);
|
sl@0
|
70 |
|
sl@0
|
71 |
if (!_dbus_loop_add_timeout (list->loop,
|
sl@0
|
72 |
list->timeout,
|
sl@0
|
73 |
call_timeout_callback, NULL, NULL))
|
sl@0
|
74 |
goto failed;
|
sl@0
|
75 |
|
sl@0
|
76 |
return list;
|
sl@0
|
77 |
|
sl@0
|
78 |
failed:
|
sl@0
|
79 |
if (list->timeout)
|
sl@0
|
80 |
_dbus_timeout_unref (list->timeout);
|
sl@0
|
81 |
|
sl@0
|
82 |
dbus_free (list);
|
sl@0
|
83 |
|
sl@0
|
84 |
return NULL;
|
sl@0
|
85 |
}
|
sl@0
|
86 |
|
sl@0
|
87 |
void
|
sl@0
|
88 |
bus_expire_list_free (BusExpireList *list)
|
sl@0
|
89 |
{
|
sl@0
|
90 |
_dbus_assert (list->items == NULL);
|
sl@0
|
91 |
|
sl@0
|
92 |
_dbus_loop_remove_timeout (list->loop, list->timeout,
|
sl@0
|
93 |
call_timeout_callback, NULL);
|
sl@0
|
94 |
|
sl@0
|
95 |
_dbus_timeout_unref (list->timeout);
|
sl@0
|
96 |
|
sl@0
|
97 |
dbus_free (list);
|
sl@0
|
98 |
}
|
sl@0
|
99 |
|
sl@0
|
100 |
void
|
sl@0
|
101 |
bus_expire_timeout_set_interval (DBusTimeout *timeout,
|
sl@0
|
102 |
int next_interval)
|
sl@0
|
103 |
{
|
sl@0
|
104 |
if (next_interval >= 0)
|
sl@0
|
105 |
{
|
sl@0
|
106 |
_dbus_timeout_set_interval (timeout,
|
sl@0
|
107 |
next_interval);
|
sl@0
|
108 |
_dbus_timeout_set_enabled (timeout, TRUE);
|
sl@0
|
109 |
|
sl@0
|
110 |
_dbus_verbose ("Enabled expire timeout with interval %d\n",
|
sl@0
|
111 |
next_interval);
|
sl@0
|
112 |
}
|
sl@0
|
113 |
else if (dbus_timeout_get_enabled (timeout))
|
sl@0
|
114 |
{
|
sl@0
|
115 |
_dbus_timeout_set_enabled (timeout, FALSE);
|
sl@0
|
116 |
|
sl@0
|
117 |
_dbus_verbose ("Disabled expire timeout\n");
|
sl@0
|
118 |
}
|
sl@0
|
119 |
else
|
sl@0
|
120 |
_dbus_verbose ("No need to disable expire timeout\n");
|
sl@0
|
121 |
}
|
sl@0
|
122 |
|
sl@0
|
123 |
static int
|
sl@0
|
124 |
do_expiration_with_current_time (BusExpireList *list,
|
sl@0
|
125 |
long tv_sec,
|
sl@0
|
126 |
long tv_usec)
|
sl@0
|
127 |
{
|
sl@0
|
128 |
DBusList *link;
|
sl@0
|
129 |
int next_interval;
|
sl@0
|
130 |
|
sl@0
|
131 |
next_interval = -1;
|
sl@0
|
132 |
|
sl@0
|
133 |
link = _dbus_list_get_first_link (&list->items);
|
sl@0
|
134 |
while (link != NULL)
|
sl@0
|
135 |
{
|
sl@0
|
136 |
DBusList *next = _dbus_list_get_next_link (&list->items, link);
|
sl@0
|
137 |
double elapsed;
|
sl@0
|
138 |
BusExpireItem *item;
|
sl@0
|
139 |
|
sl@0
|
140 |
item = link->data;
|
sl@0
|
141 |
|
sl@0
|
142 |
elapsed = ELAPSED_MILLISECONDS_SINCE (item->added_tv_sec,
|
sl@0
|
143 |
item->added_tv_usec,
|
sl@0
|
144 |
tv_sec, tv_usec);
|
sl@0
|
145 |
|
sl@0
|
146 |
if (elapsed >= (double) list->expire_after)
|
sl@0
|
147 |
{
|
sl@0
|
148 |
_dbus_verbose ("Expiring an item %p\n", item);
|
sl@0
|
149 |
|
sl@0
|
150 |
/* If the expire function fails, we just end up expiring
|
sl@0
|
151 |
* this item next time we walk through the list. This would
|
sl@0
|
152 |
* be an indeterminate time normally, so we set up the
|
sl@0
|
153 |
* next_interval to be "shortly" (just enough to avoid
|
sl@0
|
154 |
* a busy loop)
|
sl@0
|
155 |
*/
|
sl@0
|
156 |
if (!(* list->expire_func) (list, link, list->data))
|
sl@0
|
157 |
{
|
sl@0
|
158 |
next_interval = _dbus_get_oom_wait ();
|
sl@0
|
159 |
break;
|
sl@0
|
160 |
}
|
sl@0
|
161 |
}
|
sl@0
|
162 |
else
|
sl@0
|
163 |
{
|
sl@0
|
164 |
/* We can end the loop, since the connections are in oldest-first order */
|
sl@0
|
165 |
next_interval = ((double)list->expire_after) - elapsed;
|
sl@0
|
166 |
_dbus_verbose ("Item %p expires in %d milliseconds\n",
|
sl@0
|
167 |
item, next_interval);
|
sl@0
|
168 |
|
sl@0
|
169 |
break;
|
sl@0
|
170 |
}
|
sl@0
|
171 |
|
sl@0
|
172 |
link = next;
|
sl@0
|
173 |
}
|
sl@0
|
174 |
|
sl@0
|
175 |
return next_interval;
|
sl@0
|
176 |
}
|
sl@0
|
177 |
|
sl@0
|
178 |
static void
|
sl@0
|
179 |
bus_expirelist_expire (BusExpireList *list)
|
sl@0
|
180 |
{
|
sl@0
|
181 |
int next_interval;
|
sl@0
|
182 |
|
sl@0
|
183 |
next_interval = -1;
|
sl@0
|
184 |
|
sl@0
|
185 |
if (list->items != NULL)
|
sl@0
|
186 |
{
|
sl@0
|
187 |
long tv_sec, tv_usec;
|
sl@0
|
188 |
|
sl@0
|
189 |
_dbus_get_current_time (&tv_sec, &tv_usec);
|
sl@0
|
190 |
|
sl@0
|
191 |
next_interval = do_expiration_with_current_time (list, tv_sec, tv_usec);
|
sl@0
|
192 |
}
|
sl@0
|
193 |
|
sl@0
|
194 |
bus_expire_timeout_set_interval (list->timeout, next_interval);
|
sl@0
|
195 |
}
|
sl@0
|
196 |
|
sl@0
|
197 |
static dbus_bool_t
|
sl@0
|
198 |
expire_timeout_handler (void *data)
|
sl@0
|
199 |
{
|
sl@0
|
200 |
BusExpireList *list = data;
|
sl@0
|
201 |
|
sl@0
|
202 |
_dbus_verbose ("Running %s\n", _DBUS_FUNCTION_NAME);
|
sl@0
|
203 |
|
sl@0
|
204 |
/* note that this may remove the timeout */
|
sl@0
|
205 |
bus_expirelist_expire (list);
|
sl@0
|
206 |
|
sl@0
|
207 |
return TRUE;
|
sl@0
|
208 |
}
|
sl@0
|
209 |
|
sl@0
|
210 |
#ifdef DBUS_BUILD_TESTS
|
sl@0
|
211 |
|
sl@0
|
212 |
typedef struct
|
sl@0
|
213 |
{
|
sl@0
|
214 |
BusExpireItem item;
|
sl@0
|
215 |
int expire_count;
|
sl@0
|
216 |
} TestExpireItem;
|
sl@0
|
217 |
|
sl@0
|
218 |
static dbus_bool_t
|
sl@0
|
219 |
test_expire_func (BusExpireList *list,
|
sl@0
|
220 |
DBusList *link,
|
sl@0
|
221 |
void *data)
|
sl@0
|
222 |
{
|
sl@0
|
223 |
TestExpireItem *t;
|
sl@0
|
224 |
|
sl@0
|
225 |
t = (TestExpireItem*) link->data;
|
sl@0
|
226 |
|
sl@0
|
227 |
t->expire_count += 1;
|
sl@0
|
228 |
|
sl@0
|
229 |
return TRUE;
|
sl@0
|
230 |
}
|
sl@0
|
231 |
|
sl@0
|
232 |
static void
|
sl@0
|
233 |
time_add_milliseconds (long *tv_sec,
|
sl@0
|
234 |
long *tv_usec,
|
sl@0
|
235 |
int milliseconds)
|
sl@0
|
236 |
{
|
sl@0
|
237 |
*tv_sec = *tv_sec + milliseconds / 1000;
|
sl@0
|
238 |
*tv_usec = *tv_usec + milliseconds * 1000;
|
sl@0
|
239 |
if (*tv_usec >= 1000000)
|
sl@0
|
240 |
{
|
sl@0
|
241 |
*tv_usec -= 1000000;
|
sl@0
|
242 |
*tv_sec += 1;
|
sl@0
|
243 |
}
|
sl@0
|
244 |
}
|
sl@0
|
245 |
|
sl@0
|
246 |
dbus_bool_t
|
sl@0
|
247 |
bus_expire_list_test (const DBusString *test_data_dir)
|
sl@0
|
248 |
{
|
sl@0
|
249 |
DBusLoop *loop;
|
sl@0
|
250 |
BusExpireList *list;
|
sl@0
|
251 |
long tv_sec, tv_usec;
|
sl@0
|
252 |
long tv_sec_not_expired, tv_usec_not_expired;
|
sl@0
|
253 |
long tv_sec_expired, tv_usec_expired;
|
sl@0
|
254 |
long tv_sec_past, tv_usec_past;
|
sl@0
|
255 |
TestExpireItem *item;
|
sl@0
|
256 |
int next_interval;
|
sl@0
|
257 |
dbus_bool_t result = FALSE;
|
sl@0
|
258 |
|
sl@0
|
259 |
|
sl@0
|
260 |
loop = _dbus_loop_new ();
|
sl@0
|
261 |
_dbus_assert (loop != NULL);
|
sl@0
|
262 |
|
sl@0
|
263 |
#define EXPIRE_AFTER 100
|
sl@0
|
264 |
|
sl@0
|
265 |
list = bus_expire_list_new (loop, EXPIRE_AFTER,
|
sl@0
|
266 |
test_expire_func, NULL);
|
sl@0
|
267 |
_dbus_assert (list != NULL);
|
sl@0
|
268 |
|
sl@0
|
269 |
_dbus_get_current_time (&tv_sec, &tv_usec);
|
sl@0
|
270 |
|
sl@0
|
271 |
tv_sec_not_expired = tv_sec;
|
sl@0
|
272 |
tv_usec_not_expired = tv_usec;
|
sl@0
|
273 |
time_add_milliseconds (&tv_sec_not_expired,
|
sl@0
|
274 |
&tv_usec_not_expired, EXPIRE_AFTER - 1);
|
sl@0
|
275 |
|
sl@0
|
276 |
tv_sec_expired = tv_sec;
|
sl@0
|
277 |
tv_usec_expired = tv_usec;
|
sl@0
|
278 |
time_add_milliseconds (&tv_sec_expired,
|
sl@0
|
279 |
&tv_usec_expired, EXPIRE_AFTER);
|
sl@0
|
280 |
|
sl@0
|
281 |
|
sl@0
|
282 |
tv_sec_past = tv_sec - 1;
|
sl@0
|
283 |
tv_usec_past = tv_usec;
|
sl@0
|
284 |
|
sl@0
|
285 |
item = dbus_new0 (TestExpireItem, 1);
|
sl@0
|
286 |
|
sl@0
|
287 |
if (item == NULL)
|
sl@0
|
288 |
goto oom;
|
sl@0
|
289 |
|
sl@0
|
290 |
item->item.added_tv_sec = tv_sec;
|
sl@0
|
291 |
item->item.added_tv_usec = tv_usec;
|
sl@0
|
292 |
if (!_dbus_list_append (&list->items, item))
|
sl@0
|
293 |
_dbus_assert_not_reached ("out of memory");
|
sl@0
|
294 |
|
sl@0
|
295 |
next_interval =
|
sl@0
|
296 |
do_expiration_with_current_time (list, tv_sec_not_expired,
|
sl@0
|
297 |
tv_usec_not_expired);
|
sl@0
|
298 |
_dbus_assert (item->expire_count == 0);
|
sl@0
|
299 |
_dbus_verbose ("next_interval = %d\n", next_interval);
|
sl@0
|
300 |
_dbus_assert (next_interval == 1);
|
sl@0
|
301 |
|
sl@0
|
302 |
next_interval =
|
sl@0
|
303 |
do_expiration_with_current_time (list, tv_sec_expired,
|
sl@0
|
304 |
tv_usec_expired);
|
sl@0
|
305 |
_dbus_assert (item->expire_count == 1);
|
sl@0
|
306 |
_dbus_verbose ("next_interval = %d\n", next_interval);
|
sl@0
|
307 |
_dbus_assert (next_interval == -1);
|
sl@0
|
308 |
|
sl@0
|
309 |
next_interval =
|
sl@0
|
310 |
do_expiration_with_current_time (list, tv_sec_past,
|
sl@0
|
311 |
tv_usec_past);
|
sl@0
|
312 |
_dbus_assert (item->expire_count == 1);
|
sl@0
|
313 |
_dbus_verbose ("next_interval = %d\n", next_interval);
|
sl@0
|
314 |
_dbus_assert (next_interval == 1000 + EXPIRE_AFTER);
|
sl@0
|
315 |
|
sl@0
|
316 |
_dbus_list_clear (&list->items);
|
sl@0
|
317 |
dbus_free (item);
|
sl@0
|
318 |
|
sl@0
|
319 |
bus_expire_list_free (list);
|
sl@0
|
320 |
_dbus_loop_unref (loop);
|
sl@0
|
321 |
|
sl@0
|
322 |
result = TRUE;
|
sl@0
|
323 |
|
sl@0
|
324 |
oom:
|
sl@0
|
325 |
return result;
|
sl@0
|
326 |
}
|
sl@0
|
327 |
|
sl@0
|
328 |
#endif /* DBUS_BUILD_TESTS */
|