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 /* clamp the timeout otherwise math in
104 * _dbus_connection_block_for_reply would get all overflow-prone
106 if ((timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6) &&
107 (timeout_milliseconds < _DBUS_INT_MAX))
108 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
110 if (!dbus_pending_call_allocate_data_slot (¬ify_user_data_slot))
113 pending = dbus_new0 (DBusPendingCall, 1);
117 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
121 if (timeout_milliseconds != _DBUS_INT_MAX)
123 timeout = _dbus_timeout_new (timeout_milliseconds,
129 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
134 pending->timeout = timeout;
138 pending->timeout = NULL;
141 pending->refcount.value = 1;
142 pending->connection = connection;
143 _dbus_connection_ref_unlocked (pending->connection);
145 _dbus_data_slot_list_init (&pending->slot_list);
151 * Sets the reply of a pending call with the given message,
152 * or if the message is #NULL, by timing out the pending call.
154 * @param pending the pending call
155 * @param message the message to complete the call with, or #NULL
156 * to time out the call
159 _dbus_pending_call_set_reply_unlocked (DBusPendingCall *pending,
160 DBusMessage *message)
164 message = pending->timeout_link->data;
165 _dbus_list_clear (&pending->timeout_link);
168 dbus_message_ref (message);
170 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
172 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
174 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
175 "error" : "other type",
176 pending->reply_serial);
178 _dbus_assert (pending->reply == NULL);
179 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
180 pending->reply = message;
184 * Calls notifier function for the pending call
185 * and sets the call to completed.
187 * @param pending the pending call
191 _dbus_pending_call_complete (DBusPendingCall *pending)
193 _dbus_assert (!pending->completed);
195 pending->completed = TRUE;
197 if (pending->function)
200 user_data = dbus_pending_call_get_data (pending,
201 notify_user_data_slot);
203 (* pending->function) (pending, user_data);
208 * If the pending call hasn't been timed out, add its timeout
209 * error reply to the connection's incoming message queue.
211 * @param pending the pending call
212 * @param connection the connection the call was sent to
215 _dbus_pending_call_queue_timeout_error_unlocked (DBusPendingCall *pending,
216 DBusConnection *connection)
218 _dbus_assert (connection == pending->connection);
220 if (pending->timeout_link)
222 _dbus_connection_queue_synthesized_message_link (connection,
223 pending->timeout_link);
224 pending->timeout_link = NULL;
229 * Checks to see if a timeout has been added
231 * @param pending the pending_call
232 * @returns #TRUE if there is a timeout or #FALSE if not
235 _dbus_pending_call_is_timeout_added_unlocked (DBusPendingCall *pending)
237 _dbus_assert (pending != NULL);
239 return pending->timeout_added;
244 * Sets wether the timeout has been added
246 * @param pending the pending_call
247 * @param is_added whether or not a timeout is added
250 _dbus_pending_call_set_timeout_added_unlocked (DBusPendingCall *pending,
251 dbus_bool_t is_added)
253 _dbus_assert (pending != NULL);
255 pending->timeout_added = is_added;
260 * Retrives the timeout
262 * @param pending the pending_call
263 * @returns a timeout object or NULL if call has no timeout
266 _dbus_pending_call_get_timeout_unlocked (DBusPendingCall *pending)
268 _dbus_assert (pending != NULL);
270 return pending->timeout;
274 * Gets the reply's serial number
276 * @param pending the pending_call
277 * @returns a serial number for the reply or 0
280 _dbus_pending_call_get_reply_serial_unlocked (DBusPendingCall *pending)
282 _dbus_assert (pending != NULL);
284 return pending->reply_serial;
288 * Sets the reply's serial number
290 * @param pending the pending_call
291 * @param serial the serial number
294 _dbus_pending_call_set_reply_serial_unlocked (DBusPendingCall *pending,
295 dbus_uint32_t serial)
297 _dbus_assert (pending != NULL);
298 _dbus_assert (pending->reply_serial == 0);
300 pending->reply_serial = serial;
304 * Gets the connection associated with this pending call.
306 * @param pending the pending_call
307 * @returns the connection associated with the pending call
310 _dbus_pending_call_get_connection_and_lock (DBusPendingCall *pending)
312 _dbus_assert (pending != NULL);
314 CONNECTION_LOCK (pending->connection);
315 return pending->connection;
319 * Gets the connection associated with this pending call.
321 * @param pending the pending_call
322 * @returns the connection associated with the pending call
325 _dbus_pending_call_get_connection_unlocked (DBusPendingCall *pending)
327 _dbus_assert (pending != NULL);
329 return pending->connection;
333 * Sets the reply message associated with the pending call to a timeout error
335 * @param pending the pending_call
336 * @param message the message we are sending the error reply to
337 * @param serial serial number for the reply
338 * @return #FALSE on OOM
341 _dbus_pending_call_set_timeout_error_unlocked (DBusPendingCall *pending,
342 DBusMessage *message,
343 dbus_uint32_t serial)
345 DBusList *reply_link;
348 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
349 "Did not receive a reply. Possible causes include: "
350 "the remote application did not send a reply, "
351 "the message bus security policy blocked the reply, "
352 "the reply timeout expired, or "
353 "the network connection was broken.");
357 reply_link = _dbus_list_alloc_link (reply);
358 if (reply_link == NULL)
360 dbus_message_unref (reply);
364 pending->timeout_link = reply_link;
366 _dbus_pending_call_set_reply_serial_unlocked (pending, serial);
372 * Increments the reference count on a pending call,
373 * while the lock on its connection is already held.
375 * @param pending the pending call object
376 * @returns the pending call object
379 _dbus_pending_call_ref_unlocked (DBusPendingCall *pending)
381 pending->refcount.value += 1;
388 _dbus_pending_call_last_unref (DBusPendingCall *pending)
390 DBusConnection *connection;
392 /* If we get here, we should be already detached
393 * from the connection, or never attached.
395 _dbus_assert (!pending->timeout_added);
397 connection = pending->connection;
399 /* this assumes we aren't holding connection lock... */
400 _dbus_data_slot_list_free (&pending->slot_list);
402 if (pending->timeout != NULL)
403 _dbus_timeout_unref (pending->timeout);
405 if (pending->timeout_link)
407 dbus_message_unref ((DBusMessage *)pending->timeout_link->data);
408 _dbus_list_free_link (pending->timeout_link);
409 pending->timeout_link = NULL;
414 dbus_message_unref (pending->reply);
415 pending->reply = NULL;
420 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
422 /* connection lock should not be held. */
423 /* Free the connection last to avoid a weird state while
424 * calling out to application code where the pending exists
425 * but not the connection.
427 dbus_connection_unref (connection);
431 * Decrements the reference count on a pending call,
432 * freeing it if the count reaches 0. Assumes
433 * connection lock is already held.
435 * @param pending the pending call object
438 _dbus_pending_call_unref_and_unlock (DBusPendingCall *pending)
440 dbus_bool_t last_unref;
442 _dbus_assert (pending->refcount.value > 0);
444 pending->refcount.value -= 1;
445 last_unref = pending->refcount.value == 0;
447 CONNECTION_UNLOCK (pending->connection);
449 _dbus_pending_call_last_unref (pending);
453 * Checks whether the pending call has received a reply
454 * yet, or not. Assumes connection lock is held.
456 * @param pending the pending call
457 * @returns #TRUE if a reply has been received
460 _dbus_pending_call_get_completed_unlocked (DBusPendingCall *pending)
462 return pending->completed;
465 static DBusDataSlotAllocator slot_allocator;
466 _DBUS_DEFINE_GLOBAL_LOCK (pending_call_slots);
469 * Stores a pointer on a #DBusPendingCall, along
470 * with an optional function to be used for freeing
471 * the data when the data is set again, or when
472 * the pending call is finalized. The slot number
473 * must have been allocated with dbus_pending_call_allocate_data_slot().
475 * @param pending the pending_call
476 * @param slot the slot number
477 * @param data the data to store
478 * @param free_data_func finalizer function for the data
479 * @returns #TRUE if there was enough memory to store the data
482 _dbus_pending_call_set_data_unlocked (DBusPendingCall *pending,
485 DBusFreeFunction free_data_func)
487 DBusFreeFunction old_free_func;
491 retval = _dbus_data_slot_list_set (&slot_allocator,
493 slot, data, free_data_func,
494 &old_free_func, &old_data);
496 /* Drop locks to call out to app code */
497 CONNECTION_UNLOCK (pending->connection);
502 (* old_free_func) (old_data);
505 CONNECTION_LOCK (pending->connection);
513 * @defgroup DBusPendingCall DBusPendingCall
515 * @brief Pending reply to a method call message
517 * A DBusPendingCall is an object representing an
518 * expected reply. A #DBusPendingCall can be created
519 * when you send a message that should have a reply.
525 * @typedef DBusPendingCall
527 * Opaque data type representing a message pending.
531 * Increments the reference count on a pending call.
533 * @param pending the pending call object
534 * @returns the pending call object
537 dbus_pending_call_ref (DBusPendingCall *pending)
539 _dbus_return_val_if_fail (pending != NULL, NULL);
541 /* The connection lock is better than the global
542 * lock in the atomic increment fallback
544 #ifdef DBUS_HAVE_ATOMIC_INT
545 _dbus_atomic_inc (&pending->refcount);
547 CONNECTION_LOCK (pending->connection);
548 _dbus_assert (pending->refcount.value > 0);
550 pending->refcount.value += 1;
551 CONNECTION_UNLOCK (pending->connection);
558 * Decrements the reference count on a pending call,
559 * freeing it if the count reaches 0.
561 * @param pending the pending call object
564 dbus_pending_call_unref (DBusPendingCall *pending)
566 dbus_bool_t last_unref;
568 _dbus_return_if_fail (pending != NULL);
570 /* More efficient to use the connection lock instead of atomic
571 * int fallback if we lack atomic int decrement
573 #ifdef DBUS_HAVE_ATOMIC_INT
574 last_unref = (_dbus_atomic_dec (&pending->refcount) == 1);
576 CONNECTION_LOCK (pending->connection);
577 _dbus_assert (pending->refcount.value > 0);
578 pending->refcount.value -= 1;
579 last_unref = pending->refcount.value == 0;
580 CONNECTION_UNLOCK (pending->connection);
584 _dbus_pending_call_last_unref(pending);
588 * Sets a notification function to be called when the reply is
589 * received or the pending call times out.
591 * @param pending the pending call
592 * @param function notifier function
593 * @param user_data data to pass to notifier function
594 * @param free_user_data function to free the user data
595 * @returns #FALSE if not enough memory
598 dbus_pending_call_set_notify (DBusPendingCall *pending,
599 DBusPendingCallNotifyFunction function,
601 DBusFreeFunction free_user_data)
603 _dbus_return_val_if_fail (pending != NULL, FALSE);
605 CONNECTION_LOCK (pending->connection);
607 /* could invoke application code! */
608 if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
609 user_data, free_user_data))
612 pending->function = function;
614 CONNECTION_UNLOCK (pending->connection);
620 * Cancels the pending call, such that any reply or error received
621 * will just be ignored. Drops the dbus library's internal reference
622 * to the #DBusPendingCall so will free the call if nobody else is
623 * holding a reference. However you usually get a reference from
624 * dbus_connection_send_with_reply() so probably your app owns a ref
627 * Note that canceling a pending call will <em>not</em> simulate a
628 * timed-out call; if a call times out, then a timeout error reply is
629 * received. If you cancel the call, no reply is received unless the
630 * the reply was already received before you canceled.
632 * @param pending the pending call
635 dbus_pending_call_cancel (DBusPendingCall *pending)
637 _dbus_return_if_fail (pending != NULL);
639 _dbus_connection_remove_pending_call (pending->connection,
644 * Checks whether the pending call has received a reply
647 * @param pending the pending call
648 * @returns #TRUE if a reply has been received
651 dbus_pending_call_get_completed (DBusPendingCall *pending)
653 dbus_bool_t completed;
655 _dbus_return_val_if_fail (pending != NULL, FALSE);
657 CONNECTION_LOCK (pending->connection);
658 completed = pending->completed;
659 CONNECTION_UNLOCK (pending->connection);
665 * Gets the reply, or returns #NULL if none has been received
666 * yet. Ownership of the reply message passes to the caller. This
667 * function can only be called once per pending call, since the reply
668 * message is tranferred to the caller.
670 * @param pending the pending call
671 * @returns the reply message or #NULL.
674 dbus_pending_call_steal_reply (DBusPendingCall *pending)
676 DBusMessage *message;
678 _dbus_return_val_if_fail (pending != NULL, NULL);
679 _dbus_return_val_if_fail (pending->completed, NULL);
680 _dbus_return_val_if_fail (pending->reply != NULL, NULL);
682 CONNECTION_LOCK (pending->connection);
684 message = pending->reply;
685 pending->reply = NULL;
687 CONNECTION_UNLOCK (pending->connection);
693 * Block until the pending call is completed. The blocking is as with
694 * dbus_connection_send_with_reply_and_block(); it does not enter the
695 * main loop or process other messages, it simply waits for the reply
698 * If the pending call is already completed, this function returns
701 * @todo when you start blocking, the timeout is reset, but it should
702 * really only use time remaining since the pending call was created.
703 * This requires storing timestamps instead of intervals in the timeout
705 * @param pending the pending call
708 dbus_pending_call_block (DBusPendingCall *pending)
710 _dbus_return_if_fail (pending != NULL);
712 _dbus_connection_block_pending_call (pending);
716 * Allocates an integer ID to be used for storing application-specific
717 * data on any DBusPendingCall. The allocated ID may then be used
718 * with dbus_pending_call_set_data() and dbus_pending_call_get_data().
719 * The passed-in slot must be initialized to -1, and is filled in
720 * with the slot ID. If the passed-in slot is not -1, it's assumed
721 * to be already allocated, and its refcount is incremented.
723 * The allocated slot is global, i.e. all DBusPendingCall objects will
724 * have a slot with the given integer ID reserved.
726 * @param slot_p address of a global variable storing the slot
727 * @returns #FALSE on failure (no memory)
730 dbus_pending_call_allocate_data_slot (dbus_int32_t *slot_p)
732 _dbus_return_val_if_fail (slot_p != NULL, FALSE);
734 return _dbus_data_slot_allocator_alloc (&slot_allocator,
735 &_DBUS_LOCK_NAME (pending_call_slots),
740 * Deallocates a global ID for #DBusPendingCall data slots.
741 * dbus_pending_call_get_data() and dbus_pending_call_set_data() may
742 * no longer be used with this slot. Existing data stored on existing
743 * DBusPendingCall objects will be freed when the #DBusPendingCall is
744 * finalized, but may not be retrieved (and may only be replaced if
745 * someone else reallocates the slot). When the refcount on the
746 * passed-in slot reaches 0, it is set to -1.
748 * @param slot_p address storing the slot to deallocate
751 dbus_pending_call_free_data_slot (dbus_int32_t *slot_p)
753 _dbus_return_if_fail (slot_p != NULL);
754 _dbus_return_if_fail (*slot_p >= 0);
756 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
760 * Stores a pointer on a #DBusPendingCall, along
761 * with an optional function to be used for freeing
762 * the data when the data is set again, or when
763 * the pending call is finalized. The slot number
764 * must have been allocated with dbus_pending_call_allocate_data_slot().
766 * @param pending the pending_call
767 * @param slot the slot number
768 * @param data the data to store
769 * @param free_data_func finalizer function for the data
770 * @returns #TRUE if there was enough memory to store the data
773 dbus_pending_call_set_data (DBusPendingCall *pending,
776 DBusFreeFunction free_data_func)
780 _dbus_return_val_if_fail (pending != NULL, FALSE);
781 _dbus_return_val_if_fail (slot >= 0, FALSE);
784 CONNECTION_LOCK (pending->connection);
785 retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
786 CONNECTION_UNLOCK (pending->connection);
791 * Retrieves data previously set with dbus_pending_call_set_data().
792 * The slot must still be allocated (must not have been freed).
794 * @param pending the pending_call
795 * @param slot the slot to get data from
796 * @returns the data, or #NULL if not found
799 dbus_pending_call_get_data (DBusPendingCall *pending,
804 _dbus_return_val_if_fail (pending != NULL, NULL);
806 CONNECTION_LOCK (pending->connection);
807 res = _dbus_data_slot_list_get (&slot_allocator,
810 CONNECTION_UNLOCK (pending->connection);
817 #ifdef DBUS_BUILD_TESTS
820 * @ingroup DBusPendingCallInternals
821 * Unit test for DBusPendingCall.
823 * @returns #TRUE on success.
826 _dbus_pending_call_test (const char *test_data_dir)
831 #endif /* DBUS_BUILD_TESTS */