D-Bus 1.14.10
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{
76 dbus_uint32_t reply_serial;
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
104/* protected by _DBUS_LOCK_pending_call_slots */
105static dbus_int32_t notify_user_data_slot = -1;
106
119 int timeout_milliseconds,
120 DBusTimeoutHandler timeout_handler)
121{
122 DBusPendingCall *pending;
123 DBusTimeout *timeout;
124
125 _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
126
127 if (timeout_milliseconds == -1)
128 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
129
130 if (!dbus_pending_call_allocate_data_slot (&notify_user_data_slot))
131 return NULL;
132
133 pending = dbus_new0 (DBusPendingCall, 1);
134
135 if (pending == NULL)
136 {
137 dbus_pending_call_free_data_slot (&notify_user_data_slot);
138 return NULL;
139 }
140
141 if (timeout_milliseconds != DBUS_TIMEOUT_INFINITE)
142 {
143 timeout = _dbus_timeout_new (timeout_milliseconds,
144 timeout_handler,
145 pending, NULL);
146
147 if (timeout == NULL)
148 {
149 dbus_pending_call_free_data_slot (&notify_user_data_slot);
150 dbus_free (pending);
151 return NULL;
152 }
153
154 pending->timeout = timeout;
155 }
156 else
157 {
158 pending->timeout = NULL;
159 }
160
161 _dbus_atomic_inc (&pending->refcount);
162 pending->connection = connection;
164
166
167 _dbus_pending_call_trace_ref (pending, 0, 1, "new_unlocked");
168
169 return pending;
170}
171
180void
182 DBusMessage *message)
183{
184 if (message == NULL)
185 {
186 message = pending->timeout_link->data;
187 _dbus_list_clear (&pending->timeout_link);
188 }
189 else
190 dbus_message_ref (message);
191
192 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
193 message,
195 "method return" :
197 "error" : "other type",
198 pending->reply_serial);
199
200 _dbus_assert (pending->reply == NULL);
202 pending->reply = message;
203}
204
215void
217{
218 _dbus_assert (!pending->completed);
219
220 pending->completed = TRUE;
221}
222
232void
234{
235 _dbus_assert (pending->completed);
236
237 if (pending->function)
238 {
239 void *user_data;
240 user_data = dbus_pending_call_get_data (pending,
241 notify_user_data_slot);
242
243 (* pending->function) (pending, user_data);
244 }
245}
246
254void
256 DBusConnection *connection)
257{
258 _dbus_assert (connection == pending->connection);
259
260 if (pending->timeout_link)
261 {
263 pending->timeout_link);
264 pending->timeout_link = NULL;
265 }
266}
267
276{
277 _dbus_assert (pending != NULL);
278
279 return pending->timeout_added;
280}
281
282
289void
291 dbus_bool_t is_added)
292{
293 _dbus_assert (pending != NULL);
294
295 pending->timeout_added = is_added;
296}
297
298
307{
308 _dbus_assert (pending != NULL);
309
310 return pending->timeout;
311}
312
319dbus_uint32_t
321{
322 _dbus_assert (pending != NULL);
323
324 return pending->reply_serial;
325}
326
333void
335 dbus_uint32_t serial)
336{
337 _dbus_assert (pending != NULL);
338 _dbus_assert (pending->reply_serial == 0);
339
340 pending->reply_serial = serial;
341}
342
351{
352 _dbus_assert (pending != NULL);
353
354 CONNECTION_LOCK (pending->connection);
355 return pending->connection;
356}
357
366{
367 _dbus_assert (pending != NULL);
368
369 return pending->connection;
370}
371
382 DBusMessage *message,
383 dbus_uint32_t serial)
384{
385 DBusList *reply_link;
386 DBusMessage *reply;
387
389 "Did not receive a reply. Possible causes include: "
390 "the remote application did not send a reply, "
391 "the message bus security policy blocked the reply, "
392 "the reply timeout expired, or "
393 "the network connection was broken.");
394 if (reply == NULL)
395 return FALSE;
396
397 reply_link = _dbus_list_alloc_link (reply);
398 if (reply_link == NULL)
399 {
400 /* it's OK to unref this, nothing that could have attached a callback
401 * has ever seen it */
402 dbus_message_unref (reply);
403 return FALSE;
404 }
405
406 pending->timeout_link = reply_link;
407
409
410 return TRUE;
411}
412
422{
423 dbus_int32_t old_refcount;
424
425 old_refcount = _dbus_atomic_inc (&pending->refcount);
426 _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
427 "ref_unlocked");
428
429 return pending;
430}
431
432
433static void
434_dbus_pending_call_last_unref (DBusPendingCall *pending)
435{
436 DBusConnection *connection;
437
438 /* If we get here, we should be already detached
439 * from the connection, or never attached.
440 */
441 _dbus_assert (!pending->timeout_added);
442
443 connection = pending->connection;
444
445 /* this assumes we aren't holding connection lock... */
447
448 if (pending->timeout != NULL)
449 _dbus_timeout_unref (pending->timeout);
450
451 if (pending->timeout_link)
452 {
455 pending->timeout_link = NULL;
456 }
457
458 if (pending->reply)
459 {
460 dbus_message_unref (pending->reply);
461 pending->reply = NULL;
462 }
463
464 dbus_free (pending);
465
466 dbus_pending_call_free_data_slot (&notify_user_data_slot);
467
468 /* connection lock should not be held. */
469 /* Free the connection last to avoid a weird state while
470 * calling out to application code where the pending exists
471 * but not the connection.
472 */
473 dbus_connection_unref (connection);
474}
475
483void
485{
486 dbus_int32_t old_refcount;
487
488 old_refcount = _dbus_atomic_dec (&pending->refcount);
489 _dbus_assert (old_refcount > 0);
490 _dbus_pending_call_trace_ref (pending, old_refcount,
491 old_refcount - 1, "unref_and_unlock");
492
493 CONNECTION_UNLOCK (pending->connection);
494
495 if (old_refcount == 1)
496 _dbus_pending_call_last_unref (pending);
497}
498
508{
509 return pending->completed;
510}
511
512static DBusDataSlotAllocator slot_allocator =
513 _DBUS_DATA_SLOT_ALLOCATOR_INIT (_DBUS_LOCK_NAME (pending_call_slots));
514
530 dbus_int32_t slot,
531 void *data,
532 DBusFreeFunction free_data_func)
533{
534 DBusFreeFunction old_free_func;
535 void *old_data;
536 dbus_bool_t retval;
537
538 retval = _dbus_data_slot_list_set (&slot_allocator,
539 &pending->slot_list,
540 slot, data, free_data_func,
541 &old_free_func, &old_data);
542
543 /* Drop locks to call out to app code */
544 CONNECTION_UNLOCK (pending->connection);
545
546 if (retval)
547 {
548 if (old_free_func)
549 (* old_free_func) (old_data);
550 }
551
552 CONNECTION_LOCK (pending->connection);
553
554 return retval;
555}
556
605{
606 dbus_int32_t old_refcount;
607
608 _dbus_return_val_if_fail (pending != NULL, NULL);
609
610 old_refcount = _dbus_atomic_inc (&pending->refcount);
611 _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
612 "ref");
613
614 return pending;
615}
616
623void
625{
626 dbus_int32_t old_refcount;
627
628 _dbus_return_if_fail (pending != NULL);
629
630 old_refcount = _dbus_atomic_dec (&pending->refcount);
631 _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount - 1,
632 "unref");
633
634 if (old_refcount == 1)
635 _dbus_pending_call_last_unref(pending);
636}
637
651 void *user_data,
652 DBusFreeFunction free_user_data)
653{
654 dbus_bool_t ret = FALSE;
655
656 _dbus_return_val_if_fail (pending != NULL, FALSE);
657
658 CONNECTION_LOCK (pending->connection);
659
660 /* could invoke application code! */
661 if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
662 user_data, free_user_data))
663 goto out;
664
665 pending->function = function;
666 ret = TRUE;
667
668out:
669 CONNECTION_UNLOCK (pending->connection);
670
671 return ret;
672}
673
689void
691{
692 _dbus_return_if_fail (pending != NULL);
693
695 pending);
696}
697
707{
708 dbus_bool_t completed;
709
710 _dbus_return_val_if_fail (pending != NULL, FALSE);
711
712 CONNECTION_LOCK (pending->connection);
713 completed = pending->completed;
714 CONNECTION_UNLOCK (pending->connection);
715
716 return completed;
717}
718
730{
731 DBusMessage *message;
732
733 _dbus_return_val_if_fail (pending != NULL, NULL);
734 _dbus_return_val_if_fail (pending->completed, NULL);
735 _dbus_return_val_if_fail (pending->reply != NULL, NULL);
736
737 CONNECTION_LOCK (pending->connection);
738
739 message = pending->reply;
740 pending->reply = NULL;
741
742 CONNECTION_UNLOCK (pending->connection);
743
744 _dbus_message_trace_ref (message, -1, -1, "dbus_pending_call_steal_reply");
745 return message;
746}
747
763void
765{
766 _dbus_return_if_fail (pending != NULL);
767
769}
770
787{
788 _dbus_return_val_if_fail (slot_p != NULL, FALSE);
789
790 return _dbus_data_slot_allocator_alloc (&slot_allocator,
791 slot_p);
792}
793
805void
807{
808 _dbus_return_if_fail (slot_p != NULL);
809 _dbus_return_if_fail (*slot_p >= 0);
810
811 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
812}
813
829 dbus_int32_t slot,
830 void *data,
831 DBusFreeFunction free_data_func)
832{
833 dbus_bool_t retval;
834
835 _dbus_return_val_if_fail (pending != NULL, FALSE);
836 _dbus_return_val_if_fail (slot >= 0, FALSE);
837
838
839 CONNECTION_LOCK (pending->connection);
840 retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
841 CONNECTION_UNLOCK (pending->connection);
842 return retval;
843}
844
853void*
855 dbus_int32_t slot)
856{
857 void *res;
858
859 _dbus_return_val_if_fail (pending != NULL, NULL);
860
861 CONNECTION_LOCK (pending->connection);
862 res = _dbus_data_slot_list_get (&slot_allocator,
863 &pending->slot_list,
864 slot);
865 CONNECTION_UNLOCK (pending->connection);
866
867 return res;
868}
869
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:70
#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:255
void _dbus_list_clear(DBusList **list)
Frees all links in the list and sets the list head to NULL.
Definition: dbus-list.c:543
DBusList * _dbus_list_alloc_link(void *data)
Allocates a linked list node.
Definition: dbus-list.c:243
#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:63
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:692
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
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
An atomic integer safe to increment or decrement from multiple threads.
Definition: dbus-sysdeps.h:324
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