D-Bus 1.12.4
dbus-pending-call.c
1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2/* dbus-pending-call.c Object representing a call in progress.
3 *
4 * Copyright (C) 2002, 2003 Red Hat Inc.
5 *
6 * Licensed under the Academic Free License version 2.1
7 *
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.
12 *
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.
17 *
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
21 *
22 */
23
24#include <config.h>
25#include "dbus-internals.h"
26#include "dbus-connection-internal.h"
27#include "dbus-message-internal.h"
28#include "dbus-pending-call-internal.h"
29#include "dbus-pending-call.h"
30#include "dbus-list.h"
31#include "dbus-threads.h"
32#include "dbus-test.h"
33
53#define CONNECTION_LOCK(connection) _dbus_connection_lock(connection)
57#define CONNECTION_UNLOCK(connection) _dbus_connection_unlock(connection)
58
63{
83 unsigned int completed : 1;
87 unsigned int timeout_added : 1;
88};
89
90static void
91_dbus_pending_call_trace_ref (DBusPendingCall *pending_call,
92 int old_refcount,
93 int new_refcount,
94 const char *why)
95{
96#ifdef DBUS_ENABLE_VERBOSE_MODE
97 static int enabled = -1;
98
99 _dbus_trace_ref ("DBusPendingCall", pending_call, old_refcount,
100 new_refcount, why, "DBUS_PENDING_CALL_TRACE", &enabled);
101#endif
102}
103
104static dbus_int32_t notify_user_data_slot = -1;
105
118 int timeout_milliseconds,
119 DBusTimeoutHandler timeout_handler)
120{
121 DBusPendingCall *pending;
122 DBusTimeout *timeout;
123
124 _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
125
126 if (timeout_milliseconds == -1)
127 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
128
129 if (!dbus_pending_call_allocate_data_slot (&notify_user_data_slot))
130 return NULL;
131
132 pending = dbus_new0 (DBusPendingCall, 1);
133
134 if (pending == NULL)
135 {
136 dbus_pending_call_free_data_slot (&notify_user_data_slot);
137 return NULL;
138 }
139
140 if (timeout_milliseconds != DBUS_TIMEOUT_INFINITE)
141 {
142 timeout = _dbus_timeout_new (timeout_milliseconds,
143 timeout_handler,
144 pending, NULL);
145
146 if (timeout == NULL)
147 {
148 dbus_pending_call_free_data_slot (&notify_user_data_slot);
149 dbus_free (pending);
150 return NULL;
151 }
152
153 pending->timeout = timeout;
154 }
155 else
156 {
157 pending->timeout = NULL;
158 }
159
160 _dbus_atomic_inc (&pending->refcount);
161 pending->connection = connection;
163
165
166 _dbus_pending_call_trace_ref (pending, 0, 1, "new_unlocked");
167
168 return pending;
169}
170
179void
181 DBusMessage *message)
182{
183 if (message == NULL)
184 {
185 message = pending->timeout_link->data;
186 _dbus_list_clear (&pending->timeout_link);
187 }
188 else
189 dbus_message_ref (message);
190
191 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
192 message,
194 "method return" :
196 "error" : "other type",
197 pending->reply_serial);
198
199 _dbus_assert (pending->reply == NULL);
201 pending->reply = message;
202}
203
214void
216{
217 _dbus_assert (!pending->completed);
218
219 pending->completed = TRUE;
220}
221
231void
233{
234 _dbus_assert (pending->completed);
235
236 if (pending->function)
237 {
238 void *user_data;
239 user_data = dbus_pending_call_get_data (pending,
240 notify_user_data_slot);
241
242 (* pending->function) (pending, user_data);
243 }
244}
245
253void
255 DBusConnection *connection)
256{
257 _dbus_assert (connection == pending->connection);
258
259 if (pending->timeout_link)
260 {
262 pending->timeout_link);
263 pending->timeout_link = NULL;
264 }
265}
266
275{
276 _dbus_assert (pending != NULL);
277
278 return pending->timeout_added;
279}
280
281
288void
290 dbus_bool_t is_added)
291{
292 _dbus_assert (pending != NULL);
293
294 pending->timeout_added = is_added;
295}
296
297
306{
307 _dbus_assert (pending != NULL);
308
309 return pending->timeout;
310}
311
320{
321 _dbus_assert (pending != NULL);
322
323 return pending->reply_serial;
324}
325
332void
334 dbus_uint32_t serial)
335{
336 _dbus_assert (pending != NULL);
337 _dbus_assert (pending->reply_serial == 0);
338
339 pending->reply_serial = serial;
340}
341
350{
351 _dbus_assert (pending != NULL);
352
353 CONNECTION_LOCK (pending->connection);
354 return pending->connection;
355}
356
365{
366 _dbus_assert (pending != NULL);
367
368 return pending->connection;
369}
370
381 DBusMessage *message,
382 dbus_uint32_t serial)
383{
384 DBusList *reply_link;
385 DBusMessage *reply;
386
388 "Did not receive a reply. Possible causes include: "
389 "the remote application did not send a reply, "
390 "the message bus security policy blocked the reply, "
391 "the reply timeout expired, or "
392 "the network connection was broken.");
393 if (reply == NULL)
394 return FALSE;
395
396 reply_link = _dbus_list_alloc_link (reply);
397 if (reply_link == NULL)
398 {
399 /* it's OK to unref this, nothing that could have attached a callback
400 * has ever seen it */
401 dbus_message_unref (reply);
402 return FALSE;
403 }
404
405 pending->timeout_link = reply_link;
406
408
409 return TRUE;
410}
411
421{
422 dbus_int32_t old_refcount;
423
424 old_refcount = _dbus_atomic_inc (&pending->refcount);
425 _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
426 "ref_unlocked");
427
428 return pending;
429}
430
431
432static void
433_dbus_pending_call_last_unref (DBusPendingCall *pending)
434{
435 DBusConnection *connection;
436
437 /* If we get here, we should be already detached
438 * from the connection, or never attached.
439 */
440 _dbus_assert (!pending->timeout_added);
441
442 connection = pending->connection;
443
444 /* this assumes we aren't holding connection lock... */
446
447 if (pending->timeout != NULL)
448 _dbus_timeout_unref (pending->timeout);
449
450 if (pending->timeout_link)
451 {
454 pending->timeout_link = NULL;
455 }
456
457 if (pending->reply)
458 {
459 dbus_message_unref (pending->reply);
460 pending->reply = NULL;
461 }
462
463 dbus_free (pending);
464
465 dbus_pending_call_free_data_slot (&notify_user_data_slot);
466
467 /* connection lock should not be held. */
468 /* Free the connection last to avoid a weird state while
469 * calling out to application code where the pending exists
470 * but not the connection.
471 */
472 dbus_connection_unref (connection);
473}
474
482void
484{
485 dbus_int32_t old_refcount;
486
487 old_refcount = _dbus_atomic_dec (&pending->refcount);
488 _dbus_assert (old_refcount > 0);
489 _dbus_pending_call_trace_ref (pending, old_refcount,
490 old_refcount - 1, "unref_and_unlock");
491
492 CONNECTION_UNLOCK (pending->connection);
493
494 if (old_refcount == 1)
495 _dbus_pending_call_last_unref (pending);
496}
497
507{
508 return pending->completed;
509}
510
511static DBusDataSlotAllocator slot_allocator =
512 _DBUS_DATA_SLOT_ALLOCATOR_INIT (_DBUS_LOCK_NAME (pending_call_slots));
513
529 dbus_int32_t slot,
530 void *data,
531 DBusFreeFunction free_data_func)
532{
533 DBusFreeFunction old_free_func;
534 void *old_data;
535 dbus_bool_t retval;
536
537 retval = _dbus_data_slot_list_set (&slot_allocator,
538 &pending->slot_list,
539 slot, data, free_data_func,
540 &old_free_func, &old_data);
541
542 /* Drop locks to call out to app code */
543 CONNECTION_UNLOCK (pending->connection);
544
545 if (retval)
546 {
547 if (old_free_func)
548 (* old_free_func) (old_data);
549 }
550
551 CONNECTION_LOCK (pending->connection);
552
553 return retval;
554}
555
604{
605 dbus_int32_t old_refcount;
606
607 _dbus_return_val_if_fail (pending != NULL, NULL);
608
609 old_refcount = _dbus_atomic_inc (&pending->refcount);
610 _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
611 "ref");
612
613 return pending;
614}
615
622void
624{
625 dbus_int32_t old_refcount;
626
627 _dbus_return_if_fail (pending != NULL);
628
629 old_refcount = _dbus_atomic_dec (&pending->refcount);
630 _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount - 1,
631 "unref");
632
633 if (old_refcount == 1)
634 _dbus_pending_call_last_unref(pending);
635}
636
650 void *user_data,
651 DBusFreeFunction free_user_data)
652{
653 dbus_bool_t ret = FALSE;
654
655 _dbus_return_val_if_fail (pending != NULL, FALSE);
656
657 CONNECTION_LOCK (pending->connection);
658
659 /* could invoke application code! */
660 if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
661 user_data, free_user_data))
662 goto out;
663
664 pending->function = function;
665 ret = TRUE;
666
667out:
668 CONNECTION_UNLOCK (pending->connection);
669
670 return ret;
671}
672
688void
690{
691 _dbus_return_if_fail (pending != NULL);
692
694 pending);
695}
696
706{
707 dbus_bool_t completed;
708
709 _dbus_return_val_if_fail (pending != NULL, FALSE);
710
711 CONNECTION_LOCK (pending->connection);
712 completed = pending->completed;
713 CONNECTION_UNLOCK (pending->connection);
714
715 return completed;
716}
717
729{
730 DBusMessage *message;
731
732 _dbus_return_val_if_fail (pending != NULL, NULL);
733 _dbus_return_val_if_fail (pending->completed, NULL);
734 _dbus_return_val_if_fail (pending->reply != NULL, NULL);
735
736 CONNECTION_LOCK (pending->connection);
737
738 message = pending->reply;
739 pending->reply = NULL;
740
741 CONNECTION_UNLOCK (pending->connection);
742
743 _dbus_message_trace_ref (message, -1, -1, "dbus_pending_call_steal_reply");
744 return message;
745}
746
762void
764{
765 _dbus_return_if_fail (pending != NULL);
766
768}
769
786{
787 _dbus_return_val_if_fail (slot_p != NULL, FALSE);
788
789 return _dbus_data_slot_allocator_alloc (&slot_allocator,
790 slot_p);
791}
792
804void
806{
807 _dbus_return_if_fail (slot_p != NULL);
808 _dbus_return_if_fail (*slot_p >= 0);
809
810 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
811}
812
828 dbus_int32_t slot,
829 void *data,
830 DBusFreeFunction free_data_func)
831{
832 dbus_bool_t retval;
833
834 _dbus_return_val_if_fail (pending != NULL, FALSE);
835 _dbus_return_val_if_fail (slot >= 0, FALSE);
836
837
838 CONNECTION_LOCK (pending->connection);
839 retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
840 CONNECTION_UNLOCK (pending->connection);
841 return retval;
842}
843
852void*
854 dbus_int32_t slot)
855{
856 void *res;
857
858 _dbus_return_val_if_fail (pending != NULL, NULL);
859
860 CONNECTION_LOCK (pending->connection);
861 res = _dbus_data_slot_list_get (&slot_allocator,
862 &pending->slot_list,
863 slot);
864 CONNECTION_UNLOCK (pending->connection);
865
866 return res;
867}
868
void _dbus_connection_remove_pending_call(DBusConnection *connection, DBusPendingCall *pending)
Removes a pending call from the connection, such that the pending reply will be ignored.
void _dbus_connection_block_pending_call(DBusPendingCall *pending)
Blocks until a pending call times out or gets a reply.
void _dbus_connection_queue_synthesized_message_link(DBusConnection *connection, DBusList *link)
Adds a link + message to the incoming message queue.
DBUS_PRIVATE_EXPORT DBusConnection * _dbus_connection_ref_unlocked(DBusConnection *connection)
Increments the reference count of a DBusConnection.
void dbus_connection_unref(DBusConnection *connection)
Decrements the reference count of a DBusConnection, and finalizes it if the count reaches zero.
void(* DBusPendingCallNotifyFunction)(DBusPendingCall *pending, void *user_data)
Called when a pending call now has a reply available.
void _dbus_data_slot_allocator_free(DBusDataSlotAllocator *allocator, dbus_int32_t *slot_id_p)
Deallocates an ID previously allocated with _dbus_data_slot_allocator_alloc().
void _dbus_data_slot_list_init(DBusDataSlotList *list)
Initializes a slot list.
void _dbus_data_slot_list_free(DBusDataSlotList *list)
Frees the data slot list and all data slots contained in it, calling application-provided free functi...
void * _dbus_data_slot_list_get(DBusDataSlotAllocator *allocator, DBusDataSlotList *list, int slot)
Retrieves data previously set with _dbus_data_slot_list_set_data().
dbus_bool_t _dbus_data_slot_list_set(DBusDataSlotAllocator *allocator, DBusDataSlotList *list, int slot, void *data, DBusFreeFunction free_data_func, DBusFreeFunction *old_free_func, void **old_data)
Stores a pointer in the data slot list, along with an optional function to be used for freeing the da...
dbus_bool_t _dbus_data_slot_allocator_alloc(DBusDataSlotAllocator *allocator, dbus_int32_t *slot_id_p)
Allocates an integer ID to be used for storing data in a DBusDataSlotList.
Definition: dbus-dataslot.c:69
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define _DBUS_LOCK_NAME(name)
Expands to name of a global lock variable.
void _dbus_list_free_link(DBusList *link)
Frees a linked list node allocated with _dbus_list_alloc_link.
Definition: dbus-list.c:254
void _dbus_list_clear(DBusList **list)
Frees all links in the list and sets the list head to NULL.
Definition: dbus-list.c:542
DBusList * _dbus_list_alloc_link(void *data)
Allocates a linked list node.
Definition: dbus-list.c:242
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
Definition: dbus-memory.h:64
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:702
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:59
DBusMessage * dbus_message_new_error(DBusMessage *reply_to, const char *error_name, const char *error_message)
Creates a new message that is an error reply to another message.
int dbus_message_get_type(DBusMessage *message)
Gets the type of a message.
DBusMessage * dbus_message_ref(DBusMessage *message)
Increments the reference count of a DBusMessage.
dbus_uint32_t dbus_message_get_reply_serial(DBusMessage *message)
Returns the serial that the message is a reply to or 0 if none.
void dbus_message_unref(DBusMessage *message)
Decrements the reference count of a DBusMessage, freeing the message if the count reaches 0.
void _dbus_pending_call_finish_completion(DBusPendingCall *pending)
Call the notifier function for the pending call.
DBusConnection * _dbus_pending_call_get_connection_and_lock(DBusPendingCall *pending)
Gets the connection associated with this pending call.
DBusPendingCall * _dbus_pending_call_ref_unlocked(DBusPendingCall *pending)
Increments the reference count on a pending call, while the lock on its connection is already held.
void _dbus_pending_call_queue_timeout_error_unlocked(DBusPendingCall *pending, DBusConnection *connection)
If the pending call hasn't been timed out, add its timeout error reply to the connection's incoming m...
void _dbus_pending_call_unref_and_unlock(DBusPendingCall *pending)
Decrements the reference count on a pending call, freeing it if the count reaches 0.
dbus_bool_t _dbus_pending_call_get_completed_unlocked(DBusPendingCall *pending)
Checks whether the pending call has received a reply yet, or not.
void _dbus_pending_call_set_reply_unlocked(DBusPendingCall *pending, DBusMessage *message)
Sets the reply of a pending call with the given message, or if the message is NULL,...
void _dbus_pending_call_set_reply_serial_unlocked(DBusPendingCall *pending, dbus_uint32_t serial)
Sets the reply's serial number.
dbus_bool_t _dbus_pending_call_set_data_unlocked(DBusPendingCall *pending, dbus_int32_t slot, void *data, DBusFreeFunction free_data_func)
Stores a pointer on a DBusPendingCall, along with an optional function to be used for freeing the dat...
DBusPendingCall * _dbus_pending_call_new_unlocked(DBusConnection *connection, int timeout_milliseconds, DBusTimeoutHandler timeout_handler)
Creates a new pending reply object.
dbus_bool_t _dbus_pending_call_set_timeout_error_unlocked(DBusPendingCall *pending, DBusMessage *message, dbus_uint32_t serial)
Sets the reply message associated with the pending call to a timeout error.
#define CONNECTION_LOCK(connection)
Internals of DBusPendingCall.
#define CONNECTION_UNLOCK(connection)
shorter and more visible way to write _dbus_connection_unlock()
void _dbus_pending_call_start_completion_unlocked(DBusPendingCall *pending)
Sets the pending call to completed.
dbus_bool_t _dbus_pending_call_is_timeout_added_unlocked(DBusPendingCall *pending)
Checks to see if a timeout has been added.
dbus_uint32_t _dbus_pending_call_get_reply_serial_unlocked(DBusPendingCall *pending)
Gets the reply's serial number.
DBusConnection * _dbus_pending_call_get_connection_unlocked(DBusPendingCall *pending)
Gets the connection associated with this pending call.
DBusTimeout * _dbus_pending_call_get_timeout_unlocked(DBusPendingCall *pending)
Retrives the timeout.
void _dbus_pending_call_set_timeout_added_unlocked(DBusPendingCall *pending, dbus_bool_t is_added)
Sets wether the timeout has been added.
dbus_bool_t dbus_pending_call_set_notify(DBusPendingCall *pending, DBusPendingCallNotifyFunction function, void *user_data, DBusFreeFunction free_user_data)
Sets a notification function to be called when the reply is received or the pending call times out.
void dbus_pending_call_free_data_slot(dbus_int32_t *slot_p)
Deallocates a global ID for DBusPendingCall data slots.
#define DBUS_TIMEOUT_INFINITE
An integer constant representing an infinite timeout.
DBusPendingCall * dbus_pending_call_ref(DBusPendingCall *pending)
Increments the reference count on a pending call.
void * dbus_pending_call_get_data(DBusPendingCall *pending, dbus_int32_t slot)
Retrieves data previously set with dbus_pending_call_set_data().
DBusMessage * dbus_pending_call_steal_reply(DBusPendingCall *pending)
Gets the reply, or returns NULL if none has been received yet.
void dbus_pending_call_cancel(DBusPendingCall *pending)
Cancels the pending call, such that any reply or error received will just be ignored.
void dbus_pending_call_block(DBusPendingCall *pending)
Block until the pending call is completed.
dbus_bool_t dbus_pending_call_set_data(DBusPendingCall *pending, dbus_int32_t slot, void *data, DBusFreeFunction free_data_func)
Stores a pointer on a DBusPendingCall, along with an optional function to be used for freeing the dat...
dbus_bool_t dbus_pending_call_allocate_data_slot(dbus_int32_t *slot_p)
Allocates an integer ID to be used for storing application-specific data on any DBusPendingCall.
dbus_bool_t dbus_pending_call_get_completed(DBusPendingCall *pending)
Checks whether the pending call has received a reply yet, or not.
void dbus_pending_call_unref(DBusPendingCall *pending)
Decrements the reference count on a pending call, freeing it if the count reaches 0.
#define DBUS_MESSAGE_TYPE_ERROR
Message type of an error reply message, see dbus_message_get_type()
#define DBUS_MESSAGE_TYPE_METHOD_RETURN
Message type of a method return message, see dbus_message_get_type()
#define DBUS_ERROR_NO_REPLY
No reply to a message expecting one, usually means a timeout occurred.
dbus_int32_t _dbus_atomic_dec(DBusAtomic *atomic)
Atomically decrement an integer.
dbus_int32_t _dbus_atomic_inc(DBusAtomic *atomic)
Atomically increments an integer.
void _dbus_timeout_unref(DBusTimeout *timeout)
Decrements the reference count of a DBusTimeout object and finalizes the object if the count reaches ...
Definition: dbus-timeout.c:109
DBusTimeout * _dbus_timeout_new(int interval, DBusTimeoutHandler handler, void *data, DBusFreeFunction free_data_function)
Creates a new DBusTimeout, enabled by default.
Definition: dbus-timeout.c:64
dbus_bool_t(* DBusTimeoutHandler)(void *data)
function to run when the timeout is handled
Definition: dbus-timeout.h:41
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
int dbus_int32_t
A 32-bit signed integer on all platforms.
unsigned int dbus_uint32_t
A 32-bit unsigned integer on all platforms.
An atomic integer safe to increment or decrement from multiple threads.
Definition: dbus-sysdeps.h:281
Implementation details of DBusConnection.
An allocator that tracks a set of slot IDs.
Definition: dbus-dataslot.h:56
Data structure that stores the actual user data set at a given slot.
Definition: dbus-dataslot.h:70
A node in a linked list.
Definition: dbus-list.h:35
void * data
Data stored at this element.
Definition: dbus-list.h:38
Internals of DBusMessage.
Implementation details of DBusPendingCall - all fields are private.
DBusPendingCallNotifyFunction function
Notifier when reply arrives.
DBusMessage * reply
Reply (after we've received it)
DBusList * timeout_link
Preallocated timeout response.
DBusAtomic refcount
reference count
unsigned int timeout_added
TRUE if we have added the timeout.
dbus_uint32_t reply_serial
Expected serial of reply.
unsigned int completed
TRUE if some thread has taken responsibility for completing this pending call: either the pending cal...
DBusDataSlotList slot_list
Data stored by allocated integer ID.
DBusConnection * connection
Connections we're associated with.
DBusTimeout * timeout
Timeout.
Internals of DBusTimeout.
Definition: dbus-timeout.c:41