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 for default, INT_MAX for no timeout
88 * @param timeout_handler timeout handler, takes pending call as data
89 * @returns a new #DBusPendingCall or #NULL if no memory.
92 _dbus_pending_call_new_unlocked (DBusConnection *connection,
93 int timeout_milliseconds,
94 DBusTimeoutHandler timeout_handler)
96 DBusPendingCall *pending;
99 _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
101 if (timeout_milliseconds == -1)
102 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
104 if (!dbus_pending_call_allocate_data_slot (¬ify_user_data_slot))
107 pending = dbus_new0 (DBusPendingCall, 1);
111 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
115 if (timeout_milliseconds != _DBUS_INT_MAX)
117 timeout = _dbus_timeout_new (timeout_milliseconds,
123 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
128 pending->timeout = timeout;
132 pending->timeout = NULL;
135 pending->refcount.value = 1;
136 pending->connection = connection;
137 _dbus_connection_ref_unlocked (pending->connection);
139 _dbus_data_slot_list_init (&pending->slot_list);
145 * Sets the reply of a pending call with the given message,
146 * or if the message is #NULL, by timing out the pending call.
148 * @param pending the pending call
149 * @param message the message to complete the call with, or #NULL
150 * to time out the call
153 _dbus_pending_call_set_reply_unlocked (DBusPendingCall *pending,
154 DBusMessage *message)
158 message = pending->timeout_link->data;
159 _dbus_list_clear (&pending->timeout_link);
162 dbus_message_ref (message);
164 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
166 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
168 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
169 "error" : "other type",
170 pending->reply_serial);
172 _dbus_assert (pending->reply == NULL);
173 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
174 pending->reply = message;
178 * Calls notifier function for the pending call
179 * and sets the call to completed.
181 * @param pending the pending call
185 _dbus_pending_call_complete (DBusPendingCall *pending)
187 _dbus_assert (!pending->completed);
189 pending->completed = TRUE;
191 if (pending->function)
194 user_data = dbus_pending_call_get_data (pending,
195 notify_user_data_slot);
197 (* pending->function) (pending, user_data);
202 * If the pending call hasn't been timed out, add its timeout
203 * error reply to the connection's incoming message queue.
205 * @param pending the pending call
206 * @param connection the connection the call was sent to
209 _dbus_pending_call_queue_timeout_error_unlocked (DBusPendingCall *pending,
210 DBusConnection *connection)
212 _dbus_assert (connection == pending->connection);
214 if (pending->timeout_link)
216 _dbus_connection_queue_synthesized_message_link (connection,
217 pending->timeout_link);
218 pending->timeout_link = NULL;
223 * Checks to see if a timeout has been added
225 * @param pending the pending_call
226 * @returns #TRUE if there is a timeout or #FALSE if not
229 _dbus_pending_call_is_timeout_added_unlocked (DBusPendingCall *pending)
231 _dbus_assert (pending != NULL);
233 return pending->timeout_added;
238 * Sets wether the timeout has been added
240 * @param pending the pending_call
241 * @param is_added whether or not a timeout is added
244 _dbus_pending_call_set_timeout_added_unlocked (DBusPendingCall *pending,
245 dbus_bool_t is_added)
247 _dbus_assert (pending != NULL);
249 pending->timeout_added = is_added;
254 * Retrives the timeout
256 * @param pending the pending_call
257 * @returns a timeout object or NULL if call has no timeout
260 _dbus_pending_call_get_timeout_unlocked (DBusPendingCall *pending)
262 _dbus_assert (pending != NULL);
264 return pending->timeout;
268 * Gets the reply's serial number
270 * @param pending the pending_call
271 * @returns a serial number for the reply or 0
274 _dbus_pending_call_get_reply_serial_unlocked (DBusPendingCall *pending)
276 _dbus_assert (pending != NULL);
278 return pending->reply_serial;
282 * Sets the reply's serial number
284 * @param pending the pending_call
285 * @param serial the serial number
288 _dbus_pending_call_set_reply_serial_unlocked (DBusPendingCall *pending,
289 dbus_uint32_t serial)
291 _dbus_assert (pending != NULL);
292 _dbus_assert (pending->reply_serial == 0);
294 pending->reply_serial = serial;
298 * Gets the connection associated with this pending call.
300 * @param pending the pending_call
301 * @returns the connection associated with the pending call
304 _dbus_pending_call_get_connection_and_lock (DBusPendingCall *pending)
306 _dbus_assert (pending != NULL);
308 CONNECTION_LOCK (pending->connection);
309 return pending->connection;
313 * Gets the connection associated with this pending call.
315 * @param pending the pending_call
316 * @returns the connection associated with the pending call
319 _dbus_pending_call_get_connection_unlocked (DBusPendingCall *pending)
321 _dbus_assert (pending != NULL);
323 return pending->connection;
327 * Sets the reply message associated with the pending call to a timeout error
329 * @param pending the pending_call
330 * @param message the message we are sending the error reply to
331 * @param serial serial number for the reply
332 * @return #FALSE on OOM
335 _dbus_pending_call_set_timeout_error_unlocked (DBusPendingCall *pending,
336 DBusMessage *message,
337 dbus_uint32_t serial)
339 DBusList *reply_link;
342 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
343 "Did not receive a reply. Possible causes include: "
344 "the remote application did not send a reply, "
345 "the message bus security policy blocked the reply, "
346 "the reply timeout expired, or "
347 "the network connection was broken.");
351 reply_link = _dbus_list_alloc_link (reply);
352 if (reply_link == NULL)
354 dbus_message_unref (reply);
358 pending->timeout_link = reply_link;
360 _dbus_pending_call_set_reply_serial_unlocked (pending, serial);
366 * Increments the reference count on a pending call,
367 * while the lock on its connection is already held.
369 * @param pending the pending call object
370 * @returns the pending call object
373 _dbus_pending_call_ref_unlocked (DBusPendingCall *pending)
375 pending->refcount.value += 1;
382 _dbus_pending_call_last_unref (DBusPendingCall *pending)
384 DBusConnection *connection;
386 /* If we get here, we should be already detached
387 * from the connection, or never attached.
389 _dbus_assert (!pending->timeout_added);
391 connection = pending->connection;
393 /* this assumes we aren't holding connection lock... */
394 _dbus_data_slot_list_free (&pending->slot_list);
396 if (pending->timeout != NULL)
397 _dbus_timeout_unref (pending->timeout);
399 if (pending->timeout_link)
401 dbus_message_unref ((DBusMessage *)pending->timeout_link->data);
402 _dbus_list_free_link (pending->timeout_link);
403 pending->timeout_link = NULL;
408 dbus_message_unref (pending->reply);
409 pending->reply = NULL;
414 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
416 /* connection lock should not be held. */
417 /* Free the connection last to avoid a weird state while
418 * calling out to application code where the pending exists
419 * but not the connection.
421 dbus_connection_unref (connection);
425 * Decrements the reference count on a pending call,
426 * freeing it if the count reaches 0. Assumes
427 * connection lock is already held.
429 * @param pending the pending call object
432 _dbus_pending_call_unref_and_unlock (DBusPendingCall *pending)
434 dbus_bool_t last_unref;
436 _dbus_assert (pending->refcount.value > 0);
438 pending->refcount.value -= 1;
439 last_unref = pending->refcount.value == 0;
441 CONNECTION_UNLOCK (pending->connection);
443 _dbus_pending_call_last_unref (pending);
447 * Checks whether the pending call has received a reply
448 * yet, or not. Assumes connection lock is held.
450 * @param pending the pending call
451 * @returns #TRUE if a reply has been received
454 _dbus_pending_call_get_completed_unlocked (DBusPendingCall *pending)
456 return pending->completed;
459 static DBusDataSlotAllocator slot_allocator;
460 _DBUS_DEFINE_GLOBAL_LOCK (pending_call_slots);
463 * Stores a pointer on a #DBusPendingCall, along
464 * with an optional function to be used for freeing
465 * the data when the data is set again, or when
466 * the pending call is finalized. The slot number
467 * must have been allocated with dbus_pending_call_allocate_data_slot().
469 * @param pending the pending_call
470 * @param slot the slot number
471 * @param data the data to store
472 * @param free_data_func finalizer function for the data
473 * @returns #TRUE if there was enough memory to store the data
476 _dbus_pending_call_set_data_unlocked (DBusPendingCall *pending,
479 DBusFreeFunction free_data_func)
481 DBusFreeFunction old_free_func;
485 retval = _dbus_data_slot_list_set (&slot_allocator,
487 slot, data, free_data_func,
488 &old_free_func, &old_data);
490 /* Drop locks to call out to app code */
491 CONNECTION_UNLOCK (pending->connection);
496 (* old_free_func) (old_data);
499 CONNECTION_LOCK (pending->connection);
507 * @defgroup DBusPendingCall DBusPendingCall
509 * @brief Pending reply to a method call message
511 * A DBusPendingCall is an object representing an
512 * expected reply. A #DBusPendingCall can be created
513 * when you send a message that should have a reply.
519 * @typedef DBusPendingCall
521 * Opaque data type representing a message pending.
525 * Increments the reference count on a pending call.
527 * @param pending the pending call object
528 * @returns the pending call object
531 dbus_pending_call_ref (DBusPendingCall *pending)
533 _dbus_return_val_if_fail (pending != NULL, NULL);
535 /* The connection lock is better than the global
536 * lock in the atomic increment fallback
538 #ifdef DBUS_HAVE_ATOMIC_INT
539 _dbus_atomic_inc (&pending->refcount);
541 CONNECTION_LOCK (pending->connection);
542 _dbus_assert (pending->refcount.value > 0);
544 pending->refcount.value += 1;
545 CONNECTION_UNLOCK (pending->connection);
552 * Decrements the reference count on a pending call,
553 * freeing it if the count reaches 0.
555 * @param pending the pending call object
558 dbus_pending_call_unref (DBusPendingCall *pending)
560 dbus_bool_t last_unref;
562 _dbus_return_if_fail (pending != NULL);
564 /* More efficient to use the connection lock instead of atomic
565 * int fallback if we lack atomic int decrement
567 #ifdef DBUS_HAVE_ATOMIC_INT
568 last_unref = (_dbus_atomic_dec (&pending->refcount) == 1);
570 CONNECTION_LOCK (pending->connection);
571 _dbus_assert (pending->refcount.value > 0);
572 pending->refcount.value -= 1;
573 last_unref = pending->refcount.value == 0;
574 CONNECTION_UNLOCK (pending->connection);
578 _dbus_pending_call_last_unref(pending);
582 * Sets a notification function to be called when the reply is
583 * received or the pending call times out.
585 * @param pending the pending call
586 * @param function notifier function
587 * @param user_data data to pass to notifier function
588 * @param free_user_data function to free the user data
589 * @returns #FALSE if not enough memory
592 dbus_pending_call_set_notify (DBusPendingCall *pending,
593 DBusPendingCallNotifyFunction function,
595 DBusFreeFunction free_user_data)
597 _dbus_return_val_if_fail (pending != NULL, FALSE);
599 CONNECTION_LOCK (pending->connection);
601 /* could invoke application code! */
602 if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
603 user_data, free_user_data))
606 pending->function = function;
608 CONNECTION_UNLOCK (pending->connection);
614 * Cancels the pending call, such that any reply or error received
615 * will just be ignored. Drops the dbus library's internal reference
616 * to the #DBusPendingCall so will free the call if nobody else is
617 * holding a reference. However you usually get a reference from
618 * dbus_connection_send_with_reply() so probably your app owns a ref
621 * Note that canceling a pending call will <em>not</em> simulate a
622 * timed-out call; if a call times out, then a timeout error reply is
623 * received. If you cancel the call, no reply is received unless the
624 * the reply was already received before you canceled.
626 * @param pending the pending call
629 dbus_pending_call_cancel (DBusPendingCall *pending)
631 _dbus_return_if_fail (pending != NULL);
633 _dbus_connection_remove_pending_call (pending->connection,
638 * Checks whether the pending call has received a reply
641 * @param pending the pending call
642 * @returns #TRUE if a reply has been received
645 dbus_pending_call_get_completed (DBusPendingCall *pending)
647 dbus_bool_t completed;
649 _dbus_return_val_if_fail (pending != NULL, FALSE);
651 CONNECTION_LOCK (pending->connection);
652 completed = pending->completed;
653 CONNECTION_UNLOCK (pending->connection);
659 * Gets the reply, or returns #NULL if none has been received
660 * yet. Ownership of the reply message passes to the caller. This
661 * function can only be called once per pending call, since the reply
662 * message is tranferred to the caller.
664 * @param pending the pending call
665 * @returns the reply message or #NULL.
668 dbus_pending_call_steal_reply (DBusPendingCall *pending)
670 DBusMessage *message;
672 _dbus_return_val_if_fail (pending != NULL, NULL);
673 _dbus_return_val_if_fail (pending->completed, NULL);
674 _dbus_return_val_if_fail (pending->reply != NULL, NULL);
676 CONNECTION_LOCK (pending->connection);
678 message = pending->reply;
679 pending->reply = NULL;
681 CONNECTION_UNLOCK (pending->connection);
687 * Block until the pending call is completed. The blocking is as with
688 * dbus_connection_send_with_reply_and_block(); it does not enter the
689 * main loop or process other messages, it simply waits for the reply
692 * If the pending call is already completed, this function returns
695 * @todo when you start blocking, the timeout is reset, but it should
696 * really only use time remaining since the pending call was created.
697 * This requires storing timestamps instead of intervals in the timeout
699 * @param pending the pending call
702 dbus_pending_call_block (DBusPendingCall *pending)
704 _dbus_return_if_fail (pending != NULL);
706 _dbus_connection_block_pending_call (pending);
710 * Allocates an integer ID to be used for storing application-specific
711 * data on any DBusPendingCall. The allocated ID may then be used
712 * with dbus_pending_call_set_data() and dbus_pending_call_get_data().
713 * The passed-in slot must be initialized to -1, and is filled in
714 * with the slot ID. If the passed-in slot is not -1, it's assumed
715 * to be already allocated, and its refcount is incremented.
717 * The allocated slot is global, i.e. all DBusPendingCall objects will
718 * have a slot with the given integer ID reserved.
720 * @param slot_p address of a global variable storing the slot
721 * @returns #FALSE on failure (no memory)
724 dbus_pending_call_allocate_data_slot (dbus_int32_t *slot_p)
726 _dbus_return_val_if_fail (slot_p != NULL, FALSE);
728 return _dbus_data_slot_allocator_alloc (&slot_allocator,
729 &_DBUS_LOCK_NAME (pending_call_slots),
734 * Deallocates a global ID for #DBusPendingCall data slots.
735 * dbus_pending_call_get_data() and dbus_pending_call_set_data() may
736 * no longer be used with this slot. Existing data stored on existing
737 * DBusPendingCall objects will be freed when the #DBusPendingCall is
738 * finalized, but may not be retrieved (and may only be replaced if
739 * someone else reallocates the slot). When the refcount on the
740 * passed-in slot reaches 0, it is set to -1.
742 * @param slot_p address storing the slot to deallocate
745 dbus_pending_call_free_data_slot (dbus_int32_t *slot_p)
747 _dbus_return_if_fail (slot_p != NULL);
748 _dbus_return_if_fail (*slot_p >= 0);
750 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
754 * Stores a pointer on a #DBusPendingCall, along
755 * with an optional function to be used for freeing
756 * the data when the data is set again, or when
757 * the pending call is finalized. The slot number
758 * must have been allocated with dbus_pending_call_allocate_data_slot().
760 * @param pending the pending_call
761 * @param slot the slot number
762 * @param data the data to store
763 * @param free_data_func finalizer function for the data
764 * @returns #TRUE if there was enough memory to store the data
767 dbus_pending_call_set_data (DBusPendingCall *pending,
770 DBusFreeFunction free_data_func)
774 _dbus_return_val_if_fail (pending != NULL, FALSE);
775 _dbus_return_val_if_fail (slot >= 0, FALSE);
778 CONNECTION_LOCK (pending->connection);
779 retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
780 CONNECTION_UNLOCK (pending->connection);
785 * Retrieves data previously set with dbus_pending_call_set_data().
786 * The slot must still be allocated (must not have been freed).
788 * @param pending the pending_call
789 * @param slot the slot to get data from
790 * @returns the data, or #NULL if not found
793 dbus_pending_call_get_data (DBusPendingCall *pending,
798 _dbus_return_val_if_fail (pending != NULL, NULL);
800 CONNECTION_LOCK (pending->connection);
801 res = _dbus_data_slot_list_get (&slot_allocator,
804 CONNECTION_UNLOCK (pending->connection);
811 #ifdef DBUS_BUILD_TESTS
814 * @ingroup DBusPendingCallInternals
815 * Unit test for DBusPendingCall.
817 * @returns #TRUE on success.
820 _dbus_pending_call_test (const char *test_data_dir)
825 #endif /* DBUS_BUILD_TESTS */