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 struct DBusPendingCall
49 DBusAtomic refcount; /**< reference count */
51 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
53 DBusPendingCallNotifyFunction function; /**< Notifier when reply arrives. */
55 DBusConnection *connection; /**< Connections we're associated with */
56 DBusMessage *reply; /**< Reply (after we've received it) */
57 DBusTimeout *timeout; /**< Timeout */
59 DBusList *timeout_link; /**< Preallocated timeout response */
61 dbus_uint32_t reply_serial; /**< Expected serial of reply */
63 unsigned int completed : 1; /**< TRUE if completed */
64 unsigned int timeout_added : 1; /**< Have added the timeout */
67 static dbus_int32_t notify_user_data_slot = -1;
70 * Creates a new pending reply object.
72 * @param connection connection where reply will arrive
73 * @param timeout_milliseconds length of timeout, -1 for default
74 * @param timeout_handler timeout handler, takes pending call as data
75 * @returns a new #DBusPendingCall or #NULL if no memory.
78 _dbus_pending_call_new (DBusConnection *connection,
79 int timeout_milliseconds,
80 DBusTimeoutHandler timeout_handler)
82 DBusPendingCall *pending;
85 _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
87 if (timeout_milliseconds == -1)
88 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
90 /* it would probably seem logical to pass in _DBUS_INT_MAX for
91 * infinite timeout, but then math in
92 * _dbus_connection_block_for_reply would get all overflow-prone, so
95 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
96 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
98 if (!dbus_pending_call_allocate_data_slot (¬ify_user_data_slot))
101 pending = dbus_new0 (DBusPendingCall, 1);
105 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
109 timeout = _dbus_timeout_new (timeout_milliseconds,
115 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
120 pending->refcount.value = 1;
121 pending->connection = connection;
122 dbus_connection_ref (pending->connection);
124 pending->timeout = timeout;
127 _dbus_data_slot_list_init (&pending->slot_list);
133 * Sets the reply of a pending call with the given message,
134 * or if the message is #NULL, by timing out the pending call.
136 * @param pending the pending call
137 * @param message the message to complete the call with, or #NULL
138 * to time out the call
141 _dbus_pending_call_set_reply (DBusPendingCall *pending,
142 DBusMessage *message)
146 message = pending->timeout_link->data;
147 _dbus_list_clear (&pending->timeout_link);
150 dbus_message_ref (message);
152 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
154 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
156 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
157 "error" : "other type",
158 pending->reply_serial);
160 _dbus_assert (pending->reply == NULL);
161 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
162 pending->reply = message;
166 * Calls notifier function for the pending call
167 * and sets the call to completed.
169 * @param pending the pending call
173 _dbus_pending_call_complete (DBusPendingCall *pending)
175 _dbus_assert (!pending->completed);
177 pending->completed = TRUE;
179 if (pending->function)
182 user_data = dbus_pending_call_get_data (pending,
183 notify_user_data_slot);
185 (* pending->function) (pending, user_data);
190 _dbus_pending_call_queue_timeout_error (DBusPendingCall *pending,
191 DBusConnection *connection)
193 if (pending->timeout_link)
195 _dbus_connection_queue_synthesized_message_link (connection,
196 pending->timeout_link);
197 pending->timeout_link = NULL;
202 * Checks to see if a timeout has been added
204 * @param pending the pending_call
205 * @returns #TRUE if there is a timeout or #FALSE if not
208 _dbus_pending_call_is_timeout_added (DBusPendingCall *pending)
210 _dbus_assert (pending != NULL);
212 return pending->timeout_added;
217 * Sets wether the timeout has been added
219 * @param pending the pending_call
220 * @param is_added whether or not a timeout is added
223 _dbus_pending_call_set_timeout_added (DBusPendingCall *pending,
224 dbus_bool_t is_added)
226 _dbus_assert (pending != NULL);
228 pending->timeout_added = is_added;
233 * Retrives the timeout
235 * @param pending the pending_call
236 * @returns a timeout object
239 _dbus_pending_call_get_timeout (DBusPendingCall *pending)
241 _dbus_assert (pending != NULL);
243 return pending->timeout;
247 * Gets the reply's serial number
249 * @param pending the pending_call
250 * @returns a serial number for the reply or 0
253 _dbus_pending_call_get_reply_serial (DBusPendingCall *pending)
255 _dbus_assert (pending != NULL);
257 return pending->reply_serial;
261 * Sets the reply's serial number
263 * @param pending the pending_call
264 * @param serial the serial number
267 _dbus_pending_call_set_reply_serial (DBusPendingCall *pending,
268 dbus_uint32_t serial)
270 _dbus_assert (pending != NULL);
271 _dbus_assert (pending->reply_serial == 0);
273 pending->reply_serial = serial;
277 * Gets the connection associated with this pending call
279 * @param pending the pending_call
280 * @returns the connection associated with the pending call
283 _dbus_pending_call_get_connection (DBusPendingCall *pending)
285 _dbus_assert (pending != NULL);
287 return pending->connection;
291 * Sets the reply message associated with the pending call to a timeout error
293 * @param pending the pending_call
294 * @param message the message we are sending the error reply to
295 * @param serial serial number for the reply
296 * @return #FALSE on OOM
299 _dbus_pending_call_set_timeout_error (DBusPendingCall *pending,
300 DBusMessage *message,
301 dbus_uint32_t serial)
303 DBusList *reply_link;
306 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
307 "No reply within specified time");
311 reply_link = _dbus_list_alloc_link (reply);
312 if (reply_link == NULL)
314 dbus_message_unref (reply);
318 pending->timeout_link = reply_link;
320 _dbus_pending_call_set_reply_serial (pending, serial);
328 * @defgroup DBusPendingCall DBusPendingCall
330 * @brief Pending reply to a method call message
332 * A DBusPendingCall is an object representing an
333 * expected reply. A #DBusPendingCall can be created
334 * when you send a message that should have a reply.
340 * @typedef DBusPendingCall
342 * Opaque data type representing a message pending.
346 * Increments the reference count on a pending call.
348 * @param pending the pending call object
349 * @returns the pending call object
352 dbus_pending_call_ref (DBusPendingCall *pending)
354 _dbus_return_val_if_fail (pending != NULL, NULL);
356 _dbus_atomic_inc (&pending->refcount);
362 * Decrements the reference count on a pending call,
363 * freeing it if the count reaches 0.
365 * @param pending the pending call object
368 dbus_pending_call_unref (DBusPendingCall *pending)
370 dbus_bool_t last_unref;
372 _dbus_return_if_fail (pending != NULL);
374 last_unref = (_dbus_atomic_dec (&pending->refcount) == 1);
378 /* If we get here, we should be already detached
379 * from the connection, or never attached.
381 _dbus_assert (!pending->timeout_added);
383 dbus_connection_unref (pending->connection);
385 /* this assumes we aren't holding connection lock... */
386 _dbus_data_slot_list_free (&pending->slot_list);
388 if (pending->timeout != NULL)
389 _dbus_timeout_unref (pending->timeout);
391 if (pending->timeout_link)
393 dbus_message_unref ((DBusMessage *)pending->timeout_link->data);
394 _dbus_list_free_link (pending->timeout_link);
395 pending->timeout_link = NULL;
400 dbus_message_unref (pending->reply);
401 pending->reply = NULL;
406 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
411 * Sets a notification function to be called when the reply is
412 * received or the pending call times out.
414 * @param pending the pending call
415 * @param function notifier function
416 * @param user_data data to pass to notifier function
417 * @param free_user_data function to free the user data
418 * @returns #FALSE if not enough memory
421 dbus_pending_call_set_notify (DBusPendingCall *pending,
422 DBusPendingCallNotifyFunction function,
424 DBusFreeFunction free_user_data)
426 _dbus_return_val_if_fail (pending != NULL, FALSE);
428 /* could invoke application code! */
429 if (!dbus_pending_call_set_data (pending, notify_user_data_slot,
430 user_data, free_user_data))
433 pending->function = function;
439 * Cancels the pending call, such that any reply or error received
440 * will just be ignored. Drops the dbus library's internal reference
441 * to the #DBusPendingCall so will free the call if nobody else is
442 * holding a reference. However you usually get a reference
443 * from dbus_connection_send() so probably your app owns a ref also.
445 * @param pending the pending call
448 dbus_pending_call_cancel (DBusPendingCall *pending)
450 if (pending->connection)
451 _dbus_connection_remove_pending_call (pending->connection,
456 * Checks whether the pending call has received a reply
459 * @todo not thread safe? I guess it has to lock though it sucks
461 * @param pending the pending call
462 * @returns #TRUE if a reply has been received */
464 dbus_pending_call_get_completed (DBusPendingCall *pending)
466 return pending->completed;
470 * Gets the reply, or returns #NULL if none has been received
471 * yet. Ownership of the reply message passes to the caller. This
472 * function can only be called once per pending call, since the reply
473 * message is tranferred to the caller.
475 * @param pending the pending call
476 * @returns the reply message or #NULL.
479 dbus_pending_call_steal_reply (DBusPendingCall *pending)
481 DBusMessage *message;
483 _dbus_return_val_if_fail (pending->completed, NULL);
484 _dbus_return_val_if_fail (pending->reply != NULL, NULL);
486 message = pending->reply;
487 pending->reply = NULL;
493 * Block until the pending call is completed. The blocking is as with
494 * dbus_connection_send_with_reply_and_block(); it does not enter the
495 * main loop or process other messages, it simply waits for the reply
498 * If the pending call is already completed, this function returns
501 * @todo when you start blocking, the timeout is reset, but it should
502 * really only use time remaining since the pending call was created.
504 * @param pending the pending call
507 dbus_pending_call_block (DBusPendingCall *pending)
509 _dbus_connection_block_pending_call (pending);
512 static DBusDataSlotAllocator slot_allocator;
513 _DBUS_DEFINE_GLOBAL_LOCK (pending_call_slots);
516 * Allocates an integer ID to be used for storing application-specific
517 * data on any DBusPendingCall. The allocated ID may then be used
518 * with dbus_pending_call_set_data() and dbus_pending_call_get_data().
519 * The passed-in slot must be initialized to -1, and is filled in
520 * with the slot ID. If the passed-in slot is not -1, it's assumed
521 * to be already allocated, and its refcount is incremented.
523 * The allocated slot is global, i.e. all DBusPendingCall objects will
524 * have a slot with the given integer ID reserved.
526 * @param slot_p address of a global variable storing the slot
527 * @returns #FALSE on failure (no memory)
530 dbus_pending_call_allocate_data_slot (dbus_int32_t *slot_p)
532 return _dbus_data_slot_allocator_alloc (&slot_allocator,
533 _DBUS_LOCK_NAME (pending_call_slots),
538 * Deallocates a global ID for #DBusPendingCall data slots.
539 * dbus_pending_call_get_data() and dbus_pending_call_set_data() may
540 * no longer be used with this slot. Existing data stored on existing
541 * DBusPendingCall objects will be freed when the #DBusPendingCall is
542 * finalized, but may not be retrieved (and may only be replaced if
543 * someone else reallocates the slot). When the refcount on the
544 * passed-in slot reaches 0, it is set to -1.
546 * @param slot_p address storing the slot to deallocate
549 dbus_pending_call_free_data_slot (dbus_int32_t *slot_p)
551 _dbus_return_if_fail (*slot_p >= 0);
553 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
557 * Stores a pointer on a #DBusPendingCall, along
558 * with an optional function to be used for freeing
559 * the data when the data is set again, or when
560 * the pending call is finalized. The slot number
561 * must have been allocated with dbus_pending_call_allocate_data_slot().
563 * @param pending the pending_call
564 * @param slot the slot number
565 * @param data the data to store
566 * @param free_data_func finalizer function for the data
567 * @returns #TRUE if there was enough memory to store the data
570 dbus_pending_call_set_data (DBusPendingCall *pending,
573 DBusFreeFunction free_data_func)
575 DBusFreeFunction old_free_func;
579 _dbus_return_val_if_fail (pending != NULL, FALSE);
580 _dbus_return_val_if_fail (slot >= 0, FALSE);
582 retval = _dbus_data_slot_list_set (&slot_allocator,
584 slot, data, free_data_func,
585 &old_free_func, &old_data);
590 (* old_free_func) (old_data);
597 * Retrieves data previously set with dbus_pending_call_set_data().
598 * The slot must still be allocated (must not have been freed).
600 * @param pending the pending_call
601 * @param slot the slot to get data from
602 * @returns the data, or #NULL if not found
605 dbus_pending_call_get_data (DBusPendingCall *pending,
610 _dbus_return_val_if_fail (pending != NULL, NULL);
612 res = _dbus_data_slot_list_get (&slot_allocator,
621 #ifdef DBUS_BUILD_TESTS
624 * @ingroup DBusPendingCallInternals
625 * Unit test for DBusPendingCall.
627 * @returns #TRUE on success.
630 _dbus_pending_call_test (const char *test_data_dir)
635 #endif /* DBUS_BUILD_TESTS */