D-Bus  1.12.16
dbus-pending-call.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-pending-call.c Object representing a call in progress.
3  *
4  * Copyright (C) 2002, 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., 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-connection-internal.h"
27 #include "dbus-message-internal.h"
28 #include "dbus-pending-call-internal.h"
29 #include "dbus-pending-call.h"
30 #include "dbus-list.h"
31 #include "dbus-threads.h"
32 #include "dbus-test.h"
33 
53 #define CONNECTION_LOCK(connection) _dbus_connection_lock(connection)
54 
57 #define CONNECTION_UNLOCK(connection) _dbus_connection_unlock(connection)
58 
63 {
83  unsigned int completed : 1;
87  unsigned int timeout_added : 1;
88 };
89 
90 static void
91 _dbus_pending_call_trace_ref (DBusPendingCall *pending_call,
92  int old_refcount,
93  int new_refcount,
94  const char *why)
95 {
96 #ifdef DBUS_ENABLE_VERBOSE_MODE
97  static int enabled = -1;
98 
99  _dbus_trace_ref ("DBusPendingCall", pending_call, old_refcount,
100  new_refcount, why, "DBUS_PENDING_CALL_TRACE", &enabled);
101 #endif
102 }
103 
104 static dbus_int32_t notify_user_data_slot = -1;
105 
118  int timeout_milliseconds,
119  DBusTimeoutHandler timeout_handler)
120 {
121  DBusPendingCall *pending;
122  DBusTimeout *timeout;
123 
124  _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
125 
126  if (timeout_milliseconds == -1)
127  timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
128 
129  if (!dbus_pending_call_allocate_data_slot (&notify_user_data_slot))
130  return NULL;
131 
132  pending = dbus_new0 (DBusPendingCall, 1);
133 
134  if (pending == NULL)
135  {
136  dbus_pending_call_free_data_slot (&notify_user_data_slot);
137  return NULL;
138  }
139 
140  if (timeout_milliseconds != DBUS_TIMEOUT_INFINITE)
141  {
142  timeout = _dbus_timeout_new (timeout_milliseconds,
143  timeout_handler,
144  pending, NULL);
145 
146  if (timeout == NULL)
147  {
148  dbus_pending_call_free_data_slot (&notify_user_data_slot);
149  dbus_free (pending);
150  return NULL;
151  }
152 
153  pending->timeout = timeout;
154  }
155  else
156  {
157  pending->timeout = NULL;
158  }
159 
160  _dbus_atomic_inc (&pending->refcount);
161  pending->connection = connection;
163 
165 
166  _dbus_pending_call_trace_ref (pending, 0, 1, "new_unlocked");
167 
168  return pending;
169 }
170 
179 void
181  DBusMessage *message)
182 {
183  if (message == NULL)
184  {
185  message = pending->timeout_link->data;
186  _dbus_list_clear (&pending->timeout_link);
187  }
188  else
189  dbus_message_ref (message);
190 
191  _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
192  message,
194  "method return" :
196  "error" : "other type",
197  pending->reply_serial);
198 
199  _dbus_assert (pending->reply == NULL);
201  pending->reply = message;
202 }
203 
214 void
216 {
217  _dbus_assert (!pending->completed);
218 
219  pending->completed = TRUE;
220 }
221 
231 void
233 {
234  _dbus_assert (pending->completed);
235 
236  if (pending->function)
237  {
238  void *user_data;
239  user_data = dbus_pending_call_get_data (pending,
240  notify_user_data_slot);
241 
242  (* pending->function) (pending, user_data);
243  }
244 }
245 
253 void
255  DBusConnection *connection)
256 {
257  _dbus_assert (connection == pending->connection);
258 
259  if (pending->timeout_link)
260  {
262  pending->timeout_link);
263  pending->timeout_link = NULL;
264  }
265 }
266 
275 {
276  _dbus_assert (pending != NULL);
277 
278  return pending->timeout_added;
279 }
280 
281 
288 void
290  dbus_bool_t is_added)
291 {
292  _dbus_assert (pending != NULL);
293 
294  pending->timeout_added = is_added;
295 }
296 
297 
304 DBusTimeout *
306 {
307  _dbus_assert (pending != NULL);
308 
309  return pending->timeout;
310 }
311 
320 {
321  _dbus_assert (pending != NULL);
322 
323  return pending->reply_serial;
324 }
325 
332 void
334  dbus_uint32_t serial)
335 {
336  _dbus_assert (pending != NULL);
337  _dbus_assert (pending->reply_serial == 0);
338 
339  pending->reply_serial = serial;
340 }
341 
350 {
351  _dbus_assert (pending != NULL);
352 
353  CONNECTION_LOCK (pending->connection);
354  return pending->connection;
355 }
356 
365 {
366  _dbus_assert (pending != NULL);
367 
368  return pending->connection;
369 }
370 
381  DBusMessage *message,
382  dbus_uint32_t serial)
383 {
384  DBusList *reply_link;
385  DBusMessage *reply;
386 
388  "Did not receive a reply. Possible causes include: "
389  "the remote application did not send a reply, "
390  "the message bus security policy blocked the reply, "
391  "the reply timeout expired, or "
392  "the network connection was broken.");
393  if (reply == NULL)
394  return FALSE;
395 
396  reply_link = _dbus_list_alloc_link (reply);
397  if (reply_link == NULL)
398  {
399  /* it's OK to unref this, nothing that could have attached a callback
400  * has ever seen it */
401  dbus_message_unref (reply);
402  return FALSE;
403  }
404 
405  pending->timeout_link = reply_link;
406 
408 
409  return TRUE;
410 }
411 
421 {
422  dbus_int32_t old_refcount;
423 
424  old_refcount = _dbus_atomic_inc (&pending->refcount);
425  _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
426  "ref_unlocked");
427 
428  return pending;
429 }
430 
431 
432 static void
433 _dbus_pending_call_last_unref (DBusPendingCall *pending)
434 {
435  DBusConnection *connection;
436 
437  /* If we get here, we should be already detached
438  * from the connection, or never attached.
439  */
440  _dbus_assert (!pending->timeout_added);
441 
442  connection = pending->connection;
443 
444  /* this assumes we aren't holding connection lock... */
446 
447  if (pending->timeout != NULL)
448  _dbus_timeout_unref (pending->timeout);
449 
450  if (pending->timeout_link)
451  {
454  pending->timeout_link = NULL;
455  }
456 
457  if (pending->reply)
458  {
459  dbus_message_unref (pending->reply);
460  pending->reply = NULL;
461  }
462 
463  dbus_free (pending);
464 
465  dbus_pending_call_free_data_slot (&notify_user_data_slot);
466 
467  /* connection lock should not be held. */
468  /* Free the connection last to avoid a weird state while
469  * calling out to application code where the pending exists
470  * but not the connection.
471  */
472  dbus_connection_unref (connection);
473 }
474 
482 void
484 {
485  dbus_int32_t old_refcount;
486 
487  old_refcount = _dbus_atomic_dec (&pending->refcount);
488  _dbus_assert (old_refcount > 0);
489  _dbus_pending_call_trace_ref (pending, old_refcount,
490  old_refcount - 1, "unref_and_unlock");
491 
492  CONNECTION_UNLOCK (pending->connection);
493 
494  if (old_refcount == 1)
495  _dbus_pending_call_last_unref (pending);
496 }
497 
507 {
508  return pending->completed;
509 }
510 
511 static DBusDataSlotAllocator slot_allocator =
512  _DBUS_DATA_SLOT_ALLOCATOR_INIT (_DBUS_LOCK_NAME (pending_call_slots));
513 
529  dbus_int32_t slot,
530  void *data,
531  DBusFreeFunction free_data_func)
532 {
533  DBusFreeFunction old_free_func;
534  void *old_data;
535  dbus_bool_t retval;
536 
537  retval = _dbus_data_slot_list_set (&slot_allocator,
538  &pending->slot_list,
539  slot, data, free_data_func,
540  &old_free_func, &old_data);
541 
542  /* Drop locks to call out to app code */
543  CONNECTION_UNLOCK (pending->connection);
544 
545  if (retval)
546  {
547  if (old_free_func)
548  (* old_free_func) (old_data);
549  }
550 
551  CONNECTION_LOCK (pending->connection);
552 
553  return retval;
554 }
555 
604 {
605  dbus_int32_t old_refcount;
606 
607  _dbus_return_val_if_fail (pending != NULL, NULL);
608 
609  old_refcount = _dbus_atomic_inc (&pending->refcount);
610  _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
611  "ref");
612 
613  return pending;
614 }
615 
622 void
624 {
625  dbus_int32_t old_refcount;
626 
627  _dbus_return_if_fail (pending != NULL);
628 
629  old_refcount = _dbus_atomic_dec (&pending->refcount);
630  _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount - 1,
631  "unref");
632 
633  if (old_refcount == 1)
634  _dbus_pending_call_last_unref(pending);
635 }
636 
650  void *user_data,
651  DBusFreeFunction free_user_data)
652 {
653  dbus_bool_t ret = FALSE;
654 
655  _dbus_return_val_if_fail (pending != NULL, FALSE);
656 
657  CONNECTION_LOCK (pending->connection);
658 
659  /* could invoke application code! */
660  if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
661  user_data, free_user_data))
662  goto out;
663 
664  pending->function = function;
665  ret = TRUE;
666 
667 out:
668  CONNECTION_UNLOCK (pending->connection);
669 
670  return ret;
671 }
672 
688 void
690 {
691  _dbus_return_if_fail (pending != NULL);
692 
694  pending);
695 }
696 
706 {
707  dbus_bool_t completed;
708 
709  _dbus_return_val_if_fail (pending != NULL, FALSE);
710 
711  CONNECTION_LOCK (pending->connection);
712  completed = pending->completed;
713  CONNECTION_UNLOCK (pending->connection);
714 
715  return completed;
716 }
717 
729 {
730  DBusMessage *message;
731 
732  _dbus_return_val_if_fail (pending != NULL, NULL);
733  _dbus_return_val_if_fail (pending->completed, NULL);
734  _dbus_return_val_if_fail (pending->reply != NULL, NULL);
735 
736  CONNECTION_LOCK (pending->connection);
737 
738  message = pending->reply;
739  pending->reply = NULL;
740 
741  CONNECTION_UNLOCK (pending->connection);
742 
743  _dbus_message_trace_ref (message, -1, -1, "dbus_pending_call_steal_reply");
744  return message;
745 }
746 
762 void
764 {
765  _dbus_return_if_fail (pending != NULL);
766 
768 }
769 
786 {
787  _dbus_return_val_if_fail (slot_p != NULL, FALSE);
788 
789  return _dbus_data_slot_allocator_alloc (&slot_allocator,
790  slot_p);
791 }
792 
804 void
806 {
807  _dbus_return_if_fail (slot_p != NULL);
808  _dbus_return_if_fail (*slot_p >= 0);
809 
810  _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
811 }
812 
828  dbus_int32_t slot,
829  void *data,
830  DBusFreeFunction free_data_func)
831 {
832  dbus_bool_t retval;
833 
834  _dbus_return_val_if_fail (pending != NULL, FALSE);
835  _dbus_return_val_if_fail (slot >= 0, FALSE);
836 
837 
838  CONNECTION_LOCK (pending->connection);
839  retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
840  CONNECTION_UNLOCK (pending->connection);
841  return retval;
842 }
843 
852 void*
854  dbus_int32_t slot)
855 {
856  void *res;
857 
858  _dbus_return_val_if_fail (pending != NULL, NULL);
859 
860  CONNECTION_LOCK (pending->connection);
861  res = _dbus_data_slot_list_get (&slot_allocator,
862  &pending->slot_list,
863  slot);
864  CONNECTION_UNLOCK (pending->connection);
865 
866  return res;
867 }
868