D-Bus: dbus-timeout.c Source File

D-Bus 1.14.8

dbus-timeout.c
1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2/* dbus-timeout.c DBusTimeout implementation
3 *
4 * Copyright (C) 2003 CodeFactory AB
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 <config.h>
25#include "dbus-internals.h"
26#include "dbus-timeout.h"
27#include "dbus-list.h"
28
40 struct DBusTimeout
41{
42 int refcount;
43 int interval;
45 DBusTimeoutHandler handler;
46 void *handler_data;
47 DBusFreeFunction free_handler_data_function;
49 void *data;
50 DBusFreeFunction free_data_function;
51 unsigned int enabled : 1;
52 unsigned int needs_restart : 1;
53};
54
63 DBusTimeout*
64_dbus_timeout_new (int interval,
65 DBusTimeoutHandler handler,
66 void *data,
67 DBusFreeFunction free_data_function)
68{
69 DBusTimeout *timeout;
70
71 timeout = dbus_new0 (DBusTimeout, 1);
72 if (timeout == NULL)
73 return NULL;
74
75 timeout->refcount = 1;
76 timeout->interval = interval;
77
78 timeout->handler = handler;
79 timeout->handler_data = data;
80 timeout->free_handler_data_function = free_data_function;
81
82 timeout->enabled = TRUE;
83 timeout->needs_restart = FALSE;
84
85 return timeout;
86}
87
94 DBusTimeout *
95_dbus_timeout_ref (DBusTimeout *timeout)
96{
97 timeout->refcount += 1;
98
99 return timeout;
100}
101
108void
109_dbus_timeout_unref (DBusTimeout *timeout)
110{
111 _dbus_assert (timeout != NULL);
112 _dbus_assert (timeout->refcount > 0);
113
114 timeout->refcount -= 1;
115 if (timeout->refcount == 0)
116 {
117 dbus_timeout_set_data (timeout, NULL, NULL); /* call free_data_function */
118
119 if (timeout->free_handler_data_function)
120 (* timeout->free_handler_data_function) (timeout->handler_data);
121
122 dbus_free (timeout);
123 }
124}
125
137void
138_dbus_timeout_restart (DBusTimeout *timeout,
139 int interval)
140{
141 _dbus_assert (interval >= 0);
142
143 timeout->interval = interval;
144 timeout->enabled = TRUE;
145 timeout->needs_restart = TRUE;
146}
147
158void
159_dbus_timeout_disable (DBusTimeout *timeout)
160{
161 timeout->enabled = FALSE;
162}
163
180 struct DBusTimeoutList
181{
182 DBusList *timeouts;
184 DBusAddTimeoutFunction add_timeout_function;
185 DBusRemoveTimeoutFunction remove_timeout_function;
186 DBusTimeoutToggledFunction timeout_toggled_function;
187 void *timeout_data;
188 DBusFreeFunction timeout_free_data_function;
189};
190
197 DBusTimeoutList*
198_dbus_timeout_list_new (void)
199{
200 DBusTimeoutList *timeout_list;
201
202 timeout_list = dbus_new0 (DBusTimeoutList, 1);
203 if (timeout_list == NULL)
204 return NULL;
205
206 return timeout_list;
207}
208
214void
215_dbus_timeout_list_free (DBusTimeoutList *timeout_list)
216{
217 /* free timeout_data and remove timeouts as a side effect */
218 _dbus_timeout_list_set_functions (timeout_list,
219 NULL, NULL, NULL, NULL, NULL);
220
221 _dbus_list_clear_full (&timeout_list->timeouts,
222 (DBusFreeFunction) _dbus_timeout_unref);
223
224 dbus_free (timeout_list);
225}
226
240dbus_bool_t
241_dbus_timeout_list_set_functions (DBusTimeoutList *timeout_list,
242 DBusAddTimeoutFunction add_function,
243 DBusRemoveTimeoutFunction remove_function,
244 DBusTimeoutToggledFunction toggled_function,
245 void *data,
246 DBusFreeFunction free_data_function)
247{
248 /* Add timeouts with the new function, failing on OOM */
249 if (add_function != NULL)
250 {
251 DBusList *link;
252
253 link = _dbus_list_get_first_link (&timeout_list->timeouts);
254 while (link != NULL)
255 {
256 DBusList *next = _dbus_list_get_next_link (&timeout_list->timeouts,
257 link);
258
259 if (!(* add_function) (link->data, data))
260 {
261 /* remove it all again and return FALSE */
262 DBusList *link2;
263
264 link2 = _dbus_list_get_first_link (&timeout_list->timeouts);
265 while (link2 != link)
266 {
267 DBusList *next2 = _dbus_list_get_next_link (&timeout_list->timeouts,
268 link2);
269
270 (* remove_function) (link2->data, data);
271
272 link2 = next2;
273 }
274
275 return FALSE;
276 }
277
278 link = next;
279 }
280 }
281
282 /* Remove all current timeouts from previous timeout handlers */
283
284 if (timeout_list->remove_timeout_function != NULL)
285 {
286 _dbus_list_foreach (&timeout_list->timeouts,
287 (DBusForeachFunction) timeout_list->remove_timeout_function,
288 timeout_list->timeout_data);
289 }
290
291 if (timeout_list->timeout_free_data_function != NULL)
292 (* timeout_list->timeout_free_data_function) (timeout_list->timeout_data);
293
294 timeout_list->add_timeout_function = add_function;
295 timeout_list->remove_timeout_function = remove_function;
296 timeout_list->timeout_toggled_function = toggled_function;
297 timeout_list->timeout_data = data;
298 timeout_list->timeout_free_data_function = free_data_function;
299
300 return TRUE;
301}
302
311dbus_bool_t
312_dbus_timeout_list_add_timeout (DBusTimeoutList *timeout_list,
313 DBusTimeout *timeout)
314{
315 if (!_dbus_list_append (&timeout_list->timeouts, timeout))
316 return FALSE;
317
318 _dbus_timeout_ref (timeout);
319
320 if (timeout_list->add_timeout_function != NULL)
321 {
322 if (!(* timeout_list->add_timeout_function) (timeout,
323 timeout_list->timeout_data))
324 {
325 _dbus_list_remove_last (&timeout_list->timeouts, timeout);
326 _dbus_timeout_unref (timeout);
327 return FALSE;
328 }
329 }
330
331 return TRUE;
332}
333
341void
342_dbus_timeout_list_remove_timeout (DBusTimeoutList *timeout_list,
343 DBusTimeout *timeout)
344{
345 if (!_dbus_list_remove (&timeout_list->timeouts, timeout))
346 _dbus_assert_not_reached ("Nonexistent timeout was removed");
347
348 if (timeout_list->remove_timeout_function != NULL)
349 (* timeout_list->remove_timeout_function) (timeout,
350 timeout_list->timeout_data);
351
352 _dbus_timeout_unref (timeout);
353}
354
363void
364_dbus_timeout_list_toggle_timeout (DBusTimeoutList *timeout_list,
365 DBusTimeout *timeout,
366 dbus_bool_t enabled)
367{
368 enabled = !!enabled;
369
370 if (enabled == timeout->enabled)
371 return;
372
373 timeout->enabled = enabled;
374
375 if (timeout_list->timeout_toggled_function != NULL)
376 (* timeout_list->timeout_toggled_function) (timeout,
377 timeout_list->timeout_data);
378}
379
386dbus_bool_t
387_dbus_timeout_needs_restart (DBusTimeout *timeout)
388{
389 return timeout->needs_restart;
390}
391
398void
399_dbus_timeout_restarted (DBusTimeout *timeout)
400{
401 timeout->needs_restart = FALSE;
402}
403
441int
442dbus_timeout_get_interval (DBusTimeout *timeout)
443{
444 return timeout->interval;
445}
446
454void*
455dbus_timeout_get_data (DBusTimeout *timeout)
456{
457 return timeout->data;
458}
459
471void
472dbus_timeout_set_data (DBusTimeout *timeout,
473 void *data,
474 DBusFreeFunction free_data_function)
475{
476 if (timeout->free_data_function != NULL)
477 (* timeout->free_data_function) (timeout->data);
478
479 timeout->data = data;
480 timeout->free_data_function = free_data_function;
481}
482
497dbus_bool_t
498dbus_timeout_handle (DBusTimeout *timeout)
499{
500 return (* timeout->handler) (timeout->handler_data);
501}
502
503
511dbus_bool_t
512dbus_timeout_get_enabled (DBusTimeout *timeout)
513{
514 return timeout->enabled;
515}
516
DBusTimeoutToggledFunction
void(* DBusTimeoutToggledFunction)(DBusTimeout *timeout, void *data)
Called when dbus_timeout_get_enabled() may return a different value than it did before.
Definition dbus-connection.h:118
DBusAddTimeoutFunction
dbus_bool_t(* DBusAddTimeoutFunction)(DBusTimeout *timeout, void *data)
Called when libdbus needs a new timeout to be monitored by the main loop.
Definition dbus-connection.h:111
DBusRemoveTimeoutFunction
void(* DBusRemoveTimeoutFunction)(DBusTimeout *timeout, void *data)
Called when libdbus no longer needs a timeout to be monitored by the main loop.
Definition dbus-connection.h:124
_dbus_assert_not_reached
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
Definition dbus-internals.h:164
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition dbus-internals.h:153
_dbus_list_get_first_link
DBusList * _dbus_list_get_first_link(DBusList **list)
Gets the first link in the list.
Definition dbus-list.c:595
_dbus_list_remove
dbus_bool_t _dbus_list_remove(DBusList **list, void *data)
Removes a value from the list.
Definition dbus-list.c:416
_dbus_list_clear_full
void _dbus_list_clear_full(DBusList **list, DBusFreeFunction function)
Free every link and every element in the list.
Definition dbus-list.c:568
_dbus_list_foreach
void _dbus_list_foreach(DBusList **list, DBusForeachFunction function, void *data)
Calls the given function for each element in the list.
Definition dbus-list.c:787
_dbus_list_remove_last
dbus_bool_t _dbus_list_remove_last(DBusList **list, void *data)
Removes a value from the list.
Definition dbus-list.c:447
_dbus_list_append
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
Definition dbus-list.c:271
_dbus_list_get_next_link
#define _dbus_list_get_next_link(list, link)
Gets the next link in the list, or NULL if there are no more links.
Definition dbus-list.h:119
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:49
TRUE
#define TRUE
Expands to "1".
Definition dbus-macros.h:39
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:42
DBusFreeFunction
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
Definition dbus-memory.h:63
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition dbus-memory.c:692
dbus_new0
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition dbus-memory.h:58
_dbus_timeout_list_add_timeout
dbus_bool_t _dbus_timeout_list_add_timeout(DBusTimeoutList *timeout_list, DBusTimeout *timeout)
Adds a new timeout to the timeout list, invoking the application DBusAddTimeoutFunction if appropriat...
Definition dbus-timeout.c:312
_dbus_timeout_list_free
void _dbus_timeout_list_free(DBusTimeoutList *timeout_list)
Frees a DBusTimeoutList.
Definition dbus-timeout.c:215
_dbus_timeout_list_toggle_timeout
void _dbus_timeout_list_toggle_timeout(DBusTimeoutList *timeout_list, DBusTimeout *timeout, dbus_bool_t enabled)
Sets a timeout to the given enabled state, invoking the application's DBusTimeoutToggledFunction if a...
Definition dbus-timeout.c:364
_dbus_timeout_list_new
DBusTimeoutList * _dbus_timeout_list_new(void)
Creates a new timeout list.
Definition dbus-timeout.c:198
_dbus_timeout_unref
void _dbus_timeout_unref(DBusTimeout *timeout)
Decrements the reference count of a DBusTimeout object and finalizes the object if the count reaches ...
Definition dbus-timeout.c:109
_dbus_timeout_list_set_functions
dbus_bool_t _dbus_timeout_list_set_functions(DBusTimeoutList *timeout_list, DBusAddTimeoutFunction add_function, DBusRemoveTimeoutFunction remove_function, DBusTimeoutToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
Sets the timeout functions.
Definition dbus-timeout.c:241
_dbus_timeout_disable
void _dbus_timeout_disable(DBusTimeout *timeout)
Disable the timeout.
Definition dbus-timeout.c:159
_dbus_timeout_new
DBusTimeout * _dbus_timeout_new(int interval, DBusTimeoutHandler handler, void *data, DBusFreeFunction free_data_function)
Creates a new DBusTimeout, enabled by default.
Definition dbus-timeout.c:64
_dbus_timeout_restart
void _dbus_timeout_restart(DBusTimeout *timeout, int interval)
Change the timeout interval to be interval milliseconds from now (forgetting when the timeout was ini...
Definition dbus-timeout.c:138
DBusTimeoutHandler
dbus_bool_t(* DBusTimeoutHandler)(void *data)
function to run when the timeout is handled
Definition dbus-timeout.h:41
_dbus_timeout_ref
DBusTimeout * _dbus_timeout_ref(DBusTimeout *timeout)
Increments the reference count of a DBusTimeout object.
Definition dbus-timeout.c:95
_dbus_timeout_restarted
void _dbus_timeout_restarted(DBusTimeout *timeout)
Mark timeout as restarted (setting timestamps is responsibility of the event loop).
Definition dbus-timeout.c:399
_dbus_timeout_list_remove_timeout
void _dbus_timeout_list_remove_timeout(DBusTimeoutList *timeout_list, DBusTimeout *timeout)
Removes a timeout from the timeout list, invoking the application's DBusRemoveTimeoutFunction if appr...
Definition dbus-timeout.c:342
_dbus_timeout_needs_restart
dbus_bool_t _dbus_timeout_needs_restart(DBusTimeout *timeout)
Returns whether a timeout needs restart time counting in the event loop.
Definition dbus-timeout.c:387
dbus_timeout_handle
DBUS_EXPORT dbus_bool_t dbus_timeout_handle(DBusTimeout *timeout)
Calls the timeout handler for this timeout.
Definition dbus-timeout.c:498
dbus_timeout_get_enabled
DBUS_EXPORT dbus_bool_t dbus_timeout_get_enabled(DBusTimeout *timeout)
Returns whether a timeout is enabled or not.
Definition dbus-timeout.c:512
dbus_timeout_get_interval
DBUS_EXPORT int dbus_timeout_get_interval(DBusTimeout *timeout)
Gets the timeout interval.
Definition dbus-timeout.c:442
dbus_timeout_get_data
DBUS_EXPORT void * dbus_timeout_get_data(DBusTimeout *timeout)
Gets data previously set with dbus_timeout_set_data() or NULL if none.
Definition dbus-timeout.c:455
dbus_timeout_set_data
DBUS_EXPORT void dbus_timeout_set_data(DBusTimeout *timeout, void *data, DBusFreeFunction free_data_function)
Sets data which can be retrieved with dbus_timeout_get_data().
Definition dbus-timeout.c:472
DBusList
A node in a linked list.
Definition dbus-list.h:35
DBusList::data
void * data
Data stored at this element.
Definition dbus-list.h:38
DBusTimeoutList
DBusTimeoutList implementation details.
Definition dbus-timeout.c:181
DBusTimeoutList::timeout_toggled_function
DBusTimeoutToggledFunction timeout_toggled_function
Callback when timeout is enabled/disabled or changes interval.
Definition dbus-timeout.c:186
DBusTimeoutList::remove_timeout_function
DBusRemoveTimeoutFunction remove_timeout_function
Callback for removing a timeout.
Definition dbus-timeout.c:185
DBusTimeoutList::timeouts
DBusList * timeouts
Timeout objects.
Definition dbus-timeout.c:182
DBusTimeoutList::add_timeout_function
DBusAddTimeoutFunction add_timeout_function
Callback for adding a timeout.
Definition dbus-timeout.c:184
DBusTimeoutList::timeout_free_data_function
DBusFreeFunction timeout_free_data_function
Free function for timeout callback data.
Definition dbus-timeout.c:188
DBusTimeoutList::timeout_data
void * timeout_data
Data for timeout callbacks.
Definition dbus-timeout.c:187
DBusTimeout
Internals of DBusTimeout.
Definition dbus-timeout.c:41
DBusTimeout::data
void * data
Application data.
Definition dbus-timeout.c:49
DBusTimeout::refcount
int refcount
Reference count.
Definition dbus-timeout.c:42
DBusTimeout::enabled
unsigned int enabled
True if timeout is active.
Definition dbus-timeout.c:51
DBusTimeout::handler
DBusTimeoutHandler handler
Timeout handler.
Definition dbus-timeout.c:45
DBusTimeout::handler_data
void * handler_data
Timeout handler data.
Definition dbus-timeout.c:46
DBusTimeout::interval
int interval
Timeout interval in milliseconds.
Definition dbus-timeout.c:43
DBusTimeout::needs_restart
unsigned int needs_restart
Flag that timeout should be restarted after re-enabling.
Definition dbus-timeout.c:52
DBusTimeout::free_handler_data_function
DBusFreeFunction free_handler_data_function
Free the timeout handler data.
Definition dbus-timeout.c:47
DBusTimeout::free_data_function
DBusFreeFunction free_data_function
Free the application data.
Definition dbus-timeout.c:50