D-Bus  1.12.16
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 
41 {
42  int refcount;
43  int interval;
46  void *handler_data;
49  void *data;
51  unsigned int enabled : 1;
52  unsigned int needs_restart : 1;
53 };
54 
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 
96 {
97  timeout->refcount += 1;
98 
99  return timeout;
100 }
101 
108 void
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 
137 void
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 
158 void
160 {
161  timeout->enabled = FALSE;
162 }
163 
181 {
187  void *timeout_data;
189 };
190 
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 
214 void
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_foreach (&timeout_list->timeouts,
223  NULL);
224  _dbus_list_clear (&timeout_list->timeouts);
225 
226  dbus_free (timeout_list);
227 }
228 
244  DBusAddTimeoutFunction add_function,
245  DBusRemoveTimeoutFunction remove_function,
246  DBusTimeoutToggledFunction toggled_function,
247  void *data,
248  DBusFreeFunction free_data_function)
249 {
250  /* Add timeouts with the new function, failing on OOM */
251  if (add_function != NULL)
252  {
253  DBusList *link;
254 
255  link = _dbus_list_get_first_link (&timeout_list->timeouts);
256  while (link != NULL)
257  {
258  DBusList *next = _dbus_list_get_next_link (&timeout_list->timeouts,
259  link);
260 
261  if (!(* add_function) (link->data, data))
262  {
263  /* remove it all again and return FALSE */
264  DBusList *link2;
265 
266  link2 = _dbus_list_get_first_link (&timeout_list->timeouts);
267  while (link2 != link)
268  {
269  DBusList *next2 = _dbus_list_get_next_link (&timeout_list->timeouts,
270  link2);
271 
272  (* remove_function) (link2->data, data);
273 
274  link2 = next2;
275  }
276 
277  return FALSE;
278  }
279 
280  link = next;
281  }
282  }
283 
284  /* Remove all current timeouts from previous timeout handlers */
285 
286  if (timeout_list->remove_timeout_function != NULL)
287  {
288  _dbus_list_foreach (&timeout_list->timeouts,
290  timeout_list->timeout_data);
291  }
292 
293  if (timeout_list->timeout_free_data_function != NULL)
294  (* timeout_list->timeout_free_data_function) (timeout_list->timeout_data);
295 
296  timeout_list->add_timeout_function = add_function;
297  timeout_list->remove_timeout_function = remove_function;
298  timeout_list->timeout_toggled_function = toggled_function;
299  timeout_list->timeout_data = data;
300  timeout_list->timeout_free_data_function = free_data_function;
301 
302  return TRUE;
303 }
304 
315  DBusTimeout *timeout)
316 {
317  if (!_dbus_list_append (&timeout_list->timeouts, timeout))
318  return FALSE;
319 
320  _dbus_timeout_ref (timeout);
321 
322  if (timeout_list->add_timeout_function != NULL)
323  {
324  if (!(* timeout_list->add_timeout_function) (timeout,
325  timeout_list->timeout_data))
326  {
327  _dbus_list_remove_last (&timeout_list->timeouts, timeout);
328  _dbus_timeout_unref (timeout);
329  return FALSE;
330  }
331  }
332 
333  return TRUE;
334 }
335 
343 void
345  DBusTimeout *timeout)
346 {
347  if (!_dbus_list_remove (&timeout_list->timeouts, timeout))
348  _dbus_assert_not_reached ("Nonexistent timeout was removed");
349 
350  if (timeout_list->remove_timeout_function != NULL)
351  (* timeout_list->remove_timeout_function) (timeout,
352  timeout_list->timeout_data);
353 
354  _dbus_timeout_unref (timeout);
355 }
356 
365 void
367  DBusTimeout *timeout,
368  dbus_bool_t enabled)
369 {
370  enabled = !!enabled;
371 
372  if (enabled == timeout->enabled)
373  return;
374 
375  timeout->enabled = enabled;
376 
377  if (timeout_list->timeout_toggled_function != NULL)
378  (* timeout_list->timeout_toggled_function) (timeout,
379  timeout_list->timeout_data);
380 }
381 
390 {
391  return timeout->needs_restart;
392 }
393 
400 void
402 {
403  timeout->needs_restart = FALSE;
404 }
405 
443 int
445 {
446  return timeout->interval;
447 }
448 
456 void*
458 {
459  return timeout->data;
460 }
461 
473 void
475  void *data,
476  DBusFreeFunction free_data_function)
477 {
478  if (timeout->free_data_function != NULL)
479  (* timeout->free_data_function) (timeout->data);
480 
481  timeout->data = data;
482  timeout->free_data_function = free_data_function;
483 }
484 
501 {
502  return (* timeout->handler) (timeout->handler_data);
503 }
504 
505 
515 {
516  return timeout->enabled;
517 }
518