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);
369 * @defgroup DBusPendingCall DBusPendingCall
371 * @brief Pending reply to a method call message
373 * A DBusPendingCall is an object representing an
374 * expected reply. A #DBusPendingCall can be created
375 * when you send a message that should have a reply.
381 * @typedef DBusPendingCall
383 * Opaque data type representing a message pending.
387 * Increments the reference count on a pending call,
388 * while the lock on its connection is already held.
390 * @param pending the pending call object
391 * @returns the pending call object
394 _dbus_pending_call_ref_unlocked (DBusPendingCall *pending)
396 pending->refcount.value += 1;
402 * Increments the reference count on a pending call.
404 * @param pending the pending call object
405 * @returns the pending call object
408 dbus_pending_call_ref (DBusPendingCall *pending)
410 _dbus_return_val_if_fail (pending != NULL, NULL);
412 /* The connection lock is better than the global
413 * lock in the atomic increment fallback
415 #ifdef DBUS_HAVE_ATOMIC_INT
416 _dbus_atomic_inc (&pending->refcount);
418 CONNECTION_LOCK (pending->connection);
419 _dbus_assert (pending->refcount.value > 0);
421 pending->refcount.value += 1;
422 CONNECTION_UNLOCK (pending->connection);
429 _dbus_pending_call_last_unref (DBusPendingCall *pending)
431 DBusConnection *connection;
433 /* If we get here, we should be already detached
434 * from the connection, or never attached.
436 _dbus_assert (!pending->timeout_added);
438 connection = pending->connection;
440 /* this assumes we aren't holding connection lock... */
441 _dbus_data_slot_list_free (&pending->slot_list);
443 if (pending->timeout != NULL)
444 _dbus_timeout_unref (pending->timeout);
446 if (pending->timeout_link)
448 dbus_message_unref ((DBusMessage *)pending->timeout_link->data);
449 _dbus_list_free_link (pending->timeout_link);
450 pending->timeout_link = NULL;
455 dbus_message_unref (pending->reply);
456 pending->reply = NULL;
461 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
463 /* connection lock should not be held. */
464 /* Free the connection last to avoid a weird state while
465 * calling out to application code where the pending exists
466 * but not the connection.
468 dbus_connection_unref (connection);
472 * Decrements the reference count on a pending call,
473 * freeing it if the count reaches 0. Assumes
474 * connection lock is already held.
476 * @param pending the pending call object
479 _dbus_pending_call_unref_and_unlock (DBusPendingCall *pending)
481 dbus_bool_t last_unref;
483 _dbus_assert (pending->refcount.value > 0);
485 pending->refcount.value -= 1;
486 last_unref = pending->refcount.value == 0;
488 CONNECTION_UNLOCK (pending->connection);
490 _dbus_pending_call_last_unref (pending);
494 * Decrements the reference count on a pending call,
495 * freeing it if the count reaches 0.
497 * @param pending the pending call object
500 dbus_pending_call_unref (DBusPendingCall *pending)
502 dbus_bool_t last_unref;
504 _dbus_return_if_fail (pending != NULL);
506 /* More efficient to use the connection lock instead of atomic
507 * int fallback if we lack atomic int decrement
509 #ifdef DBUS_HAVE_ATOMIC_INT
510 last_unref = (_dbus_atomic_dec (&pending->refcount) == 1);
512 CONNECTION_LOCK (pending->connection);
513 _dbus_assert (pending->refcount.value > 0);
514 pending->refcount.value -= 1;
515 last_unref = pending->refcount.value == 0;
516 CONNECTION_UNLOCK (pending->connection);
520 _dbus_pending_call_last_unref(pending);
524 * Sets a notification function to be called when the reply is
525 * received or the pending call times out.
527 * @param pending the pending call
528 * @param function notifier function
529 * @param user_data data to pass to notifier function
530 * @param free_user_data function to free the user data
531 * @returns #FALSE if not enough memory
534 dbus_pending_call_set_notify (DBusPendingCall *pending,
535 DBusPendingCallNotifyFunction function,
537 DBusFreeFunction free_user_data)
539 _dbus_return_val_if_fail (pending != NULL, FALSE);
541 CONNECTION_LOCK (pending->connection);
543 /* could invoke application code! */
544 if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
545 user_data, free_user_data))
548 pending->function = function;
550 CONNECTION_UNLOCK (pending->connection);
556 * Cancels the pending call, such that any reply or error received
557 * will just be ignored. Drops the dbus library's internal reference
558 * to the #DBusPendingCall so will free the call if nobody else is
559 * holding a reference. However you usually get a reference
560 * from dbus_connection_send() so probably your app owns a ref also.
562 * @param pending the pending call
565 dbus_pending_call_cancel (DBusPendingCall *pending)
567 _dbus_return_if_fail (pending != NULL);
569 _dbus_connection_remove_pending_call (pending->connection,
574 * Checks whether the pending call has received a reply
575 * yet, or not. Assumes connection lock is held.
577 * @param pending the pending call
578 * @returns #TRUE if a reply has been received
581 _dbus_pending_call_get_completed_unlocked (DBusPendingCall *pending)
583 return pending->completed;
587 * Checks whether the pending call has received a reply
590 * @param pending the pending call
591 * @returns #TRUE if a reply has been received
594 dbus_pending_call_get_completed (DBusPendingCall *pending)
596 dbus_bool_t completed;
598 _dbus_return_val_if_fail (pending != NULL, FALSE);
600 CONNECTION_LOCK (pending->connection);
601 completed = pending->completed;
602 CONNECTION_UNLOCK (pending->connection);
608 * Gets the reply, or returns #NULL if none has been received
609 * yet. Ownership of the reply message passes to the caller. This
610 * function can only be called once per pending call, since the reply
611 * message is tranferred to the caller.
613 * @param pending the pending call
614 * @returns the reply message or #NULL.
617 dbus_pending_call_steal_reply (DBusPendingCall *pending)
619 DBusMessage *message;
621 _dbus_return_val_if_fail (pending != NULL, NULL);
622 _dbus_return_val_if_fail (pending->completed, NULL);
623 _dbus_return_val_if_fail (pending->reply != NULL, NULL);
625 CONNECTION_LOCK (pending->connection);
627 message = pending->reply;
628 pending->reply = NULL;
630 CONNECTION_UNLOCK (pending->connection);
636 * Block until the pending call is completed. The blocking is as with
637 * dbus_connection_send_with_reply_and_block(); it does not enter the
638 * main loop or process other messages, it simply waits for the reply
641 * If the pending call is already completed, this function returns
644 * @todo when you start blocking, the timeout is reset, but it should
645 * really only use time remaining since the pending call was created.
646 * This requires storing timestamps instead of intervals in the timeout
648 * @param pending the pending call
651 dbus_pending_call_block (DBusPendingCall *pending)
653 _dbus_return_if_fail (pending != NULL);
655 _dbus_connection_block_pending_call (pending);
658 static DBusDataSlotAllocator slot_allocator;
659 _DBUS_DEFINE_GLOBAL_LOCK (pending_call_slots);
662 * Allocates an integer ID to be used for storing application-specific
663 * data on any DBusPendingCall. The allocated ID may then be used
664 * with dbus_pending_call_set_data() and dbus_pending_call_get_data().
665 * The passed-in slot must be initialized to -1, and is filled in
666 * with the slot ID. If the passed-in slot is not -1, it's assumed
667 * to be already allocated, and its refcount is incremented.
669 * The allocated slot is global, i.e. all DBusPendingCall objects will
670 * have a slot with the given integer ID reserved.
672 * @param slot_p address of a global variable storing the slot
673 * @returns #FALSE on failure (no memory)
676 dbus_pending_call_allocate_data_slot (dbus_int32_t *slot_p)
678 _dbus_return_val_if_fail (slot_p != NULL, FALSE);
680 return _dbus_data_slot_allocator_alloc (&slot_allocator,
681 &_DBUS_LOCK_NAME (pending_call_slots),
686 * Deallocates a global ID for #DBusPendingCall data slots.
687 * dbus_pending_call_get_data() and dbus_pending_call_set_data() may
688 * no longer be used with this slot. Existing data stored on existing
689 * DBusPendingCall objects will be freed when the #DBusPendingCall is
690 * finalized, but may not be retrieved (and may only be replaced if
691 * someone else reallocates the slot). When the refcount on the
692 * passed-in slot reaches 0, it is set to -1.
694 * @param slot_p address storing the slot to deallocate
697 dbus_pending_call_free_data_slot (dbus_int32_t *slot_p)
699 _dbus_return_if_fail (slot_p != NULL);
700 _dbus_return_if_fail (*slot_p >= 0);
702 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
706 * Stores a pointer on a #DBusPendingCall, along
707 * with an optional function to be used for freeing
708 * the data when the data is set again, or when
709 * the pending call is finalized. The slot number
710 * must have been allocated with dbus_pending_call_allocate_data_slot().
712 * @param pending the pending_call
713 * @param slot the slot number
714 * @param data the data to store
715 * @param free_data_func finalizer function for the data
716 * @returns #TRUE if there was enough memory to store the data
719 _dbus_pending_call_set_data_unlocked (DBusPendingCall *pending,
722 DBusFreeFunction free_data_func)
724 DBusFreeFunction old_free_func;
728 retval = _dbus_data_slot_list_set (&slot_allocator,
730 slot, data, free_data_func,
731 &old_free_func, &old_data);
733 /* Drop locks to call out to app code */
734 CONNECTION_UNLOCK (pending->connection);
739 (* old_free_func) (old_data);
742 CONNECTION_LOCK (pending->connection);
748 * Stores a pointer on a #DBusPendingCall, along
749 * with an optional function to be used for freeing
750 * the data when the data is set again, or when
751 * the pending call is finalized. The slot number
752 * must have been allocated with dbus_pending_call_allocate_data_slot().
754 * @param pending the pending_call
755 * @param slot the slot number
756 * @param data the data to store
757 * @param free_data_func finalizer function for the data
758 * @returns #TRUE if there was enough memory to store the data
761 dbus_pending_call_set_data (DBusPendingCall *pending,
764 DBusFreeFunction free_data_func)
768 _dbus_return_val_if_fail (pending != NULL, FALSE);
769 _dbus_return_val_if_fail (slot >= 0, FALSE);
772 CONNECTION_LOCK (pending->connection);
773 retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
774 CONNECTION_UNLOCK (pending->connection);
779 * Retrieves data previously set with dbus_pending_call_set_data().
780 * The slot must still be allocated (must not have been freed).
782 * @param pending the pending_call
783 * @param slot the slot to get data from
784 * @returns the data, or #NULL if not found
787 dbus_pending_call_get_data (DBusPendingCall *pending,
792 _dbus_return_val_if_fail (pending != NULL, NULL);
794 CONNECTION_LOCK (pending->connection);
795 res = _dbus_data_slot_list_get (&slot_allocator,
798 CONNECTION_UNLOCK (pending->connection);
805 #ifdef DBUS_BUILD_TESTS
808 * @ingroup DBusPendingCallInternals
809 * Unit test for DBusPendingCall.
811 * @returns #TRUE on success.
814 _dbus_pending_call_test (const char *test_data_dir)
819 #endif /* DBUS_BUILD_TESTS */