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 */
79 * TRUE if some thread has taken responsibility for completing this
80 * pending call: either the pending call has completed, or it is about
81 * to be completed. Protected by the connection lock.
83 unsigned int completed : 1;
85 * TRUE if we have added the timeout. Protected by the connection lock.
87 unsigned int timeout_added : 1;
91 _dbus_pending_call_trace_ref (DBusPendingCall *pending_call,
96 #ifdef DBUS_ENABLE_VERBOSE_MODE
97 static int enabled = -1;
99 _dbus_trace_ref ("DBusPendingCall", pending_call, old_refcount,
100 new_refcount, why, "DBUS_PENDING_CALL_TRACE", &enabled);
104 static dbus_int32_t notify_user_data_slot = -1;
107 * Creates a new pending reply object.
109 * @param connection connection where reply will arrive
110 * @param timeout_milliseconds length of timeout, -1 (or
111 * #DBUS_TIMEOUT_USE_DEFAULT) for default,
112 * #DBUS_TIMEOUT_INFINITE for no timeout
113 * @param timeout_handler timeout handler, takes pending call as data
114 * @returns a new #DBusPendingCall or #NULL if no memory.
117 _dbus_pending_call_new_unlocked (DBusConnection *connection,
118 int timeout_milliseconds,
119 DBusTimeoutHandler timeout_handler)
121 DBusPendingCall *pending;
122 DBusTimeout *timeout;
124 _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
126 if (timeout_milliseconds == -1)
127 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
129 if (!dbus_pending_call_allocate_data_slot (¬ify_user_data_slot))
132 pending = dbus_new0 (DBusPendingCall, 1);
136 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
140 if (timeout_milliseconds != DBUS_TIMEOUT_INFINITE)
142 timeout = _dbus_timeout_new (timeout_milliseconds,
148 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
153 pending->timeout = timeout;
157 pending->timeout = NULL;
160 _dbus_atomic_inc (&pending->refcount);
161 pending->connection = connection;
162 _dbus_connection_ref_unlocked (pending->connection);
164 _dbus_data_slot_list_init (&pending->slot_list);
166 _dbus_pending_call_trace_ref (pending, 0, 1, "new_unlocked");
172 * Sets the reply of a pending call with the given message,
173 * or if the message is #NULL, by timing out the pending call.
175 * @param pending the pending call
176 * @param message the message to complete the call with, or #NULL
177 * to time out the call
180 _dbus_pending_call_set_reply_unlocked (DBusPendingCall *pending,
181 DBusMessage *message)
185 message = pending->timeout_link->data;
186 _dbus_list_clear (&pending->timeout_link);
189 dbus_message_ref (message);
191 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
193 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
195 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
196 "error" : "other type",
197 pending->reply_serial);
199 _dbus_assert (pending->reply == NULL);
200 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
201 pending->reply = message;
205 * Sets the pending call to completed
207 * This method is called with the connection lock held, to protect
208 * pending->completed. It must be paired with a call to
209 * _dbus_pending_call_finish_completion() after the connection lock has
212 * @param pending the pending call
215 _dbus_pending_call_start_completion_unlocked (DBusPendingCall *pending)
217 _dbus_assert (!pending->completed);
219 pending->completed = TRUE;
223 * Call the notifier function for the pending call.
225 * This method must be called after the connection lock has been
226 * released, and must be paired with a call to
227 * _dbus_pending_call_start_completion_unlocked().
229 * @param pending the pending call
232 _dbus_pending_call_finish_completion (DBusPendingCall *pending)
234 _dbus_assert (pending->completed);
236 if (pending->function)
239 user_data = dbus_pending_call_get_data (pending,
240 notify_user_data_slot);
242 (* pending->function) (pending, user_data);
247 * If the pending call hasn't been timed out, add its timeout
248 * error reply to the connection's incoming message queue.
250 * @param pending the pending call
251 * @param connection the connection the call was sent to
254 _dbus_pending_call_queue_timeout_error_unlocked (DBusPendingCall *pending,
255 DBusConnection *connection)
257 _dbus_assert (connection == pending->connection);
259 if (pending->timeout_link)
261 _dbus_connection_queue_synthesized_message_link (connection,
262 pending->timeout_link);
263 pending->timeout_link = NULL;
268 * Checks to see if a timeout has been added
270 * @param pending the pending_call
271 * @returns #TRUE if there is a timeout or #FALSE if not
274 _dbus_pending_call_is_timeout_added_unlocked (DBusPendingCall *pending)
276 _dbus_assert (pending != NULL);
278 return pending->timeout_added;
283 * Sets wether the timeout has been added
285 * @param pending the pending_call
286 * @param is_added whether or not a timeout is added
289 _dbus_pending_call_set_timeout_added_unlocked (DBusPendingCall *pending,
290 dbus_bool_t is_added)
292 _dbus_assert (pending != NULL);
294 pending->timeout_added = is_added;
299 * Retrives the timeout
301 * @param pending the pending_call
302 * @returns a timeout object or NULL if call has no timeout
305 _dbus_pending_call_get_timeout_unlocked (DBusPendingCall *pending)
307 _dbus_assert (pending != NULL);
309 return pending->timeout;
313 * Gets the reply's serial number
315 * @param pending the pending_call
316 * @returns a serial number for the reply or 0
319 _dbus_pending_call_get_reply_serial_unlocked (DBusPendingCall *pending)
321 _dbus_assert (pending != NULL);
323 return pending->reply_serial;
327 * Sets the reply's serial number
329 * @param pending the pending_call
330 * @param serial the serial number
333 _dbus_pending_call_set_reply_serial_unlocked (DBusPendingCall *pending,
334 dbus_uint32_t serial)
336 _dbus_assert (pending != NULL);
337 _dbus_assert (pending->reply_serial == 0);
339 pending->reply_serial = serial;
343 * Gets the connection associated with this pending call.
345 * @param pending the pending_call
346 * @returns the connection associated with the pending call
349 _dbus_pending_call_get_connection_and_lock (DBusPendingCall *pending)
351 _dbus_assert (pending != NULL);
353 CONNECTION_LOCK (pending->connection);
354 return pending->connection;
358 * Gets the connection associated with this pending call.
360 * @param pending the pending_call
361 * @returns the connection associated with the pending call
364 _dbus_pending_call_get_connection_unlocked (DBusPendingCall *pending)
366 _dbus_assert (pending != NULL);
368 return pending->connection;
372 * Sets the reply message associated with the pending call to a timeout error
374 * @param pending the pending_call
375 * @param message the message we are sending the error reply to
376 * @param serial serial number for the reply
377 * @return #FALSE on OOM
380 _dbus_pending_call_set_timeout_error_unlocked (DBusPendingCall *pending,
381 DBusMessage *message,
382 dbus_uint32_t serial)
384 DBusList *reply_link;
387 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
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.");
396 /* FIXME - lock may now fail */
397 dbus_message_lock (reply);
399 reply_link = _dbus_list_alloc_link (reply);
400 if (reply_link == NULL)
402 /* it's OK to unref this, nothing that could have attached a callback
403 * has ever seen it */
404 dbus_message_unref (reply);
408 pending->timeout_link = reply_link;
410 _dbus_pending_call_set_reply_serial_unlocked (pending, serial);
416 * Increments the reference count on a pending call,
417 * while the lock on its connection is already held.
419 * @param pending the pending call object
420 * @returns the pending call object
423 _dbus_pending_call_ref_unlocked (DBusPendingCall *pending)
425 dbus_int32_t old_refcount;
427 old_refcount = _dbus_atomic_inc (&pending->refcount);
428 _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
436 _dbus_pending_call_last_unref (DBusPendingCall *pending)
438 DBusConnection *connection;
440 /* If we get here, we should be already detached
441 * from the connection, or never attached.
443 _dbus_assert (!pending->timeout_added);
445 connection = pending->connection;
447 /* this assumes we aren't holding connection lock... */
448 _dbus_data_slot_list_free (&pending->slot_list);
450 if (pending->timeout != NULL)
451 _dbus_timeout_unref (pending->timeout);
453 if (pending->timeout_link)
455 dbus_message_unref ((DBusMessage *)pending->timeout_link->data);
456 _dbus_list_free_link (pending->timeout_link);
457 pending->timeout_link = NULL;
462 dbus_message_unref (pending->reply);
463 pending->reply = NULL;
468 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
470 /* connection lock should not be held. */
471 /* Free the connection last to avoid a weird state while
472 * calling out to application code where the pending exists
473 * but not the connection.
475 dbus_connection_unref (connection);
479 * Decrements the reference count on a pending call,
480 * freeing it if the count reaches 0. Assumes
481 * connection lock is already held.
483 * @param pending the pending call object
486 _dbus_pending_call_unref_and_unlock (DBusPendingCall *pending)
488 dbus_int32_t old_refcount;
490 old_refcount = _dbus_atomic_dec (&pending->refcount);
491 _dbus_assert (old_refcount > 0);
492 _dbus_pending_call_trace_ref (pending, old_refcount,
493 old_refcount - 1, "unref_and_unlock");
495 CONNECTION_UNLOCK (pending->connection);
497 if (old_refcount == 1)
498 _dbus_pending_call_last_unref (pending);
502 * Checks whether the pending call has received a reply
503 * yet, or not. Assumes connection lock is held.
505 * @param pending the pending call
506 * @returns #TRUE if a reply has been received
509 _dbus_pending_call_get_completed_unlocked (DBusPendingCall *pending)
511 return pending->completed;
514 static DBusDataSlotAllocator slot_allocator =
515 _DBUS_DATA_SLOT_ALLOCATOR_INIT (_DBUS_LOCK_NAME (pending_call_slots));
518 * Stores a pointer on a #DBusPendingCall, along
519 * with an optional function to be used for freeing
520 * the data when the data is set again, or when
521 * the pending call is finalized. The slot number
522 * must have been allocated with dbus_pending_call_allocate_data_slot().
524 * @param pending the pending_call
525 * @param slot the slot number
526 * @param data the data to store
527 * @param free_data_func finalizer function for the data
528 * @returns #TRUE if there was enough memory to store the data
531 _dbus_pending_call_set_data_unlocked (DBusPendingCall *pending,
534 DBusFreeFunction free_data_func)
536 DBusFreeFunction old_free_func;
540 retval = _dbus_data_slot_list_set (&slot_allocator,
542 slot, data, free_data_func,
543 &old_free_func, &old_data);
545 /* Drop locks to call out to app code */
546 CONNECTION_UNLOCK (pending->connection);
551 (* old_free_func) (old_data);
554 CONNECTION_LOCK (pending->connection);
562 * @defgroup DBusPendingCall DBusPendingCall
564 * @brief Pending reply to a method call message
566 * A DBusPendingCall is an object representing an
567 * expected reply. A #DBusPendingCall can be created
568 * when you send a message that should have a reply.
574 * @def DBUS_TIMEOUT_INFINITE
576 * An integer constant representing an infinite timeout. This has the
577 * numeric value 0x7fffffff (the largest 32-bit signed integer).
579 * For source compatibility with D-Bus versions earlier than 1.4.12, use
580 * 0x7fffffff, or INT32_MAX (assuming your platform has it).
584 * @def DBUS_TIMEOUT_USE_DEFAULT
586 * An integer constant representing a request to use the default timeout.
587 * This has numeric value -1.
589 * For source compatibility with D-Bus versions earlier than 1.4.12, use a
594 * @typedef DBusPendingCall
596 * Opaque data type representing a message pending.
600 * Increments the reference count on a pending call.
602 * @param pending the pending call object
603 * @returns the pending call object
606 dbus_pending_call_ref (DBusPendingCall *pending)
608 dbus_int32_t old_refcount;
610 _dbus_return_val_if_fail (pending != NULL, NULL);
612 old_refcount = _dbus_atomic_inc (&pending->refcount);
613 _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
620 * Decrements the reference count on a pending call,
621 * freeing it if the count reaches 0.
623 * @param pending the pending call object
626 dbus_pending_call_unref (DBusPendingCall *pending)
628 dbus_int32_t old_refcount;
630 _dbus_return_if_fail (pending != NULL);
632 old_refcount = _dbus_atomic_dec (&pending->refcount);
633 _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount - 1,
636 if (old_refcount == 1)
637 _dbus_pending_call_last_unref(pending);
641 * Sets a notification function to be called when the reply is
642 * received or the pending call times out.
644 * @param pending the pending call
645 * @param function notifier function
646 * @param user_data data to pass to notifier function
647 * @param free_user_data function to free the user data
648 * @returns #FALSE if not enough memory
651 dbus_pending_call_set_notify (DBusPendingCall *pending,
652 DBusPendingCallNotifyFunction function,
654 DBusFreeFunction free_user_data)
656 dbus_bool_t ret = FALSE;
658 _dbus_return_val_if_fail (pending != NULL, FALSE);
660 CONNECTION_LOCK (pending->connection);
662 /* could invoke application code! */
663 if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
664 user_data, free_user_data))
667 pending->function = function;
671 CONNECTION_UNLOCK (pending->connection);
677 * Cancels the pending call, such that any reply or error received
678 * will just be ignored. Drops the dbus library's internal reference
679 * to the #DBusPendingCall so will free the call if nobody else is
680 * holding a reference. However you usually get a reference from
681 * dbus_connection_send_with_reply() so probably your app owns a ref
684 * Note that canceling a pending call will <em>not</em> simulate a
685 * timed-out call; if a call times out, then a timeout error reply is
686 * received. If you cancel the call, no reply is received unless the
687 * the reply was already received before you canceled.
689 * @param pending the pending call
692 dbus_pending_call_cancel (DBusPendingCall *pending)
694 _dbus_return_if_fail (pending != NULL);
696 _dbus_connection_remove_pending_call (pending->connection,
701 * Checks whether the pending call has received a reply
704 * @param pending the pending call
705 * @returns #TRUE if a reply has been received
708 dbus_pending_call_get_completed (DBusPendingCall *pending)
710 dbus_bool_t completed;
712 _dbus_return_val_if_fail (pending != NULL, FALSE);
714 CONNECTION_LOCK (pending->connection);
715 completed = pending->completed;
716 CONNECTION_UNLOCK (pending->connection);
722 * Gets the reply, or returns #NULL if none has been received
723 * yet. Ownership of the reply message passes to the caller. This
724 * function can only be called once per pending call, since the reply
725 * message is tranferred to the caller.
727 * @param pending the pending call
728 * @returns the reply message or #NULL.
731 dbus_pending_call_steal_reply (DBusPendingCall *pending)
733 DBusMessage *message;
735 _dbus_return_val_if_fail (pending != NULL, NULL);
736 _dbus_return_val_if_fail (pending->completed, NULL);
737 _dbus_return_val_if_fail (pending->reply != NULL, NULL);
739 CONNECTION_LOCK (pending->connection);
741 message = pending->reply;
742 pending->reply = NULL;
744 CONNECTION_UNLOCK (pending->connection);
746 _dbus_message_trace_ref (message, -1, -1, "dbus_pending_call_steal_reply");
751 * Block until the pending call is completed. The blocking is as with
752 * dbus_connection_send_with_reply_and_block(); it does not enter the
753 * main loop or process other messages, it simply waits for the reply
756 * If the pending call is already completed, this function returns
759 * @todo when you start blocking, the timeout is reset, but it should
760 * really only use time remaining since the pending call was created.
761 * This requires storing timestamps instead of intervals in the timeout
763 * @param pending the pending call
766 dbus_pending_call_block (DBusPendingCall *pending)
768 _dbus_return_if_fail (pending != NULL);
770 _dbus_connection_block_pending_call (pending);
774 * Allocates an integer ID to be used for storing application-specific
775 * data on any DBusPendingCall. The allocated ID may then be used
776 * with dbus_pending_call_set_data() and dbus_pending_call_get_data().
777 * The passed-in slot must be initialized to -1, and is filled in
778 * with the slot ID. If the passed-in slot is not -1, it's assumed
779 * to be already allocated, and its refcount is incremented.
781 * The allocated slot is global, i.e. all DBusPendingCall objects will
782 * have a slot with the given integer ID reserved.
784 * @param slot_p address of a global variable storing the slot
785 * @returns #FALSE on failure (no memory)
788 dbus_pending_call_allocate_data_slot (dbus_int32_t *slot_p)
790 _dbus_return_val_if_fail (slot_p != NULL, FALSE);
792 return _dbus_data_slot_allocator_alloc (&slot_allocator,
797 * Deallocates a global ID for #DBusPendingCall data slots.
798 * dbus_pending_call_get_data() and dbus_pending_call_set_data() may
799 * no longer be used with this slot. Existing data stored on existing
800 * DBusPendingCall objects will be freed when the #DBusPendingCall is
801 * finalized, but may not be retrieved (and may only be replaced if
802 * someone else reallocates the slot). When the refcount on the
803 * passed-in slot reaches 0, it is set to -1.
805 * @param slot_p address storing the slot to deallocate
808 dbus_pending_call_free_data_slot (dbus_int32_t *slot_p)
810 _dbus_return_if_fail (slot_p != NULL);
811 _dbus_return_if_fail (*slot_p >= 0);
813 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
817 * Stores a pointer on a #DBusPendingCall, along
818 * with an optional function to be used for freeing
819 * the data when the data is set again, or when
820 * the pending call is finalized. The slot number
821 * must have been allocated with dbus_pending_call_allocate_data_slot().
823 * @param pending the pending_call
824 * @param slot the slot number
825 * @param data the data to store
826 * @param free_data_func finalizer function for the data
827 * @returns #TRUE if there was enough memory to store the data
830 dbus_pending_call_set_data (DBusPendingCall *pending,
833 DBusFreeFunction free_data_func)
837 _dbus_return_val_if_fail (pending != NULL, FALSE);
838 _dbus_return_val_if_fail (slot >= 0, FALSE);
841 CONNECTION_LOCK (pending->connection);
842 retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
843 CONNECTION_UNLOCK (pending->connection);
848 * Retrieves data previously set with dbus_pending_call_set_data().
849 * The slot must still be allocated (must not have been freed).
851 * @param pending the pending_call
852 * @param slot the slot to get data from
853 * @returns the data, or #NULL if not found
856 dbus_pending_call_get_data (DBusPendingCall *pending,
861 _dbus_return_val_if_fail (pending != NULL, NULL);
863 CONNECTION_LOCK (pending->connection);
864 res = _dbus_data_slot_list_get (&slot_allocator,
867 CONNECTION_UNLOCK (pending->connection);