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