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.
47 #define CONNECTION_LOCK(connection) _dbus_connection_lock(connection)
48 #define CONNECTION_UNLOCK(connection) _dbus_connection_unlock(connection)
50 struct DBusPendingCall
52 DBusAtomic refcount; /**< reference count */
54 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
56 DBusPendingCallNotifyFunction function; /**< Notifier when reply arrives. */
58 DBusConnection *connection; /**< Connections we're associated with */
59 DBusMessage *reply; /**< Reply (after we've received it) */
60 DBusTimeout *timeout; /**< Timeout */
62 DBusList *timeout_link; /**< Preallocated timeout response */
64 dbus_uint32_t reply_serial; /**< Expected serial of reply */
66 unsigned int completed : 1; /**< TRUE if completed */
67 unsigned int timeout_added : 1; /**< Have added the timeout */
70 static dbus_int32_t notify_user_data_slot = -1;
73 * Creates a new pending reply object.
75 * @param connection connection where reply will arrive
76 * @param timeout_milliseconds length of timeout, -1 for default
77 * @param timeout_handler timeout handler, takes pending call as data
78 * @returns a new #DBusPendingCall or #NULL if no memory.
81 _dbus_pending_call_new_unlocked (DBusConnection *connection,
82 int timeout_milliseconds,
83 DBusTimeoutHandler timeout_handler)
85 DBusPendingCall *pending;
88 _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
90 if (timeout_milliseconds == -1)
91 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
93 /* it would probably seem logical to pass in _DBUS_INT_MAX for
94 * infinite timeout, but then math in
95 * _dbus_connection_block_for_reply would get all overflow-prone, so
98 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
99 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
101 if (!dbus_pending_call_allocate_data_slot (¬ify_user_data_slot))
104 pending = dbus_new0 (DBusPendingCall, 1);
108 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
112 timeout = _dbus_timeout_new (timeout_milliseconds,
118 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
123 pending->refcount.value = 1;
124 pending->connection = connection;
125 _dbus_connection_ref_unlocked (pending->connection);
127 pending->timeout = timeout;
130 _dbus_data_slot_list_init (&pending->slot_list);
136 * Sets the reply of a pending call with the given message,
137 * or if the message is #NULL, by timing out the pending call.
139 * @param pending the pending call
140 * @param message the message to complete the call with, or #NULL
141 * to time out the call
144 _dbus_pending_call_set_reply_unlocked (DBusPendingCall *pending,
145 DBusMessage *message)
149 message = pending->timeout_link->data;
150 _dbus_list_clear (&pending->timeout_link);
153 dbus_message_ref (message);
155 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
157 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
159 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
160 "error" : "other type",
161 pending->reply_serial);
163 _dbus_assert (pending->reply == NULL);
164 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
165 pending->reply = message;
169 * Calls notifier function for the pending call
170 * and sets the call to completed.
172 * @param pending the pending call
176 _dbus_pending_call_complete (DBusPendingCall *pending)
178 _dbus_assert (!pending->completed);
180 pending->completed = TRUE;
182 if (pending->function)
185 user_data = dbus_pending_call_get_data (pending,
186 notify_user_data_slot);
188 (* pending->function) (pending, user_data);
193 _dbus_pending_call_queue_timeout_error_unlocked (DBusPendingCall *pending,
194 DBusConnection *connection)
196 _dbus_assert (connection == pending->connection);
198 if (pending->timeout_link)
200 _dbus_connection_queue_synthesized_message_link (connection,
201 pending->timeout_link);
202 pending->timeout_link = NULL;
207 * Checks to see if a timeout has been added
209 * @param pending the pending_call
210 * @returns #TRUE if there is a timeout or #FALSE if not
213 _dbus_pending_call_is_timeout_added_unlocked (DBusPendingCall *pending)
215 _dbus_assert (pending != NULL);
217 return pending->timeout_added;
222 * Sets wether the timeout has been added
224 * @param pending the pending_call
225 * @param is_added whether or not a timeout is added
228 _dbus_pending_call_set_timeout_added_unlocked (DBusPendingCall *pending,
229 dbus_bool_t is_added)
231 _dbus_assert (pending != NULL);
233 pending->timeout_added = is_added;
238 * Retrives the timeout
240 * @param pending the pending_call
241 * @returns a timeout object
244 _dbus_pending_call_get_timeout_unlocked (DBusPendingCall *pending)
246 _dbus_assert (pending != NULL);
248 return pending->timeout;
252 * Gets the reply's serial number
254 * @param pending the pending_call
255 * @returns a serial number for the reply or 0
258 _dbus_pending_call_get_reply_serial_unlocked (DBusPendingCall *pending)
260 _dbus_assert (pending != NULL);
262 return pending->reply_serial;
266 * Sets the reply's serial number
268 * @param pending the pending_call
269 * @param serial the serial number
272 _dbus_pending_call_set_reply_serial_unlocked (DBusPendingCall *pending,
273 dbus_uint32_t serial)
275 _dbus_assert (pending != NULL);
276 _dbus_assert (pending->reply_serial == 0);
278 pending->reply_serial = serial;
282 * Gets the connection associated with this pending call.
284 * @param pending the pending_call
285 * @returns the connection associated with the pending call
288 _dbus_pending_call_get_connection_and_lock (DBusPendingCall *pending)
290 _dbus_assert (pending != NULL);
292 CONNECTION_LOCK (pending->connection);
293 return pending->connection;
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_unlocked (DBusPendingCall *pending)
305 _dbus_assert (pending != NULL);
307 return pending->connection;
311 * Sets the reply message associated with the pending call to a timeout error
313 * @param pending the pending_call
314 * @param message the message we are sending the error reply to
315 * @param serial serial number for the reply
316 * @return #FALSE on OOM
319 _dbus_pending_call_set_timeout_error_unlocked (DBusPendingCall *pending,
320 DBusMessage *message,
321 dbus_uint32_t serial)
323 DBusList *reply_link;
326 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
327 "Did not receive a reply. Possible causes include: "
328 "the remote application did not send a reply, "
329 "the message bus security policy blocked the reply, "
330 "the reply timeout expired, or "
331 "the network connection was broken.");
335 reply_link = _dbus_list_alloc_link (reply);
336 if (reply_link == NULL)
338 dbus_message_unref (reply);
342 pending->timeout_link = reply_link;
344 _dbus_pending_call_set_reply_serial_unlocked (pending, serial);
352 * @defgroup DBusPendingCall DBusPendingCall
354 * @brief Pending reply to a method call message
356 * A DBusPendingCall is an object representing an
357 * expected reply. A #DBusPendingCall can be created
358 * when you send a message that should have a reply.
364 * @typedef DBusPendingCall
366 * Opaque data type representing a message pending.
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 pending->refcount.value += 1;
385 * Increments the reference count on a pending call.
387 * @param pending the pending call object
388 * @returns the pending call object
391 dbus_pending_call_ref (DBusPendingCall *pending)
393 _dbus_return_val_if_fail (pending != NULL, NULL);
395 /* The connection lock is better than the global
396 * lock in the atomic increment fallback
398 #ifdef DBUS_HAVE_ATOMIC_INT
399 _dbus_atomic_inc (&pending->refcount);
401 CONNECTION_LOCK (pending->connection);
402 _dbus_assert (pending->refcount.value > 0);
404 pending->refcount.value += 1;
405 CONNECTION_UNLOCK (pending->connection);
412 _dbus_pending_call_last_unref (DBusPendingCall *pending)
414 DBusConnection *connection;
416 /* If we get here, we should be already detached
417 * from the connection, or never attached.
419 _dbus_assert (!pending->timeout_added);
421 connection = pending->connection;
423 /* this assumes we aren't holding connection lock... */
424 _dbus_data_slot_list_free (&pending->slot_list);
426 if (pending->timeout != NULL)
427 _dbus_timeout_unref (pending->timeout);
429 if (pending->timeout_link)
431 dbus_message_unref ((DBusMessage *)pending->timeout_link->data);
432 _dbus_list_free_link (pending->timeout_link);
433 pending->timeout_link = NULL;
438 dbus_message_unref (pending->reply);
439 pending->reply = NULL;
444 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
446 /* connection lock should not be held. */
447 /* Free the connection last to avoid a weird state while
448 * calling out to application code where the pending exists
449 * but not the connection.
451 dbus_connection_unref (connection);
455 * Decrements the reference count on a pending call,
456 * freeing it if the count reaches 0. Assumes
457 * connection lock is already held.
459 * @param pending the pending call object
462 _dbus_pending_call_unref_and_unlock (DBusPendingCall *pending)
464 dbus_bool_t last_unref;
466 _dbus_assert (pending->refcount.value > 0);
468 pending->refcount.value -= 1;
469 last_unref = pending->refcount.value == 0;
471 CONNECTION_UNLOCK (pending->connection);
473 _dbus_pending_call_last_unref (pending);
477 * Decrements the reference count on a pending call,
478 * freeing it if the count reaches 0.
480 * @param pending the pending call object
483 dbus_pending_call_unref (DBusPendingCall *pending)
485 dbus_bool_t last_unref;
487 _dbus_return_if_fail (pending != NULL);
489 /* More efficient to use the connection lock instead of atomic
490 * int fallback if we lack atomic int decrement
492 #ifdef DBUS_HAVE_ATOMIC_INT
493 last_unref = (_dbus_atomic_dec (&pending->refcount) == 1);
495 CONNECTION_LOCK (pending->connection);
496 _dbus_assert (pending->refcount.value > 0);
497 pending->refcount.value -= 1;
498 last_unref = pending->refcount.value == 0;
499 CONNECTION_UNLOCK (pending->connection);
503 _dbus_pending_call_last_unref(pending);
507 * Sets a notification function to be called when the reply is
508 * received or the pending call times out.
510 * @param pending the pending call
511 * @param function notifier function
512 * @param user_data data to pass to notifier function
513 * @param free_user_data function to free the user data
514 * @returns #FALSE if not enough memory
517 dbus_pending_call_set_notify (DBusPendingCall *pending,
518 DBusPendingCallNotifyFunction function,
520 DBusFreeFunction free_user_data)
522 _dbus_return_val_if_fail (pending != NULL, FALSE);
524 CONNECTION_LOCK (pending->connection);
526 /* could invoke application code! */
527 if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
528 user_data, free_user_data))
531 pending->function = function;
533 CONNECTION_UNLOCK (pending->connection);
539 * Cancels the pending call, such that any reply or error received
540 * will just be ignored. Drops the dbus library's internal reference
541 * to the #DBusPendingCall so will free the call if nobody else is
542 * holding a reference. However you usually get a reference
543 * from dbus_connection_send() so probably your app owns a ref also.
545 * @param pending the pending call
548 dbus_pending_call_cancel (DBusPendingCall *pending)
550 _dbus_return_if_fail (pending != NULL);
552 _dbus_connection_remove_pending_call (pending->connection,
557 * Checks whether the pending call has received a reply
558 * yet, or not. Assumes connection lock is held.
560 * @param pending the pending call
561 * @returns #TRUE if a reply has been received
564 _dbus_pending_call_get_completed_unlocked (DBusPendingCall *pending)
566 return pending->completed;
570 * Checks whether the pending call has received a reply
573 * @param pending the pending call
574 * @returns #TRUE if a reply has been received
577 dbus_pending_call_get_completed (DBusPendingCall *pending)
579 dbus_bool_t completed;
581 _dbus_return_val_if_fail (pending != NULL, FALSE);
583 CONNECTION_LOCK (pending->connection);
584 completed = pending->completed;
585 CONNECTION_UNLOCK (pending->connection);
591 * Gets the reply, or returns #NULL if none has been received
592 * yet. Ownership of the reply message passes to the caller. This
593 * function can only be called once per pending call, since the reply
594 * message is tranferred to the caller.
596 * @param pending the pending call
597 * @returns the reply message or #NULL.
600 dbus_pending_call_steal_reply (DBusPendingCall *pending)
602 DBusMessage *message;
604 _dbus_return_val_if_fail (pending != NULL, NULL);
605 _dbus_return_val_if_fail (pending->completed, NULL);
606 _dbus_return_val_if_fail (pending->reply != NULL, NULL);
608 CONNECTION_LOCK (pending->connection);
610 message = pending->reply;
611 pending->reply = NULL;
613 CONNECTION_UNLOCK (pending->connection);
619 * Block until the pending call is completed. The blocking is as with
620 * dbus_connection_send_with_reply_and_block(); it does not enter the
621 * main loop or process other messages, it simply waits for the reply
624 * If the pending call is already completed, this function returns
627 * @todo when you start blocking, the timeout is reset, but it should
628 * really only use time remaining since the pending call was created.
629 * This requires storing timestamps instead of intervals in the timeout
631 * @param pending the pending call
634 dbus_pending_call_block (DBusPendingCall *pending)
636 _dbus_return_if_fail (pending != NULL);
638 _dbus_connection_block_pending_call (pending);
641 static DBusDataSlotAllocator slot_allocator;
642 _DBUS_DEFINE_GLOBAL_LOCK (pending_call_slots);
645 * Allocates an integer ID to be used for storing application-specific
646 * data on any DBusPendingCall. The allocated ID may then be used
647 * with dbus_pending_call_set_data() and dbus_pending_call_get_data().
648 * The passed-in slot must be initialized to -1, and is filled in
649 * with the slot ID. If the passed-in slot is not -1, it's assumed
650 * to be already allocated, and its refcount is incremented.
652 * The allocated slot is global, i.e. all DBusPendingCall objects will
653 * have a slot with the given integer ID reserved.
655 * @param slot_p address of a global variable storing the slot
656 * @returns #FALSE on failure (no memory)
659 dbus_pending_call_allocate_data_slot (dbus_int32_t *slot_p)
661 _dbus_return_val_if_fail (slot_p != NULL, FALSE);
663 return _dbus_data_slot_allocator_alloc (&slot_allocator,
664 &_DBUS_LOCK_NAME (pending_call_slots),
669 * Deallocates a global ID for #DBusPendingCall data slots.
670 * dbus_pending_call_get_data() and dbus_pending_call_set_data() may
671 * no longer be used with this slot. Existing data stored on existing
672 * DBusPendingCall objects will be freed when the #DBusPendingCall is
673 * finalized, but may not be retrieved (and may only be replaced if
674 * someone else reallocates the slot). When the refcount on the
675 * passed-in slot reaches 0, it is set to -1.
677 * @param slot_p address storing the slot to deallocate
680 dbus_pending_call_free_data_slot (dbus_int32_t *slot_p)
682 _dbus_return_if_fail (slot_p != NULL);
683 _dbus_return_if_fail (*slot_p >= 0);
685 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
689 * Stores a pointer on a #DBusPendingCall, along
690 * with an optional function to be used for freeing
691 * the data when the data is set again, or when
692 * the pending call is finalized. The slot number
693 * must have been allocated with dbus_pending_call_allocate_data_slot().
695 * @param pending the pending_call
696 * @param slot the slot number
697 * @param data the data to store
698 * @param free_data_func finalizer function for the data
699 * @returns #TRUE if there was enough memory to store the data
702 _dbus_pending_call_set_data_unlocked (DBusPendingCall *pending,
705 DBusFreeFunction free_data_func)
707 DBusFreeFunction old_free_func;
711 retval = _dbus_data_slot_list_set (&slot_allocator,
713 slot, data, free_data_func,
714 &old_free_func, &old_data);
716 /* Drop locks to call out to app code */
717 CONNECTION_UNLOCK (pending->connection);
722 (* old_free_func) (old_data);
725 CONNECTION_LOCK (pending->connection);
731 * Stores a pointer on a #DBusPendingCall, along
732 * with an optional function to be used for freeing
733 * the data when the data is set again, or when
734 * the pending call is finalized. The slot number
735 * must have been allocated with dbus_pending_call_allocate_data_slot().
737 * @param pending the pending_call
738 * @param slot the slot number
739 * @param data the data to store
740 * @param free_data_func finalizer function for the data
741 * @returns #TRUE if there was enough memory to store the data
744 dbus_pending_call_set_data (DBusPendingCall *pending,
747 DBusFreeFunction free_data_func)
751 _dbus_return_val_if_fail (pending != NULL, FALSE);
752 _dbus_return_val_if_fail (slot >= 0, FALSE);
755 CONNECTION_LOCK (pending->connection);
756 retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
757 CONNECTION_UNLOCK (pending->connection);
762 * Retrieves data previously set with dbus_pending_call_set_data().
763 * The slot must still be allocated (must not have been freed).
765 * @param pending the pending_call
766 * @param slot the slot to get data from
767 * @returns the data, or #NULL if not found
770 dbus_pending_call_get_data (DBusPendingCall *pending,
775 _dbus_return_val_if_fail (pending != NULL, NULL);
777 CONNECTION_LOCK (pending->connection);
778 res = _dbus_data_slot_list_get (&slot_allocator,
781 CONNECTION_UNLOCK (pending->connection);
788 #ifdef DBUS_BUILD_TESTS
791 * @ingroup DBusPendingCallInternals
792 * Unit test for DBusPendingCall.
794 * @returns #TRUE on success.
797 _dbus_pending_call_test (const char *test_data_dir)
802 #endif /* DBUS_BUILD_TESTS */