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)
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
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 /* it would probably seem logical to pass in _DBUS_INT_MAX for
104 * infinite timeout, but then math in
105 * _dbus_connection_block_for_reply would get all overflow-prone, so
108 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
109 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
111 if (!dbus_pending_call_allocate_data_slot (¬ify_user_data_slot))
114 pending = dbus_new0 (DBusPendingCall, 1);
118 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
122 timeout = _dbus_timeout_new (timeout_milliseconds,
128 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
133 pending->refcount.value = 1;
134 pending->connection = connection;
135 _dbus_connection_ref_unlocked (pending->connection);
137 pending->timeout = timeout;
140 _dbus_data_slot_list_init (&pending->slot_list);
146 * Sets the reply of a pending call with the given message,
147 * or if the message is #NULL, by timing out the pending call.
149 * @param pending the pending call
150 * @param message the message to complete the call with, or #NULL
151 * to time out the call
154 _dbus_pending_call_set_reply_unlocked (DBusPendingCall *pending,
155 DBusMessage *message)
159 message = pending->timeout_link->data;
160 _dbus_list_clear (&pending->timeout_link);
163 dbus_message_ref (message);
165 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
167 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
169 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
170 "error" : "other type",
171 pending->reply_serial);
173 _dbus_assert (pending->reply == NULL);
174 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
175 pending->reply = message;
179 * Calls notifier function for the pending call
180 * and sets the call to completed.
182 * @param pending the pending call
186 _dbus_pending_call_complete (DBusPendingCall *pending)
188 _dbus_assert (!pending->completed);
190 pending->completed = TRUE;
192 if (pending->function)
195 user_data = dbus_pending_call_get_data (pending,
196 notify_user_data_slot);
198 (* pending->function) (pending, user_data);
203 * If the pending call hasn't been timed out, add its timeout
204 * error reply to the connection's incoming message queue.
206 * @param pending the pending call
207 * @param connection the connection the call was sent to
210 _dbus_pending_call_queue_timeout_error_unlocked (DBusPendingCall *pending,
211 DBusConnection *connection)
213 _dbus_assert (connection == pending->connection);
215 if (pending->timeout_link)
217 _dbus_connection_queue_synthesized_message_link (connection,
218 pending->timeout_link);
219 pending->timeout_link = NULL;
224 * Checks to see if a timeout has been added
226 * @param pending the pending_call
227 * @returns #TRUE if there is a timeout or #FALSE if not
230 _dbus_pending_call_is_timeout_added_unlocked (DBusPendingCall *pending)
232 _dbus_assert (pending != NULL);
234 return pending->timeout_added;
239 * Sets wether the timeout has been added
241 * @param pending the pending_call
242 * @param is_added whether or not a timeout is added
245 _dbus_pending_call_set_timeout_added_unlocked (DBusPendingCall *pending,
246 dbus_bool_t is_added)
248 _dbus_assert (pending != NULL);
250 pending->timeout_added = is_added;
255 * Retrives the timeout
257 * @param pending the pending_call
258 * @returns a timeout object
261 _dbus_pending_call_get_timeout_unlocked (DBusPendingCall *pending)
263 _dbus_assert (pending != NULL);
265 return pending->timeout;
269 * Gets the reply's serial number
271 * @param pending the pending_call
272 * @returns a serial number for the reply or 0
275 _dbus_pending_call_get_reply_serial_unlocked (DBusPendingCall *pending)
277 _dbus_assert (pending != NULL);
279 return pending->reply_serial;
283 * Sets the reply's serial number
285 * @param pending the pending_call
286 * @param serial the serial number
289 _dbus_pending_call_set_reply_serial_unlocked (DBusPendingCall *pending,
290 dbus_uint32_t serial)
292 _dbus_assert (pending != NULL);
293 _dbus_assert (pending->reply_serial == 0);
295 pending->reply_serial = serial;
299 * Gets the connection associated with this pending call.
301 * @param pending the pending_call
302 * @returns the connection associated with the pending call
305 _dbus_pending_call_get_connection_and_lock (DBusPendingCall *pending)
307 _dbus_assert (pending != NULL);
309 CONNECTION_LOCK (pending->connection);
310 return pending->connection;
314 * Gets the connection associated with this pending call.
316 * @param pending the pending_call
317 * @returns the connection associated with the pending call
320 _dbus_pending_call_get_connection_unlocked (DBusPendingCall *pending)
322 _dbus_assert (pending != NULL);
324 return pending->connection;
328 * Sets the reply message associated with the pending call to a timeout error
330 * @param pending the pending_call
331 * @param message the message we are sending the error reply to
332 * @param serial serial number for the reply
333 * @return #FALSE on OOM
336 _dbus_pending_call_set_timeout_error_unlocked (DBusPendingCall *pending,
337 DBusMessage *message,
338 dbus_uint32_t serial)
340 DBusList *reply_link;
343 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
344 "Did not receive a reply. Possible causes include: "
345 "the remote application did not send a reply, "
346 "the message bus security policy blocked the reply, "
347 "the reply timeout expired, or "
348 "the network connection was broken.");
352 reply_link = _dbus_list_alloc_link (reply);
353 if (reply_link == NULL)
355 dbus_message_unref (reply);
359 pending->timeout_link = reply_link;
361 _dbus_pending_call_set_reply_serial_unlocked (pending, serial);
367 * Increments the reference count on a pending call,
368 * while the lock on its connection is already held.
370 * @param pending the pending call object
371 * @returns the pending call object
374 _dbus_pending_call_ref_unlocked (DBusPendingCall *pending)
376 pending->refcount.value += 1;
383 _dbus_pending_call_last_unref (DBusPendingCall *pending)
385 DBusConnection *connection;
387 /* If we get here, we should be already detached
388 * from the connection, or never attached.
390 _dbus_assert (!pending->timeout_added);
392 connection = pending->connection;
394 /* this assumes we aren't holding connection lock... */
395 _dbus_data_slot_list_free (&pending->slot_list);
397 if (pending->timeout != NULL)
398 _dbus_timeout_unref (pending->timeout);
400 if (pending->timeout_link)
402 dbus_message_unref ((DBusMessage *)pending->timeout_link->data);
403 _dbus_list_free_link (pending->timeout_link);
404 pending->timeout_link = NULL;
409 dbus_message_unref (pending->reply);
410 pending->reply = NULL;
415 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
417 /* connection lock should not be held. */
418 /* Free the connection last to avoid a weird state while
419 * calling out to application code where the pending exists
420 * but not the connection.
422 dbus_connection_unref (connection);
426 * Decrements the reference count on a pending call,
427 * freeing it if the count reaches 0. Assumes
428 * connection lock is already held.
430 * @param pending the pending call object
433 _dbus_pending_call_unref_and_unlock (DBusPendingCall *pending)
435 dbus_bool_t last_unref;
437 _dbus_assert (pending->refcount.value > 0);
439 pending->refcount.value -= 1;
440 last_unref = pending->refcount.value == 0;
442 CONNECTION_UNLOCK (pending->connection);
444 _dbus_pending_call_last_unref (pending);
448 * Checks whether the pending call has received a reply
449 * yet, or not. Assumes connection lock is held.
451 * @param pending the pending call
452 * @returns #TRUE if a reply has been received
455 _dbus_pending_call_get_completed_unlocked (DBusPendingCall *pending)
457 return pending->completed;
460 static DBusDataSlotAllocator slot_allocator;
461 _DBUS_DEFINE_GLOBAL_LOCK (pending_call_slots);
464 * Stores a pointer on a #DBusPendingCall, along
465 * with an optional function to be used for freeing
466 * the data when the data is set again, or when
467 * the pending call is finalized. The slot number
468 * must have been allocated with dbus_pending_call_allocate_data_slot().
470 * @param pending the pending_call
471 * @param slot the slot number
472 * @param data the data to store
473 * @param free_data_func finalizer function for the data
474 * @returns #TRUE if there was enough memory to store the data
477 _dbus_pending_call_set_data_unlocked (DBusPendingCall *pending,
480 DBusFreeFunction free_data_func)
482 DBusFreeFunction old_free_func;
486 retval = _dbus_data_slot_list_set (&slot_allocator,
488 slot, data, free_data_func,
489 &old_free_func, &old_data);
491 /* Drop locks to call out to app code */
492 CONNECTION_UNLOCK (pending->connection);
497 (* old_free_func) (old_data);
500 CONNECTION_LOCK (pending->connection);
508 * @defgroup DBusPendingCall DBusPendingCall
510 * @brief Pending reply to a method call message
512 * A DBusPendingCall is an object representing an
513 * expected reply. A #DBusPendingCall can be created
514 * when you send a message that should have a reply.
520 * @typedef DBusPendingCall
522 * Opaque data type representing a message pending.
526 * Increments the reference count on a pending call.
528 * @param pending the pending call object
529 * @returns the pending call object
532 dbus_pending_call_ref (DBusPendingCall *pending)
534 _dbus_return_val_if_fail (pending != NULL, NULL);
536 /* The connection lock is better than the global
537 * lock in the atomic increment fallback
539 #ifdef DBUS_HAVE_ATOMIC_INT
540 _dbus_atomic_inc (&pending->refcount);
542 CONNECTION_LOCK (pending->connection);
543 _dbus_assert (pending->refcount.value > 0);
545 pending->refcount.value += 1;
546 CONNECTION_UNLOCK (pending->connection);
553 * Decrements the reference count on a pending call,
554 * freeing it if the count reaches 0.
556 * @param pending the pending call object
559 dbus_pending_call_unref (DBusPendingCall *pending)
561 dbus_bool_t last_unref;
563 _dbus_return_if_fail (pending != NULL);
565 /* More efficient to use the connection lock instead of atomic
566 * int fallback if we lack atomic int decrement
568 #ifdef DBUS_HAVE_ATOMIC_INT
569 last_unref = (_dbus_atomic_dec (&pending->refcount) == 1);
571 CONNECTION_LOCK (pending->connection);
572 _dbus_assert (pending->refcount.value > 0);
573 pending->refcount.value -= 1;
574 last_unref = pending->refcount.value == 0;
575 CONNECTION_UNLOCK (pending->connection);
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
619 * from dbus_connection_send() so probably your app owns a ref also.
621 * @param pending the pending call
624 dbus_pending_call_cancel (DBusPendingCall *pending)
626 _dbus_return_if_fail (pending != NULL);
628 _dbus_connection_remove_pending_call (pending->connection,
633 * Checks whether the pending call has received a reply
636 * @param pending the pending call
637 * @returns #TRUE if a reply has been received
640 dbus_pending_call_get_completed (DBusPendingCall *pending)
642 dbus_bool_t completed;
644 _dbus_return_val_if_fail (pending != NULL, FALSE);
646 CONNECTION_LOCK (pending->connection);
647 completed = pending->completed;
648 CONNECTION_UNLOCK (pending->connection);
654 * Gets the reply, or returns #NULL if none has been received
655 * yet. Ownership of the reply message passes to the caller. This
656 * function can only be called once per pending call, since the reply
657 * message is tranferred to the caller.
659 * @param pending the pending call
660 * @returns the reply message or #NULL.
663 dbus_pending_call_steal_reply (DBusPendingCall *pending)
665 DBusMessage *message;
667 _dbus_return_val_if_fail (pending != NULL, NULL);
668 _dbus_return_val_if_fail (pending->completed, NULL);
669 _dbus_return_val_if_fail (pending->reply != NULL, NULL);
671 CONNECTION_LOCK (pending->connection);
673 message = pending->reply;
674 pending->reply = NULL;
676 CONNECTION_UNLOCK (pending->connection);
682 * Block until the pending call is completed. The blocking is as with
683 * dbus_connection_send_with_reply_and_block(); it does not enter the
684 * main loop or process other messages, it simply waits for the reply
687 * If the pending call is already completed, this function returns
690 * @todo when you start blocking, the timeout is reset, but it should
691 * really only use time remaining since the pending call was created.
692 * This requires storing timestamps instead of intervals in the timeout
694 * @param pending the pending call
697 dbus_pending_call_block (DBusPendingCall *pending)
699 _dbus_return_if_fail (pending != NULL);
701 _dbus_connection_block_pending_call (pending);
705 * Allocates an integer ID to be used for storing application-specific
706 * data on any DBusPendingCall. The allocated ID may then be used
707 * with dbus_pending_call_set_data() and dbus_pending_call_get_data().
708 * The passed-in slot must be initialized to -1, and is filled in
709 * with the slot ID. If the passed-in slot is not -1, it's assumed
710 * to be already allocated, and its refcount is incremented.
712 * The allocated slot is global, i.e. all DBusPendingCall objects will
713 * have a slot with the given integer ID reserved.
715 * @param slot_p address of a global variable storing the slot
716 * @returns #FALSE on failure (no memory)
719 dbus_pending_call_allocate_data_slot (dbus_int32_t *slot_p)
721 _dbus_return_val_if_fail (slot_p != NULL, FALSE);
723 return _dbus_data_slot_allocator_alloc (&slot_allocator,
724 &_DBUS_LOCK_NAME (pending_call_slots),
729 * Deallocates a global ID for #DBusPendingCall data slots.
730 * dbus_pending_call_get_data() and dbus_pending_call_set_data() may
731 * no longer be used with this slot. Existing data stored on existing
732 * DBusPendingCall objects will be freed when the #DBusPendingCall is
733 * finalized, but may not be retrieved (and may only be replaced if
734 * someone else reallocates the slot). When the refcount on the
735 * passed-in slot reaches 0, it is set to -1.
737 * @param slot_p address storing the slot to deallocate
740 dbus_pending_call_free_data_slot (dbus_int32_t *slot_p)
742 _dbus_return_if_fail (slot_p != NULL);
743 _dbus_return_if_fail (*slot_p >= 0);
745 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
749 * Stores a pointer on a #DBusPendingCall, along
750 * with an optional function to be used for freeing
751 * the data when the data is set again, or when
752 * the pending call is finalized. The slot number
753 * must have been allocated with dbus_pending_call_allocate_data_slot().
755 * @param pending the pending_call
756 * @param slot the slot number
757 * @param data the data to store
758 * @param free_data_func finalizer function for the data
759 * @returns #TRUE if there was enough memory to store the data
762 dbus_pending_call_set_data (DBusPendingCall *pending,
765 DBusFreeFunction free_data_func)
769 _dbus_return_val_if_fail (pending != NULL, FALSE);
770 _dbus_return_val_if_fail (slot >= 0, FALSE);
773 CONNECTION_LOCK (pending->connection);
774 retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
775 CONNECTION_UNLOCK (pending->connection);
780 * Retrieves data previously set with dbus_pending_call_set_data().
781 * The slot must still be allocated (must not have been freed).
783 * @param pending the pending_call
784 * @param slot the slot to get data from
785 * @returns the data, or #NULL if not found
788 dbus_pending_call_get_data (DBusPendingCall *pending,
793 _dbus_return_val_if_fail (pending != NULL, NULL);
795 CONNECTION_LOCK (pending->connection);
796 res = _dbus_data_slot_list_get (&slot_allocator,
799 CONNECTION_UNLOCK (pending->connection);
806 #ifdef DBUS_BUILD_TESTS
809 * @ingroup DBusPendingCallInternals
810 * Unit test for DBusPendingCall.
812 * @returns #TRUE on success.
815 _dbus_pending_call_test (const char *test_data_dir)
820 #endif /* DBUS_BUILD_TESTS */