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-pending-call-internal.h"
28 #include "dbus-pending-call.h"
29 #include "dbus-list.h"
30 #include "dbus-threads.h"
31 #include "dbus-test.h"
32
33 /**
34 * @defgroup DBusPendingCallInternals DBusPendingCall implementation details
35 * @ingroup DBusInternals
36 * @brief DBusPendingCall private implementation details.
37 *
38 * The guts of DBusPendingCall and its methods.
39 *
40 * @{
41 */
42
43 /**
44 * @brief Internals of DBusPendingCall
45 *
46 * Opaque object representing a reply message that we're waiting for.
47 */
48
49 /**
50 * shorter and more visible way to write _dbus_connection_lock()
51 */
52 #define CONNECTION_LOCK(connection) _dbus_connection_lock(connection)
53 /**
54 * shorter and more visible way to write _dbus_connection_unlock()
55 */
56 #define CONNECTION_UNLOCK(connection) _dbus_connection_unlock(connection)
57
58 /**
59 * Implementation details of #DBusPendingCall - all fields are private.
60 */
61 struct DBusPendingCall
62 {
63 DBusAtomic refcount; /**< reference count */
64
65 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */
66
67 DBusPendingCallNotifyFunction function; /**< Notifier when reply arrives. */
68
69 DBusConnection *connection; /**< Connections we're associated with */
70 DBusMessage *reply; /**< Reply (after we've received it) */
71 DBusTimeout *timeout; /**< Timeout */
72
73 DBusList *timeout_link; /**< Preallocated timeout response */
74
75 dbus_uint32_t reply_serial; /**< Expected serial of reply */
76
77 unsigned int completed : 1; /**< TRUE if completed */
78 unsigned int timeout_added : 1; /**< Have added the timeout */
79 };
80
81 static dbus_int32_t notify_user_data_slot = -1;
82
83 /**
84 * Creates a new pending reply object.
85 *
86 * @param connection connection where reply will arrive
87 * @param timeout_milliseconds length of timeout, -1 for default, INT_MAX for no timeout
88 * @param timeout_handler timeout handler, takes pending call as data
89 * @returns a new #DBusPendingCall or #NULL if no memory.
90 */
91 DBusPendingCall*
_dbus_pending_call_new_unlocked(DBusConnection * connection,int timeout_milliseconds,DBusTimeoutHandler timeout_handler)92 _dbus_pending_call_new_unlocked (DBusConnection *connection,
93 int timeout_milliseconds,
94 DBusTimeoutHandler timeout_handler)
95 {
96 DBusPendingCall *pending;
97 DBusTimeout *timeout;
98
99 _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
100
101 if (timeout_milliseconds == -1)
102 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
103
104 if (!dbus_pending_call_allocate_data_slot (¬ify_user_data_slot))
105 return NULL;
106
107 pending = dbus_new0 (DBusPendingCall, 1);
108
109 if (pending == NULL)
110 {
111 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
112 return NULL;
113 }
114
115 if (timeout_milliseconds != _DBUS_INT_MAX)
116 {
117 timeout = _dbus_timeout_new (timeout_milliseconds,
118 timeout_handler,
119 pending, NULL);
120
121 if (timeout == NULL)
122 {
123 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
124 dbus_free (pending);
125 return NULL;
126 }
127
128 pending->timeout = timeout;
129 }
130 else
131 {
132 pending->timeout = NULL;
133 }
134
135 pending->refcount.value = 1;
136 pending->connection = connection;
137 _dbus_connection_ref_unlocked (pending->connection);
138
139 _dbus_data_slot_list_init (&pending->slot_list);
140
141 return pending;
142 }
143
144 /**
145 * Sets the reply of a pending call with the given message,
146 * or if the message is #NULL, by timing out the pending call.
147 *
148 * @param pending the pending call
149 * @param message the message to complete the call with, or #NULL
150 * to time out the call
151 */
152 void
_dbus_pending_call_set_reply_unlocked(DBusPendingCall * pending,DBusMessage * message)153 _dbus_pending_call_set_reply_unlocked (DBusPendingCall *pending,
154 DBusMessage *message)
155 {
156 if (message == NULL)
157 {
158 message = pending->timeout_link->data;
159 _dbus_list_clear (&pending->timeout_link);
160 }
161 else
162 dbus_message_ref (message);
163
164 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
165 message,
166 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
167 "method return" :
168 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
169 "error" : "other type",
170 pending->reply_serial);
171
172 _dbus_assert (pending->reply == NULL);
173 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
174 pending->reply = message;
175 }
176
177 /**
178 * Calls notifier function for the pending call
179 * and sets the call to completed.
180 *
181 * @param pending the pending call
182 *
183 */
184 void
_dbus_pending_call_complete(DBusPendingCall * pending)185 _dbus_pending_call_complete (DBusPendingCall *pending)
186 {
187 _dbus_assert (!pending->completed);
188
189 pending->completed = TRUE;
190
191 if (pending->function)
192 {
193 void *user_data;
194 user_data = dbus_pending_call_get_data (pending,
195 notify_user_data_slot);
196
197 (* pending->function) (pending, user_data);
198 }
199 }
200
201 /**
202 * If the pending call hasn't been timed out, add its timeout
203 * error reply to the connection's incoming message queue.
204 *
205 * @param pending the pending call
206 * @param connection the connection the call was sent to
207 */
208 void
_dbus_pending_call_queue_timeout_error_unlocked(DBusPendingCall * pending,DBusConnection * connection)209 _dbus_pending_call_queue_timeout_error_unlocked (DBusPendingCall *pending,
210 DBusConnection *connection)
211 {
212 _dbus_assert (connection == pending->connection);
213
214 if (pending->timeout_link)
215 {
216 _dbus_connection_queue_synthesized_message_link (connection,
217 pending->timeout_link);
218 pending->timeout_link = NULL;
219 }
220 }
221
222 /**
223 * Checks to see if a timeout has been added
224 *
225 * @param pending the pending_call
226 * @returns #TRUE if there is a timeout or #FALSE if not
227 */
228 dbus_bool_t
_dbus_pending_call_is_timeout_added_unlocked(DBusPendingCall * pending)229 _dbus_pending_call_is_timeout_added_unlocked (DBusPendingCall *pending)
230 {
231 _dbus_assert (pending != NULL);
232
233 return pending->timeout_added;
234 }
235
236
237 /**
238 * Sets wether the timeout has been added
239 *
240 * @param pending the pending_call
241 * @param is_added whether or not a timeout is added
242 */
243 void
_dbus_pending_call_set_timeout_added_unlocked(DBusPendingCall * pending,dbus_bool_t is_added)244 _dbus_pending_call_set_timeout_added_unlocked (DBusPendingCall *pending,
245 dbus_bool_t is_added)
246 {
247 _dbus_assert (pending != NULL);
248
249 pending->timeout_added = is_added;
250 }
251
252
253 /**
254 * Retrives the timeout
255 *
256 * @param pending the pending_call
257 * @returns a timeout object or NULL if call has no timeout
258 */
259 DBusTimeout *
_dbus_pending_call_get_timeout_unlocked(DBusPendingCall * pending)260 _dbus_pending_call_get_timeout_unlocked (DBusPendingCall *pending)
261 {
262 _dbus_assert (pending != NULL);
263
264 return pending->timeout;
265 }
266
267 /**
268 * Gets the reply's serial number
269 *
270 * @param pending the pending_call
271 * @returns a serial number for the reply or 0
272 */
273 dbus_uint32_t
_dbus_pending_call_get_reply_serial_unlocked(DBusPendingCall * pending)274 _dbus_pending_call_get_reply_serial_unlocked (DBusPendingCall *pending)
275 {
276 _dbus_assert (pending != NULL);
277
278 return pending->reply_serial;
279 }
280
281 /**
282 * Sets the reply's serial number
283 *
284 * @param pending the pending_call
285 * @param serial the serial number
286 */
287 void
_dbus_pending_call_set_reply_serial_unlocked(DBusPendingCall * pending,dbus_uint32_t serial)288 _dbus_pending_call_set_reply_serial_unlocked (DBusPendingCall *pending,
289 dbus_uint32_t serial)
290 {
291 _dbus_assert (pending != NULL);
292 _dbus_assert (pending->reply_serial == 0);
293
294 pending->reply_serial = serial;
295 }
296
297 /**
298 * Gets the connection associated with this pending call.
299 *
300 * @param pending the pending_call
301 * @returns the connection associated with the pending call
302 */
303 DBusConnection *
_dbus_pending_call_get_connection_and_lock(DBusPendingCall * pending)304 _dbus_pending_call_get_connection_and_lock (DBusPendingCall *pending)
305 {
306 _dbus_assert (pending != NULL);
307
308 CONNECTION_LOCK (pending->connection);
309 return pending->connection;
310 }
311
312 /**
313 * Gets the connection associated with this pending call.
314 *
315 * @param pending the pending_call
316 * @returns the connection associated with the pending call
317 */
318 DBusConnection *
_dbus_pending_call_get_connection_unlocked(DBusPendingCall * pending)319 _dbus_pending_call_get_connection_unlocked (DBusPendingCall *pending)
320 {
321 _dbus_assert (pending != NULL);
322
323 return pending->connection;
324 }
325
326 /**
327 * Sets the reply message associated with the pending call to a timeout error
328 *
329 * @param pending the pending_call
330 * @param message the message we are sending the error reply to
331 * @param serial serial number for the reply
332 * @return #FALSE on OOM
333 */
334 dbus_bool_t
_dbus_pending_call_set_timeout_error_unlocked(DBusPendingCall * pending,DBusMessage * message,dbus_uint32_t serial)335 _dbus_pending_call_set_timeout_error_unlocked (DBusPendingCall *pending,
336 DBusMessage *message,
337 dbus_uint32_t serial)
338 {
339 DBusList *reply_link;
340 DBusMessage *reply;
341
342 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
343 "Did not receive a reply. Possible causes include: "
344 "the remote application did not send a reply, "
345 "the message bus security policy blocked the reply, "
346 "the reply timeout expired, or "
347 "the network connection was broken.");
348 if (reply == NULL)
349 return FALSE;
350
351 reply_link = _dbus_list_alloc_link (reply);
352 if (reply_link == NULL)
353 {
354 dbus_message_unref (reply);
355 return FALSE;
356 }
357
358 pending->timeout_link = reply_link;
359
360 _dbus_pending_call_set_reply_serial_unlocked (pending, serial);
361
362 return TRUE;
363 }
364
365 /**
366 * Increments the reference count on a pending call,
367 * while the lock on its connection is already held.
368 *
369 * @param pending the pending call object
370 * @returns the pending call object
371 */
372 DBusPendingCall *
_dbus_pending_call_ref_unlocked(DBusPendingCall * pending)373 _dbus_pending_call_ref_unlocked (DBusPendingCall *pending)
374 {
375 pending->refcount.value += 1;
376
377 return pending;
378 }
379
380
381 static void
_dbus_pending_call_last_unref(DBusPendingCall * pending)382 _dbus_pending_call_last_unref (DBusPendingCall *pending)
383 {
384 DBusConnection *connection;
385
386 /* If we get here, we should be already detached
387 * from the connection, or never attached.
388 */
389 _dbus_assert (!pending->timeout_added);
390
391 connection = pending->connection;
392
393 /* this assumes we aren't holding connection lock... */
394 _dbus_data_slot_list_free (&pending->slot_list);
395
396 if (pending->timeout != NULL)
397 _dbus_timeout_unref (pending->timeout);
398
399 if (pending->timeout_link)
400 {
401 dbus_message_unref ((DBusMessage *)pending->timeout_link->data);
402 _dbus_list_free_link (pending->timeout_link);
403 pending->timeout_link = NULL;
404 }
405
406 if (pending->reply)
407 {
408 dbus_message_unref (pending->reply);
409 pending->reply = NULL;
410 }
411
412 dbus_free (pending);
413
414 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
415
416 /* connection lock should not be held. */
417 /* Free the connection last to avoid a weird state while
418 * calling out to application code where the pending exists
419 * but not the connection.
420 */
421 dbus_connection_unref (connection);
422 }
423
424 /**
425 * Decrements the reference count on a pending call,
426 * freeing it if the count reaches 0. Assumes
427 * connection lock is already held.
428 *
429 * @param pending the pending call object
430 */
431 void
_dbus_pending_call_unref_and_unlock(DBusPendingCall * pending)432 _dbus_pending_call_unref_and_unlock (DBusPendingCall *pending)
433 {
434 dbus_bool_t last_unref;
435
436 _dbus_assert (pending->refcount.value > 0);
437
438 pending->refcount.value -= 1;
439 last_unref = pending->refcount.value == 0;
440
441 CONNECTION_UNLOCK (pending->connection);
442 if (last_unref)
443 _dbus_pending_call_last_unref (pending);
444 }
445
446 /**
447 * Checks whether the pending call has received a reply
448 * yet, or not. Assumes connection lock is held.
449 *
450 * @param pending the pending call
451 * @returns #TRUE if a reply has been received
452 */
453 dbus_bool_t
_dbus_pending_call_get_completed_unlocked(DBusPendingCall * pending)454 _dbus_pending_call_get_completed_unlocked (DBusPendingCall *pending)
455 {
456 return pending->completed;
457 }
458
459 static DBusDataSlotAllocator slot_allocator;
460 _DBUS_DEFINE_GLOBAL_LOCK (pending_call_slots);
461
462 /**
463 * Stores a pointer on a #DBusPendingCall, along
464 * with an optional function to be used for freeing
465 * the data when the data is set again, or when
466 * the pending call is finalized. The slot number
467 * must have been allocated with dbus_pending_call_allocate_data_slot().
468 *
469 * @param pending the pending_call
470 * @param slot the slot number
471 * @param data the data to store
472 * @param free_data_func finalizer function for the data
473 * @returns #TRUE if there was enough memory to store the data
474 */
475 dbus_bool_t
_dbus_pending_call_set_data_unlocked(DBusPendingCall * pending,dbus_int32_t slot,void * data,DBusFreeFunction free_data_func)476 _dbus_pending_call_set_data_unlocked (DBusPendingCall *pending,
477 dbus_int32_t slot,
478 void *data,
479 DBusFreeFunction free_data_func)
480 {
481 DBusFreeFunction old_free_func;
482 void *old_data;
483 dbus_bool_t retval;
484
485 retval = _dbus_data_slot_list_set (&slot_allocator,
486 &pending->slot_list,
487 slot, data, free_data_func,
488 &old_free_func, &old_data);
489
490 /* Drop locks to call out to app code */
491 CONNECTION_UNLOCK (pending->connection);
492
493 if (retval)
494 {
495 if (old_free_func)
496 (* old_free_func) (old_data);
497 }
498
499 CONNECTION_LOCK (pending->connection);
500
501 return retval;
502 }
503
504 /** @} */
505
506 /**
507 * @defgroup DBusPendingCall DBusPendingCall
508 * @ingroup DBus
509 * @brief Pending reply to a method call message
510 *
511 * A DBusPendingCall is an object representing an
512 * expected reply. A #DBusPendingCall can be created
513 * when you send a message that should have a reply.
514 *
515 * @{
516 */
517
518 /**
519 * @typedef DBusPendingCall
520 *
521 * Opaque data type representing a message pending.
522 */
523
524 /**
525 * Increments the reference count on a pending call.
526 *
527 * @param pending the pending call object
528 * @returns the pending call object
529 */
530 DBusPendingCall *
dbus_pending_call_ref(DBusPendingCall * pending)531 dbus_pending_call_ref (DBusPendingCall *pending)
532 {
533 _dbus_return_val_if_fail (pending != NULL, NULL);
534
535 /* The connection lock is better than the global
536 * lock in the atomic increment fallback
537 */
538 #ifdef DBUS_HAVE_ATOMIC_INT
539 _dbus_atomic_inc (&pending->refcount);
540 #else
541 CONNECTION_LOCK (pending->connection);
542 _dbus_assert (pending->refcount.value > 0);
543
544 pending->refcount.value += 1;
545 CONNECTION_UNLOCK (pending->connection);
546 #endif
547
548 return pending;
549 }
550
551 /**
552 * Decrements the reference count on a pending call,
553 * freeing it if the count reaches 0.
554 *
555 * @param pending the pending call object
556 */
557 void
dbus_pending_call_unref(DBusPendingCall * pending)558 dbus_pending_call_unref (DBusPendingCall *pending)
559 {
560 dbus_bool_t last_unref;
561
562 _dbus_return_if_fail (pending != NULL);
563
564 /* More efficient to use the connection lock instead of atomic
565 * int fallback if we lack atomic int decrement
566 */
567 #ifdef DBUS_HAVE_ATOMIC_INT
568 last_unref = (_dbus_atomic_dec (&pending->refcount) == 1);
569 #else
570 CONNECTION_LOCK (pending->connection);
571 _dbus_assert (pending->refcount.value > 0);
572 pending->refcount.value -= 1;
573 last_unref = pending->refcount.value == 0;
574 CONNECTION_UNLOCK (pending->connection);
575 #endif
576
577 if (last_unref)
578 _dbus_pending_call_last_unref(pending);
579 }
580
581 /**
582 * Sets a notification function to be called when the reply is
583 * received or the pending call times out.
584 *
585 * @param pending the pending call
586 * @param function notifier function
587 * @param user_data data to pass to notifier function
588 * @param free_user_data function to free the user data
589 * @returns #FALSE if not enough memory
590 */
591 dbus_bool_t
dbus_pending_call_set_notify(DBusPendingCall * pending,DBusPendingCallNotifyFunction function,void * user_data,DBusFreeFunction free_user_data)592 dbus_pending_call_set_notify (DBusPendingCall *pending,
593 DBusPendingCallNotifyFunction function,
594 void *user_data,
595 DBusFreeFunction free_user_data)
596 {
597 _dbus_return_val_if_fail (pending != NULL, FALSE);
598
599 CONNECTION_LOCK (pending->connection);
600
601 /* could invoke application code! */
602 if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
603 user_data, free_user_data))
604 return FALSE;
605
606 pending->function = function;
607
608 CONNECTION_UNLOCK (pending->connection);
609
610 return TRUE;
611 }
612
613 /**
614 * Cancels the pending call, such that any reply or error received
615 * will just be ignored. Drops the dbus library's internal reference
616 * to the #DBusPendingCall so will free the call if nobody else is
617 * holding a reference. However you usually get a reference from
618 * dbus_connection_send_with_reply() so probably your app owns a ref
619 * also.
620 *
621 * Note that canceling a pending call will <em>not</em> simulate a
622 * timed-out call; if a call times out, then a timeout error reply is
623 * received. If you cancel the call, no reply is received unless the
624 * the reply was already received before you canceled.
625 *
626 * @param pending the pending call
627 */
628 void
dbus_pending_call_cancel(DBusPendingCall * pending)629 dbus_pending_call_cancel (DBusPendingCall *pending)
630 {
631 _dbus_return_if_fail (pending != NULL);
632
633 _dbus_connection_remove_pending_call (pending->connection,
634 pending);
635 }
636
637 /**
638 * Checks whether the pending call has received a reply
639 * yet, or not.
640 *
641 * @param pending the pending call
642 * @returns #TRUE if a reply has been received
643 */
644 dbus_bool_t
dbus_pending_call_get_completed(DBusPendingCall * pending)645 dbus_pending_call_get_completed (DBusPendingCall *pending)
646 {
647 dbus_bool_t completed;
648
649 _dbus_return_val_if_fail (pending != NULL, FALSE);
650
651 CONNECTION_LOCK (pending->connection);
652 completed = pending->completed;
653 CONNECTION_UNLOCK (pending->connection);
654
655 return completed;
656 }
657
658 /**
659 * Gets the reply, or returns #NULL if none has been received
660 * yet. Ownership of the reply message passes to the caller. This
661 * function can only be called once per pending call, since the reply
662 * message is tranferred to the caller.
663 *
664 * @param pending the pending call
665 * @returns the reply message or #NULL.
666 */
667 DBusMessage*
dbus_pending_call_steal_reply(DBusPendingCall * pending)668 dbus_pending_call_steal_reply (DBusPendingCall *pending)
669 {
670 DBusMessage *message;
671
672 _dbus_return_val_if_fail (pending != NULL, NULL);
673 _dbus_return_val_if_fail (pending->completed, NULL);
674 _dbus_return_val_if_fail (pending->reply != NULL, NULL);
675
676 CONNECTION_LOCK (pending->connection);
677
678 message = pending->reply;
679 pending->reply = NULL;
680
681 CONNECTION_UNLOCK (pending->connection);
682
683 return message;
684 }
685
686 /**
687 * Block until the pending call is completed. The blocking is as with
688 * dbus_connection_send_with_reply_and_block(); it does not enter the
689 * main loop or process other messages, it simply waits for the reply
690 * in question.
691 *
692 * If the pending call is already completed, this function returns
693 * immediately.
694 *
695 * @todo when you start blocking, the timeout is reset, but it should
696 * really only use time remaining since the pending call was created.
697 * This requires storing timestamps instead of intervals in the timeout
698 *
699 * @param pending the pending call
700 */
701 void
dbus_pending_call_block(DBusPendingCall * pending)702 dbus_pending_call_block (DBusPendingCall *pending)
703 {
704 _dbus_return_if_fail (pending != NULL);
705
706 _dbus_connection_block_pending_call (pending);
707 }
708
709 /**
710 * Allocates an integer ID to be used for storing application-specific
711 * data on any DBusPendingCall. The allocated ID may then be used
712 * with dbus_pending_call_set_data() and dbus_pending_call_get_data().
713 * The passed-in slot must be initialized to -1, and is filled in
714 * with the slot ID. If the passed-in slot is not -1, it's assumed
715 * to be already allocated, and its refcount is incremented.
716 *
717 * The allocated slot is global, i.e. all DBusPendingCall objects will
718 * have a slot with the given integer ID reserved.
719 *
720 * @param slot_p address of a global variable storing the slot
721 * @returns #FALSE on failure (no memory)
722 */
723 dbus_bool_t
dbus_pending_call_allocate_data_slot(dbus_int32_t * slot_p)724 dbus_pending_call_allocate_data_slot (dbus_int32_t *slot_p)
725 {
726 _dbus_return_val_if_fail (slot_p != NULL, FALSE);
727
728 return _dbus_data_slot_allocator_alloc (&slot_allocator,
729 &_DBUS_LOCK_NAME (pending_call_slots),
730 slot_p);
731 }
732
733 /**
734 * Deallocates a global ID for #DBusPendingCall data slots.
735 * dbus_pending_call_get_data() and dbus_pending_call_set_data() may
736 * no longer be used with this slot. Existing data stored on existing
737 * DBusPendingCall objects will be freed when the #DBusPendingCall is
738 * finalized, but may not be retrieved (and may only be replaced if
739 * someone else reallocates the slot). When the refcount on the
740 * passed-in slot reaches 0, it is set to -1.
741 *
742 * @param slot_p address storing the slot to deallocate
743 */
744 void
dbus_pending_call_free_data_slot(dbus_int32_t * slot_p)745 dbus_pending_call_free_data_slot (dbus_int32_t *slot_p)
746 {
747 _dbus_return_if_fail (slot_p != NULL);
748 _dbus_return_if_fail (*slot_p >= 0);
749
750 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
751 }
752
753 /**
754 * Stores a pointer on a #DBusPendingCall, along
755 * with an optional function to be used for freeing
756 * the data when the data is set again, or when
757 * the pending call is finalized. The slot number
758 * must have been allocated with dbus_pending_call_allocate_data_slot().
759 *
760 * @param pending the pending_call
761 * @param slot the slot number
762 * @param data the data to store
763 * @param free_data_func finalizer function for the data
764 * @returns #TRUE if there was enough memory to store the data
765 */
766 dbus_bool_t
dbus_pending_call_set_data(DBusPendingCall * pending,dbus_int32_t slot,void * data,DBusFreeFunction free_data_func)767 dbus_pending_call_set_data (DBusPendingCall *pending,
768 dbus_int32_t slot,
769 void *data,
770 DBusFreeFunction free_data_func)
771 {
772 dbus_bool_t retval;
773
774 _dbus_return_val_if_fail (pending != NULL, FALSE);
775 _dbus_return_val_if_fail (slot >= 0, FALSE);
776
777
778 CONNECTION_LOCK (pending->connection);
779 retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
780 CONNECTION_UNLOCK (pending->connection);
781 return retval;
782 }
783
784 /**
785 * Retrieves data previously set with dbus_pending_call_set_data().
786 * The slot must still be allocated (must not have been freed).
787 *
788 * @param pending the pending_call
789 * @param slot the slot to get data from
790 * @returns the data, or #NULL if not found
791 */
792 void*
dbus_pending_call_get_data(DBusPendingCall * pending,dbus_int32_t slot)793 dbus_pending_call_get_data (DBusPendingCall *pending,
794 dbus_int32_t slot)
795 {
796 void *res;
797
798 _dbus_return_val_if_fail (pending != NULL, NULL);
799
800 CONNECTION_LOCK (pending->connection);
801 res = _dbus_data_slot_list_get (&slot_allocator,
802 &pending->slot_list,
803 slot);
804 CONNECTION_UNLOCK (pending->connection);
805
806 return res;
807 }
808
809 /** @} */
810
811 #ifdef DBUS_BUILD_TESTS
812
813 /**
814 * @ingroup DBusPendingCallInternals
815 * Unit test for DBusPendingCall.
816 *
817 * @returns #TRUE on success.
818 */
819 dbus_bool_t
_dbus_pending_call_test(const char * test_data_dir)820 _dbus_pending_call_test (const char *test_data_dir)
821 {
822
823 return TRUE;
824 }
825 #endif /* DBUS_BUILD_TESTS */
826