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-pending-call-internal.h"
28 #include "dbus-pending-call.h"
29 #include "dbus-list.h"
30 #include "dbus-threads.h"
31 #include "dbus-test.h"
34 * @defgroup DBusPendingCallInternals DBusPendingCall implementation details
35 * @ingroup DBusInternals
36 * @brief DBusPendingCall private implementation details.
38 * The guts of DBusPendingCall and its methods.
44 * @brief Internals of DBusPendingCall
46 * Opaque object representing a reply message that we're waiting for.
50 * shorter and more visible way to write _dbus_connection_lock()
52 #define CONNECTION_LOCK(connection) _dbus_connection_lock(connection)
54 * shorter and more visible way to write _dbus_connection_unlock()
56 #define CONNECTION_UNLOCK(connection) _dbus_connection_unlock(connection)
59 * Implementation details of #DBusPendingCall - all fields are private.
61 struct DBusPendingCall
63 DBusAtomic refcount; /**< reference count */
65 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
67 DBusPendingCallNotifyFunction function; /**< Notifier when reply arrives. */
69 DBusConnection *connection; /**< Connections we're associated with */
70 DBusMessage *reply; /**< Reply (after we've received it) */
71 DBusTimeout *timeout; /**< Timeout */
73 DBusList *timeout_link; /**< Preallocated timeout response */
75 dbus_uint32_t reply_serial; /**< Expected serial of reply */
77 unsigned int completed : 1; /**< TRUE if completed */
78 unsigned int timeout_added : 1; /**< Have added the timeout */
81 static dbus_int32_t notify_user_data_slot = -1;
84 * Creates a new pending reply object.
86 * @param connection connection where reply will arrive
87 * @param timeout_milliseconds length of timeout, -1 (or
88 * #DBUS_TIMEOUT_USE_DEFAULT) for default,
89 * #DBUS_TIMEOUT_INFINITE for no timeout
90 * @param timeout_handler timeout handler, takes pending call as data
91 * @returns a new #DBusPendingCall or #NULL if no memory.
94 _dbus_pending_call_new_unlocked (DBusConnection *connection,
95 int timeout_milliseconds,
96 DBusTimeoutHandler timeout_handler)
98 DBusPendingCall *pending;
101 _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
103 if (timeout_milliseconds == -1)
104 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
106 if (!dbus_pending_call_allocate_data_slot (¬ify_user_data_slot))
109 pending = dbus_new0 (DBusPendingCall, 1);
113 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
117 if (timeout_milliseconds != DBUS_TIMEOUT_INFINITE)
119 timeout = _dbus_timeout_new (timeout_milliseconds,
125 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
130 pending->timeout = timeout;
134 pending->timeout = NULL;
137 _dbus_atomic_inc (&pending->refcount);
138 pending->connection = connection;
139 _dbus_connection_ref_unlocked (pending->connection);
141 _dbus_data_slot_list_init (&pending->slot_list);
147 * Sets the reply of a pending call with the given message,
148 * or if the message is #NULL, by timing out the pending call.
150 * @param pending the pending call
151 * @param message the message to complete the call with, or #NULL
152 * to time out the call
155 _dbus_pending_call_set_reply_unlocked (DBusPendingCall *pending,
156 DBusMessage *message)
160 message = pending->timeout_link->data;
161 _dbus_list_clear (&pending->timeout_link);
164 dbus_message_ref (message);
166 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
168 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
170 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
171 "error" : "other type",
172 pending->reply_serial);
174 _dbus_assert (pending->reply == NULL);
175 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
176 pending->reply = message;
180 * Calls notifier function for the pending call
181 * and sets the call to completed.
183 * @param pending the pending call
187 _dbus_pending_call_complete (DBusPendingCall *pending)
189 _dbus_assert (!pending->completed);
191 pending->completed = TRUE;
193 if (pending->function)
196 user_data = dbus_pending_call_get_data (pending,
197 notify_user_data_slot);
199 (* pending->function) (pending, user_data);
204 * If the pending call hasn't been timed out, add its timeout
205 * error reply to the connection's incoming message queue.
207 * @param pending the pending call
208 * @param connection the connection the call was sent to
211 _dbus_pending_call_queue_timeout_error_unlocked (DBusPendingCall *pending,
212 DBusConnection *connection)
214 _dbus_assert (connection == pending->connection);
216 if (pending->timeout_link)
218 _dbus_connection_queue_synthesized_message_link (connection,
219 pending->timeout_link);
220 pending->timeout_link = NULL;
225 * Checks to see if a timeout has been added
227 * @param pending the pending_call
228 * @returns #TRUE if there is a timeout or #FALSE if not
231 _dbus_pending_call_is_timeout_added_unlocked (DBusPendingCall *pending)
233 _dbus_assert (pending != NULL);
235 return pending->timeout_added;
240 * Sets wether the timeout has been added
242 * @param pending the pending_call
243 * @param is_added whether or not a timeout is added
246 _dbus_pending_call_set_timeout_added_unlocked (DBusPendingCall *pending,
247 dbus_bool_t is_added)
249 _dbus_assert (pending != NULL);
251 pending->timeout_added = is_added;
256 * Retrives the timeout
258 * @param pending the pending_call
259 * @returns a timeout object or NULL if call has no timeout
262 _dbus_pending_call_get_timeout_unlocked (DBusPendingCall *pending)
264 _dbus_assert (pending != NULL);
266 return pending->timeout;
270 * Gets the reply's serial number
272 * @param pending the pending_call
273 * @returns a serial number for the reply or 0
276 _dbus_pending_call_get_reply_serial_unlocked (DBusPendingCall *pending)
278 _dbus_assert (pending != NULL);
280 return pending->reply_serial;
284 * Sets the reply's serial number
286 * @param pending the pending_call
287 * @param serial the serial number
290 _dbus_pending_call_set_reply_serial_unlocked (DBusPendingCall *pending,
291 dbus_uint32_t serial)
293 _dbus_assert (pending != NULL);
294 _dbus_assert (pending->reply_serial == 0);
296 pending->reply_serial = serial;
300 * Gets the connection associated with this pending call.
302 * @param pending the pending_call
303 * @returns the connection associated with the pending call
306 _dbus_pending_call_get_connection_and_lock (DBusPendingCall *pending)
308 _dbus_assert (pending != NULL);
310 CONNECTION_LOCK (pending->connection);
311 return pending->connection;
315 * Gets the connection associated with this pending call.
317 * @param pending the pending_call
318 * @returns the connection associated with the pending call
321 _dbus_pending_call_get_connection_unlocked (DBusPendingCall *pending)
323 _dbus_assert (pending != NULL);
325 return pending->connection;
329 * Sets the reply message associated with the pending call to a timeout error
331 * @param pending the pending_call
332 * @param message the message we are sending the error reply to
333 * @param serial serial number for the reply
334 * @return #FALSE on OOM
337 _dbus_pending_call_set_timeout_error_unlocked (DBusPendingCall *pending,
338 DBusMessage *message,
339 dbus_uint32_t serial)
341 DBusList *reply_link;
344 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
345 "Did not receive a reply. Possible causes include: "
346 "the remote application did not send a reply, "
347 "the message bus security policy blocked the reply, "
348 "the reply timeout expired, or "
349 "the network connection was broken.");
353 reply_link = _dbus_list_alloc_link (reply);
354 if (reply_link == NULL)
356 /* it's OK to unref this, nothing that could have attached a callback
357 * has ever seen it */
358 dbus_message_unref (reply);
362 pending->timeout_link = reply_link;
364 _dbus_pending_call_set_reply_serial_unlocked (pending, serial);
370 * Increments the reference count on a pending call,
371 * while the lock on its connection is already held.
373 * @param pending the pending call object
374 * @returns the pending call object
377 _dbus_pending_call_ref_unlocked (DBusPendingCall *pending)
379 _dbus_atomic_inc (&pending->refcount);
386 _dbus_pending_call_last_unref (DBusPendingCall *pending)
388 DBusConnection *connection;
390 /* If we get here, we should be already detached
391 * from the connection, or never attached.
393 _dbus_assert (!pending->timeout_added);
395 connection = pending->connection;
397 /* this assumes we aren't holding connection lock... */
398 _dbus_data_slot_list_free (&pending->slot_list);
400 if (pending->timeout != NULL)
401 _dbus_timeout_unref (pending->timeout);
403 if (pending->timeout_link)
405 dbus_message_unref ((DBusMessage *)pending->timeout_link->data);
406 _dbus_list_free_link (pending->timeout_link);
407 pending->timeout_link = NULL;
412 dbus_message_unref (pending->reply);
413 pending->reply = NULL;
418 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
420 /* connection lock should not be held. */
421 /* Free the connection last to avoid a weird state while
422 * calling out to application code where the pending exists
423 * but not the connection.
425 dbus_connection_unref (connection);
429 * Decrements the reference count on a pending call,
430 * freeing it if the count reaches 0. Assumes
431 * connection lock is already held.
433 * @param pending the pending call object
436 _dbus_pending_call_unref_and_unlock (DBusPendingCall *pending)
438 dbus_int32_t old_refcount;
440 old_refcount = _dbus_atomic_dec (&pending->refcount);
441 _dbus_assert (old_refcount > 0);
443 CONNECTION_UNLOCK (pending->connection);
445 if (old_refcount == 1)
446 _dbus_pending_call_last_unref (pending);
450 * Checks whether the pending call has received a reply
451 * yet, or not. Assumes connection lock is held.
453 * @param pending the pending call
454 * @returns #TRUE if a reply has been received
457 _dbus_pending_call_get_completed_unlocked (DBusPendingCall *pending)
459 return pending->completed;
462 static DBusDataSlotAllocator slot_allocator;
463 _DBUS_DEFINE_GLOBAL_LOCK (pending_call_slots);
466 * Stores a pointer on a #DBusPendingCall, along
467 * with an optional function to be used for freeing
468 * the data when the data is set again, or when
469 * the pending call is finalized. The slot number
470 * must have been allocated with dbus_pending_call_allocate_data_slot().
472 * @param pending the pending_call
473 * @param slot the slot number
474 * @param data the data to store
475 * @param free_data_func finalizer function for the data
476 * @returns #TRUE if there was enough memory to store the data
479 _dbus_pending_call_set_data_unlocked (DBusPendingCall *pending,
482 DBusFreeFunction free_data_func)
484 DBusFreeFunction old_free_func;
488 retval = _dbus_data_slot_list_set (&slot_allocator,
490 slot, data, free_data_func,
491 &old_free_func, &old_data);
493 /* Drop locks to call out to app code */
494 CONNECTION_UNLOCK (pending->connection);
499 (* old_free_func) (old_data);
502 CONNECTION_LOCK (pending->connection);
510 * @defgroup DBusPendingCall DBusPendingCall
512 * @brief Pending reply to a method call message
514 * A DBusPendingCall is an object representing an
515 * expected reply. A #DBusPendingCall can be created
516 * when you send a message that should have a reply.
522 * @def DBUS_TIMEOUT_INFINITE
524 * An integer constant representing an infinite timeout. This has the
525 * numeric value 0x7fffffff (the largest 32-bit signed integer).
527 * For source compatibility with D-Bus versions earlier than 1.4.12, use
528 * 0x7fffffff, or INT32_MAX (assuming your platform has it).
532 * @def DBUS_TIMEOUT_USE_DEFAULT
534 * An integer constant representing a request to use the default timeout.
535 * This has numeric value -1.
537 * For source compatibility with D-Bus versions earlier than 1.4.12, use a
542 * @typedef DBusPendingCall
544 * Opaque data type representing a message pending.
548 * Increments the reference count on a pending call.
550 * @param pending the pending call object
551 * @returns the pending call object
554 dbus_pending_call_ref (DBusPendingCall *pending)
556 _dbus_return_val_if_fail (pending != NULL, NULL);
558 _dbus_atomic_inc (&pending->refcount);
564 * Decrements the reference count on a pending call,
565 * freeing it if the count reaches 0.
567 * @param pending the pending call object
570 dbus_pending_call_unref (DBusPendingCall *pending)
572 dbus_bool_t last_unref;
574 _dbus_return_if_fail (pending != NULL);
576 last_unref = (_dbus_atomic_dec (&pending->refcount) == 1);
579 _dbus_pending_call_last_unref(pending);
583 * Sets a notification function to be called when the reply is
584 * received or the pending call times out.
586 * @param pending the pending call
587 * @param function notifier function
588 * @param user_data data to pass to notifier function
589 * @param free_user_data function to free the user data
590 * @returns #FALSE if not enough memory
593 dbus_pending_call_set_notify (DBusPendingCall *pending,
594 DBusPendingCallNotifyFunction function,
596 DBusFreeFunction free_user_data)
598 _dbus_return_val_if_fail (pending != NULL, FALSE);
600 CONNECTION_LOCK (pending->connection);
602 /* could invoke application code! */
603 if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
604 user_data, free_user_data))
607 pending->function = function;
609 CONNECTION_UNLOCK (pending->connection);
615 * Cancels the pending call, such that any reply or error received
616 * will just be ignored. Drops the dbus library's internal reference
617 * to the #DBusPendingCall so will free the call if nobody else is
618 * holding a reference. However you usually get a reference from
619 * dbus_connection_send_with_reply() so probably your app owns a ref
622 * Note that canceling a pending call will <em>not</em> simulate a
623 * timed-out call; if a call times out, then a timeout error reply is
624 * received. If you cancel the call, no reply is received unless the
625 * the reply was already received before you canceled.
627 * @param pending the pending call
630 dbus_pending_call_cancel (DBusPendingCall *pending)
632 _dbus_return_if_fail (pending != NULL);
634 _dbus_connection_remove_pending_call (pending->connection,
639 * Checks whether the pending call has received a reply
642 * @param pending the pending call
643 * @returns #TRUE if a reply has been received
646 dbus_pending_call_get_completed (DBusPendingCall *pending)
648 dbus_bool_t completed;
650 _dbus_return_val_if_fail (pending != NULL, FALSE);
652 CONNECTION_LOCK (pending->connection);
653 completed = pending->completed;
654 CONNECTION_UNLOCK (pending->connection);
660 * Gets the reply, or returns #NULL if none has been received
661 * yet. Ownership of the reply message passes to the caller. This
662 * function can only be called once per pending call, since the reply
663 * message is tranferred to the caller.
665 * @param pending the pending call
666 * @returns the reply message or #NULL.
669 dbus_pending_call_steal_reply (DBusPendingCall *pending)
671 DBusMessage *message;
673 _dbus_return_val_if_fail (pending != NULL, NULL);
674 _dbus_return_val_if_fail (pending->completed, NULL);
675 _dbus_return_val_if_fail (pending->reply != NULL, NULL);
677 CONNECTION_LOCK (pending->connection);
679 message = pending->reply;
680 pending->reply = NULL;
682 CONNECTION_UNLOCK (pending->connection);
688 * Block until the pending call is completed. The blocking is as with
689 * dbus_connection_send_with_reply_and_block(); it does not enter the
690 * main loop or process other messages, it simply waits for the reply
693 * If the pending call is already completed, this function returns
696 * @todo when you start blocking, the timeout is reset, but it should
697 * really only use time remaining since the pending call was created.
698 * This requires storing timestamps instead of intervals in the timeout
700 * @param pending the pending call
703 dbus_pending_call_block (DBusPendingCall *pending)
705 _dbus_return_if_fail (pending != NULL);
707 _dbus_connection_block_pending_call (pending);
711 * Allocates an integer ID to be used for storing application-specific
712 * data on any DBusPendingCall. The allocated ID may then be used
713 * with dbus_pending_call_set_data() and dbus_pending_call_get_data().
714 * The passed-in slot must be initialized to -1, and is filled in
715 * with the slot ID. If the passed-in slot is not -1, it's assumed
716 * to be already allocated, and its refcount is incremented.
718 * The allocated slot is global, i.e. all DBusPendingCall objects will
719 * have a slot with the given integer ID reserved.
721 * @param slot_p address of a global variable storing the slot
722 * @returns #FALSE on failure (no memory)
725 dbus_pending_call_allocate_data_slot (dbus_int32_t *slot_p)
727 _dbus_return_val_if_fail (slot_p != NULL, FALSE);
729 return _dbus_data_slot_allocator_alloc (&slot_allocator,
730 &_DBUS_LOCK_NAME (pending_call_slots),
735 * Deallocates a global ID for #DBusPendingCall data slots.
736 * dbus_pending_call_get_data() and dbus_pending_call_set_data() may
737 * no longer be used with this slot. Existing data stored on existing
738 * DBusPendingCall objects will be freed when the #DBusPendingCall is
739 * finalized, but may not be retrieved (and may only be replaced if
740 * someone else reallocates the slot). When the refcount on the
741 * passed-in slot reaches 0, it is set to -1.
743 * @param slot_p address storing the slot to deallocate
746 dbus_pending_call_free_data_slot (dbus_int32_t *slot_p)
748 _dbus_return_if_fail (slot_p != NULL);
749 _dbus_return_if_fail (*slot_p >= 0);
751 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
755 * Stores a pointer on a #DBusPendingCall, along
756 * with an optional function to be used for freeing
757 * the data when the data is set again, or when
758 * the pending call is finalized. The slot number
759 * must have been allocated with dbus_pending_call_allocate_data_slot().
761 * @param pending the pending_call
762 * @param slot the slot number
763 * @param data the data to store
764 * @param free_data_func finalizer function for the data
765 * @returns #TRUE if there was enough memory to store the data
768 dbus_pending_call_set_data (DBusPendingCall *pending,
771 DBusFreeFunction free_data_func)
775 _dbus_return_val_if_fail (pending != NULL, FALSE);
776 _dbus_return_val_if_fail (slot >= 0, FALSE);
779 CONNECTION_LOCK (pending->connection);
780 retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
781 CONNECTION_UNLOCK (pending->connection);
786 * Retrieves data previously set with dbus_pending_call_set_data().
787 * The slot must still be allocated (must not have been freed).
789 * @param pending the pending_call
790 * @param slot the slot to get data from
791 * @returns the data, or #NULL if not found
794 dbus_pending_call_get_data (DBusPendingCall *pending,
799 _dbus_return_val_if_fail (pending != NULL, NULL);
801 CONNECTION_LOCK (pending->connection);
802 res = _dbus_data_slot_list_get (&slot_allocator,
805 CONNECTION_UNLOCK (pending->connection);
812 #ifdef DBUS_BUILD_TESTS
815 * @ingroup DBusPendingCallInternals
816 * Unit test for DBusPendingCall.
818 * @returns #TRUE on success.
821 _dbus_pending_call_test (const char *test_data_dir)
826 #endif /* DBUS_BUILD_TESTS */