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 1.2
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.h"
27 #include "dbus-list.h"
28 #include "dbus-threads.h"
29 #include "dbus-test.h"
32 * @defgroup DBusPendingCallInternals DBusPendingCall implementation details
33 * @ingroup DBusInternals
34 * @brief DBusPendingCall private implementation details.
36 * The guts of DBusPendingCall and its methods.
41 static dbus_int32_t notify_user_data_slot = -1;
44 * Creates a new pending reply object.
46 * @param connection connection where reply will arrive
47 * @param timeout_milliseconds length of timeout, -1 for default
48 * @param timeout_handler timeout handler, takes pending call as data
49 * @returns a new #DBusPendingCall or #NULL if no memory.
52 _dbus_pending_call_new (DBusConnection *connection,
53 int timeout_milliseconds,
54 DBusTimeoutHandler timeout_handler)
56 DBusPendingCall *pending;
59 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
61 if (timeout_milliseconds == -1)
62 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
64 if (!dbus_pending_call_allocate_data_slot (¬ify_user_data_slot))
67 pending = dbus_new (DBusPendingCall, 1);
71 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
75 timeout = _dbus_timeout_new (timeout_milliseconds,
81 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
86 pending->refcount.value = 1;
87 pending->connection = connection;
88 pending->timeout = timeout;
90 _dbus_data_slot_list_init (&pending->slot_list);
96 * Calls notifier function for the pending call
97 * and sets the call to completed.
99 * @param pending the pending call
103 _dbus_pending_call_notify (DBusPendingCall *pending)
105 pending->completed = TRUE;
107 if (pending->function)
110 user_data = dbus_pending_call_get_data (pending,
111 notify_user_data_slot);
113 (* pending->function) (pending, user_data);
120 * @defgroup DBusPendingCall DBusPendingCall
122 * @brief Pending reply to a method call message
124 * A DBusPendingCall is an object representing an
125 * expected reply. A #DBusPendingCall can be created
126 * when you send a message that should have a reply.
132 * @typedef DBusPendingCall
134 * Opaque data type representing a message pending.
138 * Increments the reference count on a pending call.
140 * @param pending the pending call object
143 dbus_pending_call_ref (DBusPendingCall *pending)
145 _dbus_return_if_fail (pending != NULL);
147 _dbus_atomic_inc (&pending->refcount);
151 * Decrements the reference count on a pending call,
152 * freeing it if the count reaches 0.
154 * @param pending the pending call object
157 dbus_pending_call_unref (DBusPendingCall *pending)
159 dbus_bool_t last_unref;
161 _dbus_return_if_fail (pending != NULL);
163 last_unref = (_dbus_atomic_dec (&pending->refcount) == 1);
167 /* If we get here, we should be already detached
168 * from the connection, or never attached.
170 _dbus_assert (pending->connection == NULL);
171 _dbus_assert (!pending->timeout_added);
173 /* this assumes we aren't holding connection lock... */
174 _dbus_data_slot_list_free (&pending->slot_list);
176 if (pending->timeout != NULL)
177 _dbus_timeout_unref (pending->timeout);
179 if (pending->timeout_link)
181 dbus_message_unref ((DBusMessage *)pending->timeout_link->data);
182 _dbus_list_free_link (pending->timeout_link);
183 pending->timeout_link = NULL;
188 dbus_message_unref (pending->reply);
189 pending->reply = NULL;
194 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
199 * Sets a notification function to be called when the reply is
200 * received or the pending call times out.
202 * @param pending the pending call
203 * @param function notifier function
204 * @param user_data data to pass to notifier function
205 * @param free_user_data function to free the user data
206 * @returns #FALSE if not enough memory
209 dbus_pending_call_set_notify (DBusPendingCall *pending,
210 DBusPendingCallNotifyFunction function,
212 DBusFreeFunction free_user_data)
214 _dbus_return_val_if_fail (pending != NULL, FALSE);
216 /* could invoke application code! */
217 if (!dbus_pending_call_set_data (pending, notify_user_data_slot,
218 user_data, free_user_data))
221 pending->function = function;
227 * Cancels the pending call, such that any reply
228 * or error received will just be ignored.
229 * Drops at least one reference to the #DBusPendingCall
230 * so will free the call if nobody else is holding
233 * @param pending the pending call
236 dbus_pending_call_cancel (DBusPendingCall *pending)
238 if (pending->connection)
239 _dbus_connection_remove_pending_call (pending->connection,
244 * Checks whether the pending call has received a reply
247 * @todo not thread safe? I guess it has to lock though it sucks
249 * @param pending the pending call
250 * @returns #TRUE if a reply has been received */
252 dbus_pending_call_get_completed (DBusPendingCall *pending)
254 return pending->completed;
258 * Gets the reply, or returns #NULL if none has been received yet. The
259 * reference count is not incremented on the returned message, so you
260 * have to keep a reference count on the pending call (or add one
263 * @todo not thread safe? I guess it has to lock though it sucks
264 * @todo maybe to make this threadsafe, it should be steal_reply(), i.e. only one thread can ever get the message
266 * @param pending the pending call
267 * @returns the reply message or #NULL.
270 dbus_pending_call_get_reply (DBusPendingCall *pending)
272 return pending->reply;
276 * Block until the pending call is completed. The blocking is as with
277 * dbus_connection_send_with_reply_and_block(); it does not enter the
278 * main loop or process other messages, it simply waits for the reply
281 * If the pending call is already completed, this function returns
284 * @todo when you start blocking, the timeout is reset, but it should
285 * really only use time remaining since the pending call was created.
287 * @param pending the pending call
290 dbus_pending_call_block (DBusPendingCall *pending)
292 DBusMessage *message;
294 if (dbus_pending_call_get_completed (pending))
297 message = _dbus_connection_block_for_reply (pending->connection,
298 pending->reply_serial,
299 dbus_timeout_get_interval (pending->timeout));
301 _dbus_connection_lock (pending->connection);
302 _dbus_pending_call_complete_and_unlock (pending, message);
303 dbus_message_unref (message);
306 static DBusDataSlotAllocator slot_allocator;
307 _DBUS_DEFINE_GLOBAL_LOCK (pending_call_slots);
310 * Allocates an integer ID to be used for storing application-specific
311 * data on any DBusPendingCall. The allocated ID may then be used
312 * with dbus_pending_call_set_data() and dbus_pending_call_get_data().
313 * The passed-in slot must be initialized to -1, and is filled in
314 * with the slot ID. If the passed-in slot is not -1, it's assumed
315 * to be already allocated, and its refcount is incremented.
317 * The allocated slot is global, i.e. all DBusPendingCall objects will
318 * have a slot with the given integer ID reserved.
320 * @param slot_p address of a global variable storing the slot
321 * @returns #FALSE on failure (no memory)
324 dbus_pending_call_allocate_data_slot (dbus_int32_t *slot_p)
326 return _dbus_data_slot_allocator_alloc (&slot_allocator,
327 _DBUS_LOCK_NAME (pending_call_slots),
332 * Deallocates a global ID for #DBusPendingCall data slots.
333 * dbus_pending_call_get_data() and dbus_pending_call_set_data() may
334 * no longer be used with this slot. Existing data stored on existing
335 * DBusPendingCall objects will be freed when the #DBusPendingCall is
336 * finalized, but may not be retrieved (and may only be replaced if
337 * someone else reallocates the slot). When the refcount on the
338 * passed-in slot reaches 0, it is set to -1.
340 * @param slot_p address storing the slot to deallocate
343 dbus_pending_call_free_data_slot (dbus_int32_t *slot_p)
345 _dbus_return_if_fail (*slot_p >= 0);
347 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
351 * Stores a pointer on a #DBusPendingCall, along
352 * with an optional function to be used for freeing
353 * the data when the data is set again, or when
354 * the pending call is finalized. The slot number
355 * must have been allocated with dbus_pending_call_allocate_data_slot().
357 * @param pending the pending_call
358 * @param slot the slot number
359 * @param data the data to store
360 * @param free_data_func finalizer function for the data
361 * @returns #TRUE if there was enough memory to store the data
364 dbus_pending_call_set_data (DBusPendingCall *pending,
367 DBusFreeFunction free_data_func)
369 DBusFreeFunction old_free_func;
373 _dbus_return_val_if_fail (pending != NULL, FALSE);
374 _dbus_return_val_if_fail (slot >= 0, FALSE);
376 retval = _dbus_data_slot_list_set (&slot_allocator,
378 slot, data, free_data_func,
379 &old_free_func, &old_data);
384 (* old_free_func) (old_data);
391 * Retrieves data previously set with dbus_pending_call_set_data().
392 * The slot must still be allocated (must not have been freed).
394 * @param pending the pending_call
395 * @param slot the slot to get data from
396 * @returns the data, or #NULL if not found
399 dbus_pending_call_get_data (DBusPendingCall *pending,
404 _dbus_return_val_if_fail (pending != NULL, NULL);
406 res = _dbus_data_slot_list_get (&slot_allocator,
415 #ifdef DBUS_BUILD_TESTS
418 * @ingroup DBusPendingCallInternals
419 * Unit test for DBusPendingCall.
421 * @returns #TRUE on success.
424 _dbus_pending_call_test (const char *test_data_dir)
429 #endif /* DBUS_BUILD_TESTS */