+/**
+ * If the pending call hasn't been timed out, add its timeout
+ * error reply to the connection's incoming message queue.
+ *
+ * @param pending the pending call
+ * @param connection the connection the call was sent to
+ */
+void
+_dbus_pending_call_queue_timeout_error_unlocked (DBusPendingCall *pending,
+ DBusConnection *connection)
+{
+ _dbus_assert (connection == pending->connection);
+
+ if (pending->timeout_link)
+ {
+ _dbus_connection_queue_synthesized_message_link (connection,
+ pending->timeout_link);
+ pending->timeout_link = NULL;
+ }
+}
+
+/**
+ * Checks to see if a timeout has been added
+ *
+ * @param pending the pending_call
+ * @returns #TRUE if there is a timeout or #FALSE if not
+ */
+dbus_bool_t
+_dbus_pending_call_is_timeout_added_unlocked (DBusPendingCall *pending)
+{
+ _dbus_assert (pending != NULL);
+
+ return pending->timeout_added;
+}
+
+
+/**
+ * Sets wether the timeout has been added
+ *
+ * @param pending the pending_call
+ * @param is_added whether or not a timeout is added
+ */
+void
+_dbus_pending_call_set_timeout_added_unlocked (DBusPendingCall *pending,
+ dbus_bool_t is_added)
+{
+ _dbus_assert (pending != NULL);
+
+ pending->timeout_added = is_added;
+}
+
+
+/**
+ * Retrives the timeout
+ *
+ * @param pending the pending_call
+ * @returns a timeout object or NULL if call has no timeout
+ */
+DBusTimeout *
+_dbus_pending_call_get_timeout_unlocked (DBusPendingCall *pending)
+{
+ _dbus_assert (pending != NULL);
+
+ return pending->timeout;
+}
+
+/**
+ * Gets the reply's serial number
+ *
+ * @param pending the pending_call
+ * @returns a serial number for the reply or 0
+ */
+dbus_uint32_t
+_dbus_pending_call_get_reply_serial_unlocked (DBusPendingCall *pending)
+{
+ _dbus_assert (pending != NULL);
+
+ return pending->reply_serial;
+}
+
+/**
+ * Sets the reply's serial number
+ *
+ * @param pending the pending_call
+ * @param serial the serial number
+ */
+void
+_dbus_pending_call_set_reply_serial_unlocked (DBusPendingCall *pending,
+ dbus_uint32_t serial)
+{
+ _dbus_assert (pending != NULL);
+ _dbus_assert (pending->reply_serial == 0);
+
+ pending->reply_serial = serial;
+}
+
+/**
+ * Gets the connection associated with this pending call.
+ *
+ * @param pending the pending_call
+ * @returns the connection associated with the pending call
+ */
+DBusConnection *
+_dbus_pending_call_get_connection_and_lock (DBusPendingCall *pending)
+{
+ _dbus_assert (pending != NULL);
+
+ CONNECTION_LOCK (pending->connection);
+ return pending->connection;
+}
+
+/**
+ * Gets the connection associated with this pending call.
+ *
+ * @param pending the pending_call
+ * @returns the connection associated with the pending call
+ */
+DBusConnection *
+_dbus_pending_call_get_connection_unlocked (DBusPendingCall *pending)
+{
+ _dbus_assert (pending != NULL);
+
+ return pending->connection;
+}
+
+/**
+ * Sets the reply message associated with the pending call to a timeout error
+ *
+ * @param pending the pending_call
+ * @param message the message we are sending the error reply to
+ * @param serial serial number for the reply
+ * @return #FALSE on OOM
+ */
+dbus_bool_t
+_dbus_pending_call_set_timeout_error_unlocked (DBusPendingCall *pending,
+ DBusMessage *message,
+ dbus_uint32_t serial)
+{
+ DBusList *reply_link;
+ DBusMessage *reply;
+
+ reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
+ "Did not receive a reply. Possible causes include: "
+ "the remote application did not send a reply, "
+ "the message bus security policy blocked the reply, "
+ "the reply timeout expired, or "
+ "the network connection was broken.");
+ if (reply == NULL)
+ return FALSE;
+
+ reply_link = _dbus_list_alloc_link (reply);
+ if (reply_link == NULL)
+ {
+ /* it's OK to unref this, nothing that could have attached a callback
+ * has ever seen it */
+ dbus_message_unref (reply);
+ return FALSE;
+ }
+
+ pending->timeout_link = reply_link;
+
+ _dbus_pending_call_set_reply_serial_unlocked (pending, serial);
+
+ return TRUE;
+}
+
+/**
+ * Increments the reference count on a pending call,
+ * while the lock on its connection is already held.
+ *
+ * @param pending the pending call object
+ * @returns the pending call object
+ */
+DBusPendingCall *
+_dbus_pending_call_ref_unlocked (DBusPendingCall *pending)
+{
+ dbus_int32_t old_refcount;
+
+ old_refcount = _dbus_atomic_inc (&pending->refcount);
+ _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
+ "ref_unlocked");
+
+ return pending;
+}
+
+
+static void
+_dbus_pending_call_last_unref (DBusPendingCall *pending)
+{
+ DBusConnection *connection;
+
+ /* If we get here, we should be already detached
+ * from the connection, or never attached.
+ */
+ _dbus_assert (!pending->timeout_added);
+
+ connection = pending->connection;
+
+ /* this assumes we aren't holding connection lock... */
+ _dbus_data_slot_list_free (&pending->slot_list);
+
+ if (pending->timeout != NULL)
+ _dbus_timeout_unref (pending->timeout);
+
+ if (pending->timeout_link)
+ {
+ dbus_message_unref ((DBusMessage *)pending->timeout_link->data);
+ _dbus_list_free_link (pending->timeout_link);
+ pending->timeout_link = NULL;
+ }
+
+ if (pending->reply)
+ {
+ dbus_message_unref (pending->reply);
+ pending->reply = NULL;
+ }
+
+ dbus_free (pending);
+
+ dbus_pending_call_free_data_slot (¬ify_user_data_slot);
+
+ /* connection lock should not be held. */
+ /* Free the connection last to avoid a weird state while
+ * calling out to application code where the pending exists
+ * but not the connection.
+ */
+ dbus_connection_unref (connection);
+}
+
+/**
+ * Decrements the reference count on a pending call,
+ * freeing it if the count reaches 0. Assumes
+ * connection lock is already held.
+ *
+ * @param pending the pending call object
+ */
+void
+_dbus_pending_call_unref_and_unlock (DBusPendingCall *pending)
+{
+ dbus_int32_t old_refcount;
+
+ old_refcount = _dbus_atomic_dec (&pending->refcount);
+ _dbus_assert (old_refcount > 0);
+ _dbus_pending_call_trace_ref (pending, old_refcount,
+ old_refcount - 1, "unref_and_unlock");
+
+ CONNECTION_UNLOCK (pending->connection);
+
+ if (old_refcount == 1)
+ _dbus_pending_call_last_unref (pending);
+}
+
+/**
+ * Checks whether the pending call has received a reply
+ * yet, or not. Assumes connection lock is held.
+ *
+ * @param pending the pending call
+ * @returns #TRUE if a reply has been received
+ */
+dbus_bool_t
+_dbus_pending_call_get_completed_unlocked (DBusPendingCall *pending)
+{
+ return pending->completed;
+}
+
+static DBusDataSlotAllocator slot_allocator =
+ _DBUS_DATA_SLOT_ALLOCATOR_INIT (_DBUS_LOCK_NAME (pending_call_slots));
+
+/**
+ * Stores a pointer on a #DBusPendingCall, along
+ * with an optional function to be used for freeing
+ * the data when the data is set again, or when
+ * the pending call is finalized. The slot number
+ * must have been allocated with dbus_pending_call_allocate_data_slot().
+ *
+ * @param pending the pending_call
+ * @param slot the slot number
+ * @param data the data to store
+ * @param free_data_func finalizer function for the data
+ * @returns #TRUE if there was enough memory to store the data
+ */
+dbus_bool_t
+_dbus_pending_call_set_data_unlocked (DBusPendingCall *pending,
+ dbus_int32_t slot,
+ void *data,
+ DBusFreeFunction free_data_func)
+{
+ DBusFreeFunction old_free_func;
+ void *old_data;
+ dbus_bool_t retval;
+
+ retval = _dbus_data_slot_list_set (&slot_allocator,
+ &pending->slot_list,
+ slot, data, free_data_func,
+ &old_free_func, &old_data);
+
+ /* Drop locks to call out to app code */
+ CONNECTION_UNLOCK (pending->connection);
+
+ if (retval)
+ {
+ if (old_free_func)
+ (* old_free_func) (old_data);
+ }
+
+ CONNECTION_LOCK (pending->connection);
+
+ return retval;
+}
+