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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-connection-internal.h"
26 #include "dbus-pending-call-internal.h"
27 #include "dbus-pending-call.h"
28 #include "dbus-list.h"
29 #include "dbus-threads.h"
30 #include "dbus-test.h"
33 * @defgroup DBusPendingCallInternals DBusPendingCall implementation details
34 * @ingroup DBusInternals
35 * @brief DBusPendingCall private implementation details.
37 * The guts of DBusPendingCall and its methods.
43 * @brief Internals of DBusPendingCall
45 * Opaque object representing a reply message that we're waiting for.
49 * shorter and more visible way to write _dbus_connection_lock()
51 #define CONNECTION_LOCK(connection) _dbus_connection_lock(connection)
53 * shorter and more visible way to write _dbus_connection_unlock()
55 #define CONNECTION_UNLOCK(connection) _dbus_connection_unlock(connection)
58 * Implementation details of #DBusPendingCall - all fields are private.
60 struct DBusPendingCall
62 DBusAtomic refcount; /**< reference count */
64 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
66 DBusPendingCallNotifyFunction function; /**< Notifier when reply arrives. */
68 DBusConnection *connection; /**< Connections we're associated with */
69 DBusMessage *reply; /**< Reply (after we've received it) */
70 DBusTimeout *timeout; /**< Timeout */
72 DBusList *timeout_link; /**< Preallocated timeout response */
74 dbus_uint32_t reply_serial; /**< Expected serial of reply */
76 unsigned int completed : 1; /**< TRUE if completed */
77 unsigned int timeout_added : 1; /**< Have added the timeout */
80 static dbus_int32_t notify_user_data_slot = -1;
83 * Creates a new pending reply object.
85 * @param connection connection where reply will arrive
86 * @param timeout_milliseconds length of timeout, -1 for default, INT_MAX for no timeout
87 * @param timeout_handler timeout handler, takes pending call as data
88 * @returns a new #DBusPendingCall or #NULL if no memory.
91 _dbus_pending_call_new_unlocked (DBusConnection *connection,
92 int timeout_milliseconds,
93 DBusTimeoutHandler timeout_handler)
95 DBusPendingCall *pending;
98 _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
100 if (timeout_milliseconds == -1)
101 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
103 if (!dbus_pending_call_allocate_data_slot (¬ify_user_data_slot))
106 pending = dbus_new0 (DBusPendingCall, 1);
110 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
114 if (timeout_milliseconds != _DBUS_INT_MAX)
116 timeout = _dbus_timeout_new (timeout_milliseconds,
122 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
127 pending->timeout = timeout;
131 pending->timeout = NULL;
134 pending->refcount.value = 1;
135 pending->connection = connection;
136 _dbus_connection_ref_unlocked (pending->connection);
138 _dbus_data_slot_list_init (&pending->slot_list);
144 * Sets the reply of a pending call with the given message,
145 * or if the message is #NULL, by timing out the pending call.
147 * @param pending the pending call
148 * @param message the message to complete the call with, or #NULL
149 * to time out the call
152 _dbus_pending_call_set_reply_unlocked (DBusPendingCall *pending,
153 DBusMessage *message)
157 message = pending->timeout_link->data;
158 _dbus_list_clear (&pending->timeout_link);
161 dbus_message_ref (message);
163 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
165 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
167 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
168 "error" : "other type",
169 pending->reply_serial);
171 _dbus_assert (pending->reply == NULL);
172 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
173 pending->reply = message;
177 * Calls notifier function for the pending call
178 * and sets the call to completed.
180 * @param pending the pending call
184 _dbus_pending_call_complete (DBusPendingCall *pending)
186 _dbus_assert (!pending->completed);
188 pending->completed = TRUE;
190 if (pending->function)
193 user_data = dbus_pending_call_get_data (pending,
194 notify_user_data_slot);
196 (* pending->function) (pending, user_data);
201 * If the pending call hasn't been timed out, add its timeout
202 * error reply to the connection's incoming message queue.
204 * @param pending the pending call
205 * @param connection the connection the call was sent to
208 _dbus_pending_call_queue_timeout_error_unlocked (DBusPendingCall *pending,
209 DBusConnection *connection)
211 _dbus_assert (connection == pending->connection);
213 if (pending->timeout_link)
215 _dbus_connection_queue_synthesized_message_link (connection,
216 pending->timeout_link);
217 pending->timeout_link = NULL;
222 * Checks to see if a timeout has been added
224 * @param pending the pending_call
225 * @returns #TRUE if there is a timeout or #FALSE if not
228 _dbus_pending_call_is_timeout_added_unlocked (DBusPendingCall *pending)
230 _dbus_assert (pending != NULL);
232 return pending->timeout_added;
237 * Sets wether the timeout has been added
239 * @param pending the pending_call
240 * @param is_added whether or not a timeout is added
243 _dbus_pending_call_set_timeout_added_unlocked (DBusPendingCall *pending,
244 dbus_bool_t is_added)
246 _dbus_assert (pending != NULL);
248 pending->timeout_added = is_added;
253 * Retrives the timeout
255 * @param pending the pending_call
256 * @returns a timeout object or NULL if call has no timeout
259 _dbus_pending_call_get_timeout_unlocked (DBusPendingCall *pending)
261 _dbus_assert (pending != NULL);
263 return pending->timeout;
267 * Gets the reply's serial number
269 * @param pending the pending_call
270 * @returns a serial number for the reply or 0
273 _dbus_pending_call_get_reply_serial_unlocked (DBusPendingCall *pending)
275 _dbus_assert (pending != NULL);
277 return pending->reply_serial;
281 * Sets the reply's serial number
283 * @param pending the pending_call
284 * @param serial the serial number
287 _dbus_pending_call_set_reply_serial_unlocked (DBusPendingCall *pending,
288 dbus_uint32_t serial)
290 _dbus_assert (pending != NULL);
291 _dbus_assert (pending->reply_serial == 0);
293 pending->reply_serial = serial;
297 * Gets the connection associated with this pending call.
299 * @param pending the pending_call
300 * @returns the connection associated with the pending call
303 _dbus_pending_call_get_connection_and_lock (DBusPendingCall *pending)
305 _dbus_assert (pending != NULL);
307 CONNECTION_LOCK (pending->connection);
308 return pending->connection;
312 * Gets the connection associated with this pending call.
314 * @param pending the pending_call
315 * @returns the connection associated with the pending call
318 _dbus_pending_call_get_connection_unlocked (DBusPendingCall *pending)
320 _dbus_assert (pending != NULL);
322 return pending->connection;
326 * Sets the reply message associated with the pending call to a timeout error
328 * @param pending the pending_call
329 * @param message the message we are sending the error reply to
330 * @param serial serial number for the reply
331 * @return #FALSE on OOM
334 _dbus_pending_call_set_timeout_error_unlocked (DBusPendingCall *pending,
335 DBusMessage *message,
336 dbus_uint32_t serial)
338 DBusList *reply_link;
341 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
342 "Did not receive a reply. Possible causes include: "
343 "the remote application did not send a reply, "
344 "the message bus security policy blocked the reply, "
345 "the reply timeout expired, or "
346 "the network connection was broken.");
350 reply_link = _dbus_list_alloc_link (reply);
351 if (reply_link == NULL)
353 dbus_message_unref (reply);
357 pending->timeout_link = reply_link;
359 _dbus_pending_call_set_reply_serial_unlocked (pending, serial);
365 * Increments the reference count on a pending call,
366 * while the lock on its connection is already held.
368 * @param pending the pending call object
369 * @returns the pending call object
372 _dbus_pending_call_ref_unlocked (DBusPendingCall *pending)
374 pending->refcount.value += 1;
381 _dbus_pending_call_last_unref (DBusPendingCall *pending)
383 DBusConnection *connection;
385 /* If we get here, we should be already detached
386 * from the connection, or never attached.
388 _dbus_assert (!pending->timeout_added);
390 connection = pending->connection;
392 /* this assumes we aren't holding connection lock... */
393 _dbus_data_slot_list_free (&pending->slot_list);
395 if (pending->timeout != NULL)
396 _dbus_timeout_unref (pending->timeout);
398 if (pending->timeout_link)
400 dbus_message_unref ((DBusMessage *)pending->timeout_link->data);
401 _dbus_list_free_link (pending->timeout_link);
402 pending->timeout_link = NULL;
407 dbus_message_unref (pending->reply);
408 pending->reply = NULL;
413 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
415 /* connection lock should not be held. */
416 /* Free the connection last to avoid a weird state while
417 * calling out to application code where the pending exists
418 * but not the connection.
420 dbus_connection_unref (connection);
424 * Decrements the reference count on a pending call,
425 * freeing it if the count reaches 0. Assumes
426 * connection lock is already held.
428 * @param pending the pending call object
431 _dbus_pending_call_unref_and_unlock (DBusPendingCall *pending)
433 dbus_bool_t last_unref;
435 _dbus_assert (pending->refcount.value > 0);
437 pending->refcount.value -= 1;
438 last_unref = pending->refcount.value == 0;
440 CONNECTION_UNLOCK (pending->connection);
442 _dbus_pending_call_last_unref (pending);
446 * Checks whether the pending call has received a reply
447 * yet, or not. Assumes connection lock is held.
449 * @param pending the pending call
450 * @returns #TRUE if a reply has been received
453 _dbus_pending_call_get_completed_unlocked (DBusPendingCall *pending)
455 return pending->completed;
458 static DBusDataSlotAllocator slot_allocator;
459 _DBUS_DEFINE_GLOBAL_LOCK (pending_call_slots);
462 * Stores a pointer on a #DBusPendingCall, along
463 * with an optional function to be used for freeing
464 * the data when the data is set again, or when
465 * the pending call is finalized. The slot number
466 * must have been allocated with dbus_pending_call_allocate_data_slot().
468 * @param pending the pending_call
469 * @param slot the slot number
470 * @param data the data to store
471 * @param free_data_func finalizer function for the data
472 * @returns #TRUE if there was enough memory to store the data
475 _dbus_pending_call_set_data_unlocked (DBusPendingCall *pending,
478 DBusFreeFunction free_data_func)
480 DBusFreeFunction old_free_func;
484 retval = _dbus_data_slot_list_set (&slot_allocator,
486 slot, data, free_data_func,
487 &old_free_func, &old_data);
489 /* Drop locks to call out to app code */
490 CONNECTION_UNLOCK (pending->connection);
495 (* old_free_func) (old_data);
498 CONNECTION_LOCK (pending->connection);
506 * @defgroup DBusPendingCall DBusPendingCall
508 * @brief Pending reply to a method call message
510 * A DBusPendingCall is an object representing an
511 * expected reply. A #DBusPendingCall can be created
512 * when you send a message that should have a reply.
518 * @typedef DBusPendingCall
520 * Opaque data type representing a message pending.
524 * Increments the reference count on a pending call.
526 * @param pending the pending call object
527 * @returns the pending call object
530 dbus_pending_call_ref (DBusPendingCall *pending)
532 _dbus_return_val_if_fail (pending != NULL, NULL);
534 /* The connection lock is better than the global
535 * lock in the atomic increment fallback
537 #ifdef DBUS_HAVE_ATOMIC_INT
538 _dbus_atomic_inc (&pending->refcount);
540 CONNECTION_LOCK (pending->connection);
541 _dbus_assert (pending->refcount.value > 0);
543 pending->refcount.value += 1;
544 CONNECTION_UNLOCK (pending->connection);
551 * Decrements the reference count on a pending call,
552 * freeing it if the count reaches 0.
554 * @param pending the pending call object
557 dbus_pending_call_unref (DBusPendingCall *pending)
559 dbus_bool_t last_unref;
561 _dbus_return_if_fail (pending != NULL);
563 /* More efficient to use the connection lock instead of atomic
564 * int fallback if we lack atomic int decrement
566 #ifdef DBUS_HAVE_ATOMIC_INT
567 last_unref = (_dbus_atomic_dec (&pending->refcount) == 1);
569 CONNECTION_LOCK (pending->connection);
570 _dbus_assert (pending->refcount.value > 0);
571 pending->refcount.value -= 1;
572 last_unref = pending->refcount.value == 0;
573 CONNECTION_UNLOCK (pending->connection);
577 _dbus_pending_call_last_unref(pending);
581 * Sets a notification function to be called when the reply is
582 * received or the pending call times out.
584 * @param pending the pending call
585 * @param function notifier function
586 * @param user_data data to pass to notifier function
587 * @param free_user_data function to free the user data
588 * @returns #FALSE if not enough memory
591 dbus_pending_call_set_notify (DBusPendingCall *pending,
592 DBusPendingCallNotifyFunction function,
594 DBusFreeFunction free_user_data)
596 _dbus_return_val_if_fail (pending != NULL, FALSE);
598 CONNECTION_LOCK (pending->connection);
600 /* could invoke application code! */
601 if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
602 user_data, free_user_data))
605 pending->function = function;
607 CONNECTION_UNLOCK (pending->connection);
613 * Cancels the pending call, such that any reply or error received
614 * will just be ignored. Drops the dbus library's internal reference
615 * to the #DBusPendingCall so will free the call if nobody else is
616 * holding a reference. However you usually get a reference from
617 * dbus_connection_send_with_reply() so probably your app owns a ref
620 * Note that canceling a pending call will <em>not</em> simulate a
621 * timed-out call; if a call times out, then a timeout error reply is
622 * received. If you cancel the call, no reply is received unless the
623 * the reply was already received before you canceled.
625 * @param pending the pending call
628 dbus_pending_call_cancel (DBusPendingCall *pending)
630 _dbus_return_if_fail (pending != NULL);
632 _dbus_connection_remove_pending_call (pending->connection,
637 * Checks whether the pending call has received a reply
640 * @param pending the pending call
641 * @returns #TRUE if a reply has been received
644 dbus_pending_call_get_completed (DBusPendingCall *pending)
646 dbus_bool_t completed;
648 _dbus_return_val_if_fail (pending != NULL, FALSE);
650 CONNECTION_LOCK (pending->connection);
651 completed = pending->completed;
652 CONNECTION_UNLOCK (pending->connection);
658 * Gets the reply, or returns #NULL if none has been received
659 * yet. Ownership of the reply message passes to the caller. This
660 * function can only be called once per pending call, since the reply
661 * message is tranferred to the caller.
663 * @param pending the pending call
664 * @returns the reply message or #NULL.
667 dbus_pending_call_steal_reply (DBusPendingCall *pending)
669 DBusMessage *message;
671 _dbus_return_val_if_fail (pending != NULL, NULL);
672 _dbus_return_val_if_fail (pending->completed, NULL);
673 _dbus_return_val_if_fail (pending->reply != NULL, NULL);
675 CONNECTION_LOCK (pending->connection);
677 message = pending->reply;
678 pending->reply = NULL;
680 CONNECTION_UNLOCK (pending->connection);
686 * Block until the pending call is completed. The blocking is as with
687 * dbus_connection_send_with_reply_and_block(); it does not enter the
688 * main loop or process other messages, it simply waits for the reply
691 * If the pending call is already completed, this function returns
694 * @todo when you start blocking, the timeout is reset, but it should
695 * really only use time remaining since the pending call was created.
696 * This requires storing timestamps instead of intervals in the timeout
698 * @param pending the pending call
701 dbus_pending_call_block (DBusPendingCall *pending)
703 _dbus_return_if_fail (pending != NULL);
705 _dbus_connection_block_pending_call (pending);
709 * Allocates an integer ID to be used for storing application-specific
710 * data on any DBusPendingCall. The allocated ID may then be used
711 * with dbus_pending_call_set_data() and dbus_pending_call_get_data().
712 * The passed-in slot must be initialized to -1, and is filled in
713 * with the slot ID. If the passed-in slot is not -1, it's assumed
714 * to be already allocated, and its refcount is incremented.
716 * The allocated slot is global, i.e. all DBusPendingCall objects will
717 * have a slot with the given integer ID reserved.
719 * @param slot_p address of a global variable storing the slot
720 * @returns #FALSE on failure (no memory)
723 dbus_pending_call_allocate_data_slot (dbus_int32_t *slot_p)
725 _dbus_return_val_if_fail (slot_p != NULL, FALSE);
727 return _dbus_data_slot_allocator_alloc (&slot_allocator,
728 &_DBUS_LOCK_NAME (pending_call_slots),
733 * Deallocates a global ID for #DBusPendingCall data slots.
734 * dbus_pending_call_get_data() and dbus_pending_call_set_data() may
735 * no longer be used with this slot. Existing data stored on existing
736 * DBusPendingCall objects will be freed when the #DBusPendingCall is
737 * finalized, but may not be retrieved (and may only be replaced if
738 * someone else reallocates the slot). When the refcount on the
739 * passed-in slot reaches 0, it is set to -1.
741 * @param slot_p address storing the slot to deallocate
744 dbus_pending_call_free_data_slot (dbus_int32_t *slot_p)
746 _dbus_return_if_fail (slot_p != NULL);
747 _dbus_return_if_fail (*slot_p >= 0);
749 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
753 * Stores a pointer on a #DBusPendingCall, along
754 * with an optional function to be used for freeing
755 * the data when the data is set again, or when
756 * the pending call is finalized. The slot number
757 * must have been allocated with dbus_pending_call_allocate_data_slot().
759 * @param pending the pending_call
760 * @param slot the slot number
761 * @param data the data to store
762 * @param free_data_func finalizer function for the data
763 * @returns #TRUE if there was enough memory to store the data
766 dbus_pending_call_set_data (DBusPendingCall *pending,
769 DBusFreeFunction free_data_func)
773 _dbus_return_val_if_fail (pending != NULL, FALSE);
774 _dbus_return_val_if_fail (slot >= 0, FALSE);
777 CONNECTION_LOCK (pending->connection);
778 retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
779 CONNECTION_UNLOCK (pending->connection);
784 * Retrieves data previously set with dbus_pending_call_set_data().
785 * The slot must still be allocated (must not have been freed).
787 * @param pending the pending_call
788 * @param slot the slot to get data from
789 * @returns the data, or #NULL if not found
792 dbus_pending_call_get_data (DBusPendingCall *pending,
797 _dbus_return_val_if_fail (pending != NULL, NULL);
799 CONNECTION_LOCK (pending->connection);
800 res = _dbus_data_slot_list_get (&slot_allocator,
803 CONNECTION_UNLOCK (pending->connection);
810 #ifdef DBUS_BUILD_TESTS
813 * @ingroup DBusPendingCallInternals
814 * Unit test for DBusPendingCall.
816 * @returns #TRUE on success.
819 _dbus_pending_call_test (const char *test_data_dir)
824 #endif /* DBUS_BUILD_TESTS */