1 /* -*- mode: C; c-file-style: "gnu" -*- */
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)
57 struct DBusPendingCall
59 DBusAtomic refcount; /**< reference count */
61 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
63 DBusPendingCallNotifyFunction function; /**< Notifier when reply arrives. */
65 DBusConnection *connection; /**< Connections we're associated with */
66 DBusMessage *reply; /**< Reply (after we've received it) */
67 DBusTimeout *timeout; /**< Timeout */
69 DBusList *timeout_link; /**< Preallocated timeout response */
71 dbus_uint32_t reply_serial; /**< Expected serial of reply */
73 unsigned int completed : 1; /**< TRUE if completed */
74 unsigned int timeout_added : 1; /**< Have added the timeout */
77 static dbus_int32_t notify_user_data_slot = -1;
80 * Creates a new pending reply object.
82 * @param connection connection where reply will arrive
83 * @param timeout_milliseconds length of timeout, -1 for default
84 * @param timeout_handler timeout handler, takes pending call as data
85 * @returns a new #DBusPendingCall or #NULL if no memory.
88 _dbus_pending_call_new_unlocked (DBusConnection *connection,
89 int timeout_milliseconds,
90 DBusTimeoutHandler timeout_handler)
92 DBusPendingCall *pending;
95 _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
97 if (timeout_milliseconds == -1)
98 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
100 /* it would probably seem logical to pass in _DBUS_INT_MAX for
101 * infinite timeout, but then math in
102 * _dbus_connection_block_for_reply would get all overflow-prone, so
105 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
106 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
108 if (!dbus_pending_call_allocate_data_slot (¬ify_user_data_slot))
111 pending = dbus_new0 (DBusPendingCall, 1);
115 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
119 timeout = _dbus_timeout_new (timeout_milliseconds,
125 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
130 pending->refcount.value = 1;
131 pending->connection = connection;
132 _dbus_connection_ref_unlocked (pending->connection);
134 pending->timeout = timeout;
137 _dbus_data_slot_list_init (&pending->slot_list);
143 * Sets the reply of a pending call with the given message,
144 * or if the message is #NULL, by timing out the pending call.
146 * @param pending the pending call
147 * @param message the message to complete the call with, or #NULL
148 * to time out the call
151 _dbus_pending_call_set_reply_unlocked (DBusPendingCall *pending,
152 DBusMessage *message)
156 message = pending->timeout_link->data;
157 _dbus_list_clear (&pending->timeout_link);
160 dbus_message_ref (message);
162 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
164 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
166 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
167 "error" : "other type",
168 pending->reply_serial);
170 _dbus_assert (pending->reply == NULL);
171 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
172 pending->reply = message;
176 * Calls notifier function for the pending call
177 * and sets the call to completed.
179 * @param pending the pending call
183 _dbus_pending_call_complete (DBusPendingCall *pending)
185 _dbus_assert (!pending->completed);
187 pending->completed = TRUE;
189 if (pending->function)
192 user_data = dbus_pending_call_get_data (pending,
193 notify_user_data_slot);
195 (* pending->function) (pending, user_data);
200 * If the pending call hasn't been timed out, add its timeout
201 * error reply to the connection's incoming message queue.
203 * @param pending the pending call
204 * @param connection the connection the call was sent to
207 _dbus_pending_call_queue_timeout_error_unlocked (DBusPendingCall *pending,
208 DBusConnection *connection)
210 _dbus_assert (connection == pending->connection);
212 if (pending->timeout_link)
214 _dbus_connection_queue_synthesized_message_link (connection,
215 pending->timeout_link);
216 pending->timeout_link = NULL;
221 * Checks to see if a timeout has been added
223 * @param pending the pending_call
224 * @returns #TRUE if there is a timeout or #FALSE if not
227 _dbus_pending_call_is_timeout_added_unlocked (DBusPendingCall *pending)
229 _dbus_assert (pending != NULL);
231 return pending->timeout_added;
236 * Sets wether the timeout has been added
238 * @param pending the pending_call
239 * @param is_added whether or not a timeout is added
242 _dbus_pending_call_set_timeout_added_unlocked (DBusPendingCall *pending,
243 dbus_bool_t is_added)
245 _dbus_assert (pending != NULL);
247 pending->timeout_added = is_added;
252 * Retrives the timeout
254 * @param pending the pending_call
255 * @returns a timeout object
258 _dbus_pending_call_get_timeout_unlocked (DBusPendingCall *pending)
260 _dbus_assert (pending != NULL);
262 return pending->timeout;
266 * Gets the reply's serial number
268 * @param pending the pending_call
269 * @returns a serial number for the reply or 0
272 _dbus_pending_call_get_reply_serial_unlocked (DBusPendingCall *pending)
274 _dbus_assert (pending != NULL);
276 return pending->reply_serial;
280 * Sets the reply's serial number
282 * @param pending the pending_call
283 * @param serial the serial number
286 _dbus_pending_call_set_reply_serial_unlocked (DBusPendingCall *pending,
287 dbus_uint32_t serial)
289 _dbus_assert (pending != NULL);
290 _dbus_assert (pending->reply_serial == 0);
292 pending->reply_serial = serial;
296 * Gets the connection associated with this pending call.
298 * @param pending the pending_call
299 * @returns the connection associated with the pending call
302 _dbus_pending_call_get_connection_and_lock (DBusPendingCall *pending)
304 _dbus_assert (pending != NULL);
306 CONNECTION_LOCK (pending->connection);
307 return pending->connection;
311 * Gets the connection associated with this pending call.
313 * @param pending the pending_call
314 * @returns the connection associated with the pending call
317 _dbus_pending_call_get_connection_unlocked (DBusPendingCall *pending)
319 _dbus_assert (pending != NULL);
321 return pending->connection;
325 * Sets the reply message associated with the pending call to a timeout error
327 * @param pending the pending_call
328 * @param message the message we are sending the error reply to
329 * @param serial serial number for the reply
330 * @return #FALSE on OOM
333 _dbus_pending_call_set_timeout_error_unlocked (DBusPendingCall *pending,
334 DBusMessage *message,
335 dbus_uint32_t serial)
337 DBusList *reply_link;
340 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
341 "Did not receive a reply. Possible causes include: "
342 "the remote application did not send a reply, "
343 "the message bus security policy blocked the reply, "
344 "the reply timeout expired, or "
345 "the network connection was broken.");
349 reply_link = _dbus_list_alloc_link (reply);
350 if (reply_link == NULL)
352 dbus_message_unref (reply);
356 pending->timeout_link = reply_link;
358 _dbus_pending_call_set_reply_serial_unlocked (pending, serial);
366 * @defgroup DBusPendingCall DBusPendingCall
368 * @brief Pending reply to a method call message
370 * A DBusPendingCall is an object representing an
371 * expected reply. A #DBusPendingCall can be created
372 * when you send a message that should have a reply.
378 * @typedef DBusPendingCall
380 * Opaque data type representing a message pending.
384 * Increments the reference count on a pending call,
385 * while the lock on its connection is already held.
387 * @param pending the pending call object
388 * @returns the pending call object
391 _dbus_pending_call_ref_unlocked (DBusPendingCall *pending)
393 pending->refcount.value += 1;
399 * Increments the reference count on a pending call.
401 * @param pending the pending call object
402 * @returns the pending call object
405 dbus_pending_call_ref (DBusPendingCall *pending)
407 _dbus_return_val_if_fail (pending != NULL, NULL);
409 /* The connection lock is better than the global
410 * lock in the atomic increment fallback
412 #ifdef DBUS_HAVE_ATOMIC_INT
413 _dbus_atomic_inc (&pending->refcount);
415 CONNECTION_LOCK (pending->connection);
416 _dbus_assert (pending->refcount.value > 0);
418 pending->refcount.value += 1;
419 CONNECTION_UNLOCK (pending->connection);
426 _dbus_pending_call_last_unref (DBusPendingCall *pending)
428 DBusConnection *connection;
430 /* If we get here, we should be already detached
431 * from the connection, or never attached.
433 _dbus_assert (!pending->timeout_added);
435 connection = pending->connection;
437 /* this assumes we aren't holding connection lock... */
438 _dbus_data_slot_list_free (&pending->slot_list);
440 if (pending->timeout != NULL)
441 _dbus_timeout_unref (pending->timeout);
443 if (pending->timeout_link)
445 dbus_message_unref ((DBusMessage *)pending->timeout_link->data);
446 _dbus_list_free_link (pending->timeout_link);
447 pending->timeout_link = NULL;
452 dbus_message_unref (pending->reply);
453 pending->reply = NULL;
458 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
460 /* connection lock should not be held. */
461 /* Free the connection last to avoid a weird state while
462 * calling out to application code where the pending exists
463 * but not the connection.
465 dbus_connection_unref (connection);
469 * Decrements the reference count on a pending call,
470 * freeing it if the count reaches 0. Assumes
471 * connection lock is already held.
473 * @param pending the pending call object
476 _dbus_pending_call_unref_and_unlock (DBusPendingCall *pending)
478 dbus_bool_t last_unref;
480 _dbus_assert (pending->refcount.value > 0);
482 pending->refcount.value -= 1;
483 last_unref = pending->refcount.value == 0;
485 CONNECTION_UNLOCK (pending->connection);
487 _dbus_pending_call_last_unref (pending);
491 * Decrements the reference count on a pending call,
492 * freeing it if the count reaches 0.
494 * @param pending the pending call object
497 dbus_pending_call_unref (DBusPendingCall *pending)
499 dbus_bool_t last_unref;
501 _dbus_return_if_fail (pending != NULL);
503 /* More efficient to use the connection lock instead of atomic
504 * int fallback if we lack atomic int decrement
506 #ifdef DBUS_HAVE_ATOMIC_INT
507 last_unref = (_dbus_atomic_dec (&pending->refcount) == 1);
509 CONNECTION_LOCK (pending->connection);
510 _dbus_assert (pending->refcount.value > 0);
511 pending->refcount.value -= 1;
512 last_unref = pending->refcount.value == 0;
513 CONNECTION_UNLOCK (pending->connection);
517 _dbus_pending_call_last_unref(pending);
521 * Sets a notification function to be called when the reply is
522 * received or the pending call times out.
524 * @param pending the pending call
525 * @param function notifier function
526 * @param user_data data to pass to notifier function
527 * @param free_user_data function to free the user data
528 * @returns #FALSE if not enough memory
531 dbus_pending_call_set_notify (DBusPendingCall *pending,
532 DBusPendingCallNotifyFunction function,
534 DBusFreeFunction free_user_data)
536 _dbus_return_val_if_fail (pending != NULL, FALSE);
538 CONNECTION_LOCK (pending->connection);
540 /* could invoke application code! */
541 if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
542 user_data, free_user_data))
545 pending->function = function;
547 CONNECTION_UNLOCK (pending->connection);
553 * Cancels the pending call, such that any reply or error received
554 * will just be ignored. Drops the dbus library's internal reference
555 * to the #DBusPendingCall so will free the call if nobody else is
556 * holding a reference. However you usually get a reference
557 * from dbus_connection_send() so probably your app owns a ref also.
559 * @param pending the pending call
562 dbus_pending_call_cancel (DBusPendingCall *pending)
564 _dbus_return_if_fail (pending != NULL);
566 _dbus_connection_remove_pending_call (pending->connection,
571 * Checks whether the pending call has received a reply
572 * yet, or not. Assumes connection lock is held.
574 * @param pending the pending call
575 * @returns #TRUE if a reply has been received
578 _dbus_pending_call_get_completed_unlocked (DBusPendingCall *pending)
580 return pending->completed;
584 * Checks whether the pending call has received a reply
587 * @param pending the pending call
588 * @returns #TRUE if a reply has been received
591 dbus_pending_call_get_completed (DBusPendingCall *pending)
593 dbus_bool_t completed;
595 _dbus_return_val_if_fail (pending != NULL, FALSE);
597 CONNECTION_LOCK (pending->connection);
598 completed = pending->completed;
599 CONNECTION_UNLOCK (pending->connection);
605 * Gets the reply, or returns #NULL if none has been received
606 * yet. Ownership of the reply message passes to the caller. This
607 * function can only be called once per pending call, since the reply
608 * message is tranferred to the caller.
610 * @param pending the pending call
611 * @returns the reply message or #NULL.
614 dbus_pending_call_steal_reply (DBusPendingCall *pending)
616 DBusMessage *message;
618 _dbus_return_val_if_fail (pending != NULL, NULL);
619 _dbus_return_val_if_fail (pending->completed, NULL);
620 _dbus_return_val_if_fail (pending->reply != NULL, NULL);
622 CONNECTION_LOCK (pending->connection);
624 message = pending->reply;
625 pending->reply = NULL;
627 CONNECTION_UNLOCK (pending->connection);
633 * Block until the pending call is completed. The blocking is as with
634 * dbus_connection_send_with_reply_and_block(); it does not enter the
635 * main loop or process other messages, it simply waits for the reply
638 * If the pending call is already completed, this function returns
641 * @todo when you start blocking, the timeout is reset, but it should
642 * really only use time remaining since the pending call was created.
643 * This requires storing timestamps instead of intervals in the timeout
645 * @param pending the pending call
648 dbus_pending_call_block (DBusPendingCall *pending)
650 _dbus_return_if_fail (pending != NULL);
652 _dbus_connection_block_pending_call (pending);
655 static DBusDataSlotAllocator slot_allocator;
656 _DBUS_DEFINE_GLOBAL_LOCK (pending_call_slots);
659 * Allocates an integer ID to be used for storing application-specific
660 * data on any DBusPendingCall. The allocated ID may then be used
661 * with dbus_pending_call_set_data() and dbus_pending_call_get_data().
662 * The passed-in slot must be initialized to -1, and is filled in
663 * with the slot ID. If the passed-in slot is not -1, it's assumed
664 * to be already allocated, and its refcount is incremented.
666 * The allocated slot is global, i.e. all DBusPendingCall objects will
667 * have a slot with the given integer ID reserved.
669 * @param slot_p address of a global variable storing the slot
670 * @returns #FALSE on failure (no memory)
673 dbus_pending_call_allocate_data_slot (dbus_int32_t *slot_p)
675 _dbus_return_val_if_fail (slot_p != NULL, FALSE);
677 return _dbus_data_slot_allocator_alloc (&slot_allocator,
678 &_DBUS_LOCK_NAME (pending_call_slots),
683 * Deallocates a global ID for #DBusPendingCall data slots.
684 * dbus_pending_call_get_data() and dbus_pending_call_set_data() may
685 * no longer be used with this slot. Existing data stored on existing
686 * DBusPendingCall objects will be freed when the #DBusPendingCall is
687 * finalized, but may not be retrieved (and may only be replaced if
688 * someone else reallocates the slot). When the refcount on the
689 * passed-in slot reaches 0, it is set to -1.
691 * @param slot_p address storing the slot to deallocate
694 dbus_pending_call_free_data_slot (dbus_int32_t *slot_p)
696 _dbus_return_if_fail (slot_p != NULL);
697 _dbus_return_if_fail (*slot_p >= 0);
699 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
703 * Stores a pointer on a #DBusPendingCall, along
704 * with an optional function to be used for freeing
705 * the data when the data is set again, or when
706 * the pending call is finalized. The slot number
707 * must have been allocated with dbus_pending_call_allocate_data_slot().
709 * @param pending the pending_call
710 * @param slot the slot number
711 * @param data the data to store
712 * @param free_data_func finalizer function for the data
713 * @returns #TRUE if there was enough memory to store the data
716 _dbus_pending_call_set_data_unlocked (DBusPendingCall *pending,
719 DBusFreeFunction free_data_func)
721 DBusFreeFunction old_free_func;
725 retval = _dbus_data_slot_list_set (&slot_allocator,
727 slot, data, free_data_func,
728 &old_free_func, &old_data);
730 /* Drop locks to call out to app code */
731 CONNECTION_UNLOCK (pending->connection);
736 (* old_free_func) (old_data);
739 CONNECTION_LOCK (pending->connection);
745 * Stores a pointer on a #DBusPendingCall, along
746 * with an optional function to be used for freeing
747 * the data when the data is set again, or when
748 * the pending call is finalized. The slot number
749 * must have been allocated with dbus_pending_call_allocate_data_slot().
751 * @param pending the pending_call
752 * @param slot the slot number
753 * @param data the data to store
754 * @param free_data_func finalizer function for the data
755 * @returns #TRUE if there was enough memory to store the data
758 dbus_pending_call_set_data (DBusPendingCall *pending,
761 DBusFreeFunction free_data_func)
765 _dbus_return_val_if_fail (pending != NULL, FALSE);
766 _dbus_return_val_if_fail (slot >= 0, FALSE);
769 CONNECTION_LOCK (pending->connection);
770 retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
771 CONNECTION_UNLOCK (pending->connection);
776 * Retrieves data previously set with dbus_pending_call_set_data().
777 * The slot must still be allocated (must not have been freed).
779 * @param pending the pending_call
780 * @param slot the slot to get data from
781 * @returns the data, or #NULL if not found
784 dbus_pending_call_get_data (DBusPendingCall *pending,
789 _dbus_return_val_if_fail (pending != NULL, NULL);
791 CONNECTION_LOCK (pending->connection);
792 res = _dbus_data_slot_list_get (&slot_allocator,
795 CONNECTION_UNLOCK (pending->connection);
802 #ifdef DBUS_BUILD_TESTS
805 * @ingroup DBusPendingCallInternals
806 * Unit test for DBusPendingCall.
808 * @returns #TRUE on success.
811 _dbus_pending_call_test (const char *test_data_dir)
816 #endif /* DBUS_BUILD_TESTS */