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