1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-pending-call.c Object representing a call in progress.
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
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.
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.
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
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"
35 * @defgroup DBusPendingCallInternals DBusPendingCall implementation details
36 * @ingroup DBusInternals
37 * @brief DBusPendingCall private implementation details.
39 * The guts of DBusPendingCall and its methods.
45 * @brief Internals of DBusPendingCall
47 * Opaque object representing a reply message that we're waiting for.
51 * shorter and more visible way to write _dbus_connection_lock()
53 #define CONNECTION_LOCK(connection) _dbus_connection_lock(connection)
55 * shorter and more visible way to write _dbus_connection_unlock()
57 #define CONNECTION_UNLOCK(connection) _dbus_connection_unlock(connection)
60 * Implementation details of #DBusPendingCall - all fields are private.
62 struct DBusPendingCall
64 DBusAtomic refcount; /**< reference count */
66 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
68 DBusPendingCallNotifyFunction function; /**< Notifier when reply arrives. */
70 DBusConnection *connection; /**< Connections we're associated with */
71 DBusMessage *reply; /**< Reply (after we've received it) */
72 DBusTimeout *timeout; /**< Timeout */
74 DBusList *timeout_link; /**< Preallocated timeout response */
76 dbus_uint32_t reply_serial; /**< Expected serial of reply */
78 unsigned int completed : 1; /**< TRUE if completed */
79 unsigned int timeout_added : 1; /**< Have added the timeout */
82 #ifdef DBUS_ENABLE_VERBOSE_MODE
84 _dbus_pending_call_trace_ref (DBusPendingCall *pending_call,
89 static int enabled = -1;
91 _dbus_trace_ref ("DBusPendingCall", pending_call, old_refcount,
92 new_refcount, why, "DBUS_PENDING_CALL_TRACE", &enabled);
95 #define _dbus_pending_call_trace_ref(p, o, n, w) \
103 static dbus_int32_t notify_user_data_slot = -1;
106 * Creates a new pending reply object.
108 * @param connection connection where reply will arrive
109 * @param timeout_milliseconds length of timeout, -1 (or
110 * #DBUS_TIMEOUT_USE_DEFAULT) for default,
111 * #DBUS_TIMEOUT_INFINITE for no timeout
112 * @param timeout_handler timeout handler, takes pending call as data
113 * @returns a new #DBusPendingCall or #NULL if no memory.
116 _dbus_pending_call_new_unlocked (DBusConnection *connection,
117 int timeout_milliseconds,
118 DBusTimeoutHandler timeout_handler)
120 DBusPendingCall *pending;
121 DBusTimeout *timeout;
123 _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
125 if (timeout_milliseconds == -1)
126 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
128 if (!dbus_pending_call_allocate_data_slot (¬ify_user_data_slot))
131 pending = dbus_new0 (DBusPendingCall, 1);
135 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
139 if (timeout_milliseconds != DBUS_TIMEOUT_INFINITE)
141 timeout = _dbus_timeout_new (timeout_milliseconds,
147 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
152 pending->timeout = timeout;
156 pending->timeout = NULL;
159 _dbus_atomic_inc (&pending->refcount);
160 pending->connection = connection;
161 _dbus_connection_ref_unlocked (pending->connection);
163 _dbus_data_slot_list_init (&pending->slot_list);
165 _dbus_pending_call_trace_ref (pending, 0, 1, "new_unlocked");
171 * Sets the reply of a pending call with the given message,
172 * or if the message is #NULL, by timing out the pending call.
174 * @param pending the pending call
175 * @param message the message to complete the call with, or #NULL
176 * to time out the call
179 _dbus_pending_call_set_reply_unlocked (DBusPendingCall *pending,
180 DBusMessage *message)
184 message = pending->timeout_link->data;
185 _dbus_list_clear (&pending->timeout_link);
188 dbus_message_ref (message);
190 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
192 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
194 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
195 "error" : "other type",
196 pending->reply_serial);
198 _dbus_assert (pending->reply == NULL);
199 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
200 pending->reply = message;
204 * Calls notifier function for the pending call
205 * and sets the call to completed.
207 * @param pending the pending call
211 _dbus_pending_call_complete (DBusPendingCall *pending)
213 _dbus_assert (!pending->completed);
215 pending->completed = TRUE;
217 if (pending->function)
220 user_data = dbus_pending_call_get_data (pending,
221 notify_user_data_slot);
223 (* pending->function) (pending, user_data);
228 * If the pending call hasn't been timed out, add its timeout
229 * error reply to the connection's incoming message queue.
231 * @param pending the pending call
232 * @param connection the connection the call was sent to
235 _dbus_pending_call_queue_timeout_error_unlocked (DBusPendingCall *pending,
236 DBusConnection *connection)
238 _dbus_assert (connection == pending->connection);
240 if (pending->timeout_link)
242 _dbus_connection_queue_synthesized_message_link (connection,
243 pending->timeout_link);
244 pending->timeout_link = NULL;
249 * Checks to see if a timeout has been added
251 * @param pending the pending_call
252 * @returns #TRUE if there is a timeout or #FALSE if not
255 _dbus_pending_call_is_timeout_added_unlocked (DBusPendingCall *pending)
257 _dbus_assert (pending != NULL);
259 return pending->timeout_added;
264 * Sets wether the timeout has been added
266 * @param pending the pending_call
267 * @param is_added whether or not a timeout is added
270 _dbus_pending_call_set_timeout_added_unlocked (DBusPendingCall *pending,
271 dbus_bool_t is_added)
273 _dbus_assert (pending != NULL);
275 pending->timeout_added = is_added;
280 * Retrives the timeout
282 * @param pending the pending_call
283 * @returns a timeout object or NULL if call has no timeout
286 _dbus_pending_call_get_timeout_unlocked (DBusPendingCall *pending)
288 _dbus_assert (pending != NULL);
290 return pending->timeout;
294 * Gets the reply's serial number
296 * @param pending the pending_call
297 * @returns a serial number for the reply or 0
300 _dbus_pending_call_get_reply_serial_unlocked (DBusPendingCall *pending)
302 _dbus_assert (pending != NULL);
304 return pending->reply_serial;
308 * Sets the reply's serial number
310 * @param pending the pending_call
311 * @param serial the serial number
314 _dbus_pending_call_set_reply_serial_unlocked (DBusPendingCall *pending,
315 dbus_uint32_t serial)
317 _dbus_assert (pending != NULL);
318 _dbus_assert (pending->reply_serial == 0);
320 pending->reply_serial = serial;
324 * Gets the connection associated with this pending call.
326 * @param pending the pending_call
327 * @returns the connection associated with the pending call
330 _dbus_pending_call_get_connection_and_lock (DBusPendingCall *pending)
332 _dbus_assert (pending != NULL);
334 CONNECTION_LOCK (pending->connection);
335 return pending->connection;
339 * Gets the connection associated with this pending call.
341 * @param pending the pending_call
342 * @returns the connection associated with the pending call
345 _dbus_pending_call_get_connection_unlocked (DBusPendingCall *pending)
347 _dbus_assert (pending != NULL);
349 return pending->connection;
353 * Sets the reply message associated with the pending call to a timeout error
355 * @param pending the pending_call
356 * @param message the message we are sending the error reply to
357 * @param serial serial number for the reply
358 * @return #FALSE on OOM
361 _dbus_pending_call_set_timeout_error_unlocked (DBusPendingCall *pending,
362 DBusMessage *message,
363 dbus_uint32_t serial)
365 DBusList *reply_link;
368 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
369 "Did not receive a reply. Possible causes include: "
370 "the remote application did not send a reply, "
371 "the message bus security policy blocked the reply, "
372 "the reply timeout expired, or "
373 "the network connection was broken.");
377 reply_link = _dbus_list_alloc_link (reply);
378 if (reply_link == NULL)
380 /* it's OK to unref this, nothing that could have attached a callback
381 * has ever seen it */
382 dbus_message_unref (reply);
386 pending->timeout_link = reply_link;
388 _dbus_pending_call_set_reply_serial_unlocked (pending, serial);
394 * Increments the reference count on a pending call,
395 * while the lock on its connection is already held.
397 * @param pending the pending call object
398 * @returns the pending call object
401 _dbus_pending_call_ref_unlocked (DBusPendingCall *pending)
403 dbus_int32_t old_refcount;
405 old_refcount = _dbus_atomic_inc (&pending->refcount);
406 _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
414 _dbus_pending_call_last_unref (DBusPendingCall *pending)
416 DBusConnection *connection;
418 /* If we get here, we should be already detached
419 * from the connection, or never attached.
421 _dbus_assert (!pending->timeout_added);
423 connection = pending->connection;
425 /* this assumes we aren't holding connection lock... */
426 _dbus_data_slot_list_free (&pending->slot_list);
428 if (pending->timeout != NULL)
429 _dbus_timeout_unref (pending->timeout);
431 if (pending->timeout_link)
433 dbus_message_unref ((DBusMessage *)pending->timeout_link->data);
434 _dbus_list_free_link (pending->timeout_link);
435 pending->timeout_link = NULL;
440 dbus_message_unref (pending->reply);
441 pending->reply = NULL;
446 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
448 /* connection lock should not be held. */
449 /* Free the connection last to avoid a weird state while
450 * calling out to application code where the pending exists
451 * but not the connection.
453 dbus_connection_unref (connection);
457 * Decrements the reference count on a pending call,
458 * freeing it if the count reaches 0. Assumes
459 * connection lock is already held.
461 * @param pending the pending call object
464 _dbus_pending_call_unref_and_unlock (DBusPendingCall *pending)
466 dbus_int32_t old_refcount;
468 old_refcount = _dbus_atomic_dec (&pending->refcount);
469 _dbus_assert (old_refcount > 0);
470 _dbus_pending_call_trace_ref (pending, old_refcount,
471 old_refcount - 1, "unref_and_unlock");
473 CONNECTION_UNLOCK (pending->connection);
475 if (old_refcount == 1)
476 _dbus_pending_call_last_unref (pending);
480 * Checks whether the pending call has received a reply
481 * yet, or not. Assumes connection lock is held.
483 * @param pending the pending call
484 * @returns #TRUE if a reply has been received
487 _dbus_pending_call_get_completed_unlocked (DBusPendingCall *pending)
489 return pending->completed;
492 static DBusDataSlotAllocator slot_allocator =
493 _DBUS_DATA_SLOT_ALLOCATOR_INIT (_DBUS_LOCK_NAME (pending_call_slots));
496 * Stores a pointer on a #DBusPendingCall, along
497 * with an optional function to be used for freeing
498 * the data when the data is set again, or when
499 * the pending call is finalized. The slot number
500 * must have been allocated with dbus_pending_call_allocate_data_slot().
502 * @param pending the pending_call
503 * @param slot the slot number
504 * @param data the data to store
505 * @param free_data_func finalizer function for the data
506 * @returns #TRUE if there was enough memory to store the data
509 _dbus_pending_call_set_data_unlocked (DBusPendingCall *pending,
512 DBusFreeFunction free_data_func)
514 DBusFreeFunction old_free_func;
518 retval = _dbus_data_slot_list_set (&slot_allocator,
520 slot, data, free_data_func,
521 &old_free_func, &old_data);
523 /* Drop locks to call out to app code */
524 CONNECTION_UNLOCK (pending->connection);
529 (* old_free_func) (old_data);
532 CONNECTION_LOCK (pending->connection);
540 * @defgroup DBusPendingCall DBusPendingCall
542 * @brief Pending reply to a method call message
544 * A DBusPendingCall is an object representing an
545 * expected reply. A #DBusPendingCall can be created
546 * when you send a message that should have a reply.
552 * @def DBUS_TIMEOUT_INFINITE
554 * An integer constant representing an infinite timeout. This has the
555 * numeric value 0x7fffffff (the largest 32-bit signed integer).
557 * For source compatibility with D-Bus versions earlier than 1.4.12, use
558 * 0x7fffffff, or INT32_MAX (assuming your platform has it).
562 * @def DBUS_TIMEOUT_USE_DEFAULT
564 * An integer constant representing a request to use the default timeout.
565 * This has numeric value -1.
567 * For source compatibility with D-Bus versions earlier than 1.4.12, use a
572 * @typedef DBusPendingCall
574 * Opaque data type representing a message pending.
578 * Increments the reference count on a pending call.
580 * @param pending the pending call object
581 * @returns the pending call object
584 dbus_pending_call_ref (DBusPendingCall *pending)
586 dbus_int32_t old_refcount;
588 _dbus_return_val_if_fail (pending != NULL, NULL);
590 old_refcount = _dbus_atomic_inc (&pending->refcount);
591 _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
598 * Decrements the reference count on a pending call,
599 * freeing it if the count reaches 0.
601 * @param pending the pending call object
604 dbus_pending_call_unref (DBusPendingCall *pending)
606 dbus_int32_t old_refcount;
608 _dbus_return_if_fail (pending != NULL);
610 old_refcount = _dbus_atomic_dec (&pending->refcount);
611 _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount - 1,
614 if (old_refcount == 1)
615 _dbus_pending_call_last_unref(pending);
619 * Sets a notification function to be called when the reply is
620 * received or the pending call times out.
622 * @param pending the pending call
623 * @param function notifier function
624 * @param user_data data to pass to notifier function
625 * @param free_user_data function to free the user data
626 * @returns #FALSE if not enough memory
629 dbus_pending_call_set_notify (DBusPendingCall *pending,
630 DBusPendingCallNotifyFunction function,
632 DBusFreeFunction free_user_data)
634 dbus_bool_t ret = FALSE;
636 _dbus_return_val_if_fail (pending != NULL, FALSE);
638 CONNECTION_LOCK (pending->connection);
640 /* could invoke application code! */
641 if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
642 user_data, free_user_data))
645 pending->function = function;
649 CONNECTION_UNLOCK (pending->connection);
655 * Cancels the pending call, such that any reply or error received
656 * will just be ignored. Drops the dbus library's internal reference
657 * to the #DBusPendingCall so will free the call if nobody else is
658 * holding a reference. However you usually get a reference from
659 * dbus_connection_send_with_reply() so probably your app owns a ref
662 * Note that canceling a pending call will <em>not</em> simulate a
663 * timed-out call; if a call times out, then a timeout error reply is
664 * received. If you cancel the call, no reply is received unless the
665 * the reply was already received before you canceled.
667 * @param pending the pending call
670 dbus_pending_call_cancel (DBusPendingCall *pending)
672 _dbus_return_if_fail (pending != NULL);
674 _dbus_connection_remove_pending_call (pending->connection,
679 * Checks whether the pending call has received a reply
682 * @param pending the pending call
683 * @returns #TRUE if a reply has been received
686 dbus_pending_call_get_completed (DBusPendingCall *pending)
688 dbus_bool_t completed;
690 _dbus_return_val_if_fail (pending != NULL, FALSE);
692 CONNECTION_LOCK (pending->connection);
693 completed = pending->completed;
694 CONNECTION_UNLOCK (pending->connection);
700 * Gets the reply, or returns #NULL if none has been received
701 * yet. Ownership of the reply message passes to the caller. This
702 * function can only be called once per pending call, since the reply
703 * message is tranferred to the caller.
705 * @param pending the pending call
706 * @returns the reply message or #NULL.
709 dbus_pending_call_steal_reply (DBusPendingCall *pending)
711 DBusMessage *message;
713 _dbus_return_val_if_fail (pending != NULL, NULL);
714 _dbus_return_val_if_fail (pending->completed, NULL);
715 _dbus_return_val_if_fail (pending->reply != NULL, NULL);
717 CONNECTION_LOCK (pending->connection);
719 message = pending->reply;
720 pending->reply = NULL;
722 CONNECTION_UNLOCK (pending->connection);
724 _dbus_message_trace_ref (message, -1, -1, "dbus_pending_call_steal_reply");
729 * Block until the pending call is completed. The blocking is as with
730 * dbus_connection_send_with_reply_and_block(); it does not enter the
731 * main loop or process other messages, it simply waits for the reply
734 * If the pending call is already completed, this function returns
737 * @todo when you start blocking, the timeout is reset, but it should
738 * really only use time remaining since the pending call was created.
739 * This requires storing timestamps instead of intervals in the timeout
741 * @param pending the pending call
744 dbus_pending_call_block (DBusPendingCall *pending)
746 _dbus_return_if_fail (pending != NULL);
748 _dbus_connection_block_pending_call (pending);
752 * Allocates an integer ID to be used for storing application-specific
753 * data on any DBusPendingCall. The allocated ID may then be used
754 * with dbus_pending_call_set_data() and dbus_pending_call_get_data().
755 * The passed-in slot must be initialized to -1, and is filled in
756 * with the slot ID. If the passed-in slot is not -1, it's assumed
757 * to be already allocated, and its refcount is incremented.
759 * The allocated slot is global, i.e. all DBusPendingCall objects will
760 * have a slot with the given integer ID reserved.
762 * @param slot_p address of a global variable storing the slot
763 * @returns #FALSE on failure (no memory)
766 dbus_pending_call_allocate_data_slot (dbus_int32_t *slot_p)
768 _dbus_return_val_if_fail (slot_p != NULL, FALSE);
770 return _dbus_data_slot_allocator_alloc (&slot_allocator,
775 * Deallocates a global ID for #DBusPendingCall data slots.
776 * dbus_pending_call_get_data() and dbus_pending_call_set_data() may
777 * no longer be used with this slot. Existing data stored on existing
778 * DBusPendingCall objects will be freed when the #DBusPendingCall is
779 * finalized, but may not be retrieved (and may only be replaced if
780 * someone else reallocates the slot). When the refcount on the
781 * passed-in slot reaches 0, it is set to -1.
783 * @param slot_p address storing the slot to deallocate
786 dbus_pending_call_free_data_slot (dbus_int32_t *slot_p)
788 _dbus_return_if_fail (slot_p != NULL);
789 _dbus_return_if_fail (*slot_p >= 0);
791 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
795 * Stores a pointer on a #DBusPendingCall, along
796 * with an optional function to be used for freeing
797 * the data when the data is set again, or when
798 * the pending call is finalized. The slot number
799 * must have been allocated with dbus_pending_call_allocate_data_slot().
801 * @param pending the pending_call
802 * @param slot the slot number
803 * @param data the data to store
804 * @param free_data_func finalizer function for the data
805 * @returns #TRUE if there was enough memory to store the data
808 dbus_pending_call_set_data (DBusPendingCall *pending,
811 DBusFreeFunction free_data_func)
815 _dbus_return_val_if_fail (pending != NULL, FALSE);
816 _dbus_return_val_if_fail (slot >= 0, FALSE);
819 CONNECTION_LOCK (pending->connection);
820 retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
821 CONNECTION_UNLOCK (pending->connection);
826 * Retrieves data previously set with dbus_pending_call_set_data().
827 * The slot must still be allocated (must not have been freed).
829 * @param pending the pending_call
830 * @param slot the slot to get data from
831 * @returns the data, or #NULL if not found
834 dbus_pending_call_get_data (DBusPendingCall *pending,
839 _dbus_return_val_if_fail (pending != NULL, NULL);
841 CONNECTION_LOCK (pending->connection);
842 res = _dbus_data_slot_list_get (&slot_allocator,
845 CONNECTION_UNLOCK (pending->connection);