dbus-pending-call.c 23.8 KB
Newer Older
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 3 4 5
/* dbus-pending-call.c Object representing a call in progress.
 *
 * Copyright (C) 2002, 2003 Red Hat Inc.
 *
6
 * Licensed under the Academic Free License version 2.1
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * 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 31 32 33 34 35 36 37 38 39 40 41 42 43
#include "dbus-list.h"
#include "dbus-threads.h"
#include "dbus-test.h"

/**
 * @defgroup DBusPendingCallInternals DBusPendingCall implementation details
 * @ingroup DBusInternals
 * @brief DBusPendingCall private implementation details.
 *
 * The guts of DBusPendingCall and its methods.
 *
 * @{
 */

44 45 46 47 48
/**
 * @brief Internals of DBusPendingCall
 *
 * Opaque object representing a reply message that we're waiting for.
 */
49 50 51 52

/**
 * shorter and more visible way to write _dbus_connection_lock()
 */
53
#define CONNECTION_LOCK(connection)   _dbus_connection_lock(connection)
54 55 56
/**
 * shorter and more visible way to write _dbus_connection_unlock()
 */
57 58
#define CONNECTION_UNLOCK(connection) _dbus_connection_unlock(connection)

59 60 61
/**
 * Implementation details of #DBusPendingCall - all fields are private.
 */
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
struct DBusPendingCall
{
  DBusAtomic refcount;                            /**< reference count */

  DBusDataSlotList slot_list;                     /**< Data stored by allocated integer ID */
  
  DBusPendingCallNotifyFunction function;         /**< Notifier when reply arrives. */

  DBusConnection *connection;                     /**< Connections we're associated with */
  DBusMessage *reply;                             /**< Reply (after we've received it) */
  DBusTimeout *timeout;                           /**< Timeout */

  DBusList *timeout_link;                         /**< Preallocated timeout response */
  
  dbus_uint32_t reply_serial;                     /**< Expected serial of reply */

  unsigned int completed : 1;                     /**< TRUE if completed */
  unsigned int timeout_added : 1;                 /**< Have added the timeout */
};

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
#ifdef DBUS_ENABLE_VERBOSE_MODE
static void
_dbus_pending_call_trace_ref (DBusPendingCall *pending_call,
    int old_refcount,
    int new_refcount,
    const char *why)
{
  static int enabled = -1;

  _dbus_trace_ref ("DBusPendingCall", pending_call, old_refcount,
      new_refcount, why, "DBUS_PENDING_CALL_TRACE", &enabled);
}
#else
#define _dbus_pending_call_trace_ref(p, o, n, w) \
  do \
  {\
    (void) (o); \
    (void) (n); \
  } while (0)
#endif

103 104
static dbus_int32_t notify_user_data_slot = -1;

105 106 107 108
/**
 * Creates a new pending reply object.
 *
 * @param connection connection where reply will arrive
109 110 111
 * @param timeout_milliseconds length of timeout, -1 (or
 *  #DBUS_TIMEOUT_USE_DEFAULT) for default,
 *  #DBUS_TIMEOUT_INFINITE for no timeout
112
 * @param timeout_handler timeout handler, takes pending call as data
113 114 115
 * @returns a new #DBusPendingCall or #NULL if no memory.
 */
DBusPendingCall*
116 117 118
_dbus_pending_call_new_unlocked (DBusConnection    *connection,
                                 int                timeout_milliseconds,
                                 DBusTimeoutHandler timeout_handler)
119 120
{
  DBusPendingCall *pending;
121
  DBusTimeout *timeout;
122

123
  _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
124
 
125 126
  if (timeout_milliseconds == -1)
    timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
127 128 129

  if (!dbus_pending_call_allocate_data_slot (&notify_user_data_slot))
    return NULL;
130
  
131
  pending = dbus_new0 (DBusPendingCall, 1);
132 133
  
  if (pending == NULL)
134 135 136 137
    {
      dbus_pending_call_free_data_slot (&notify_user_data_slot);
      return NULL;
    }
138

139
  if (timeout_milliseconds != DBUS_TIMEOUT_INFINITE)
140
    {
141 142 143 144 145 146 147 148 149 150 151 152
      timeout = _dbus_timeout_new (timeout_milliseconds,
                                   timeout_handler,
                                   pending, NULL);  

      if (timeout == NULL)
        {
          dbus_pending_call_free_data_slot (&notify_user_data_slot);
          dbus_free (pending);
          return NULL;
        }

      pending->timeout = timeout;
153
    }
154 155 156 157
  else
    {
      pending->timeout = NULL;
    }
158 159

  _dbus_atomic_inc (&pending->refcount);
160
  pending->connection = connection;
161
  _dbus_connection_ref_unlocked (pending->connection);
162

163
  _dbus_data_slot_list_init (&pending->slot_list);
164 165 166

  _dbus_pending_call_trace_ref (pending, 0, 1, "new_unlocked");

167 168 169
  return pending;
}

170 171 172 173 174 175 176 177 178
/**
 * Sets the reply of a pending call with the given message,
 * or if the message is #NULL, by timing out the pending call.
 * 
 * @param pending the pending call
 * @param message the message to complete the call with, or #NULL
 *  to time out the call
 */
void
179 180
_dbus_pending_call_set_reply_unlocked (DBusPendingCall *pending,
                                       DBusMessage     *message)
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
{
  if (message == NULL)
    {
      message = pending->timeout_link->data;
      _dbus_list_clear (&pending->timeout_link);
    }
  else
    dbus_message_ref (message);

  _dbus_verbose ("  handing message %p (%s) to pending call serial %u\n",
                 message,
                 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
                 "method return" :
                 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
                 "error" : "other type",
                 pending->reply_serial);
  
  _dbus_assert (pending->reply == NULL);
  _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
  pending->reply = message;
}

203 204 205 206 207 208 209 210
/**
 * Calls notifier function for the pending call
 * and sets the call to completed.
 *
 * @param pending the pending call
 * 
 */
void
211
_dbus_pending_call_complete (DBusPendingCall *pending)
212
{
213 214
  _dbus_assert (!pending->completed);
  
215 216 217
  pending->completed = TRUE;

  if (pending->function)
218 219 220 221 222 223 224
    {
      void *user_data;
      user_data = dbus_pending_call_get_data (pending,
                                              notify_user_data_slot);
      
      (* pending->function) (pending, user_data);
    }
225 226
}

227 228 229 230 231 232 233
/**
 * If the pending call hasn't been timed out, add its timeout
 * error reply to the connection's incoming message queue.
 *
 * @param pending the pending call
 * @param connection the connection the call was sent to
 */
234
void
235 236
_dbus_pending_call_queue_timeout_error_unlocked (DBusPendingCall *pending, 
                                                 DBusConnection  *connection)
237
{
238 239
  _dbus_assert (connection == pending->connection);
  
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  if (pending->timeout_link)
    {
      _dbus_connection_queue_synthesized_message_link (connection,
						       pending->timeout_link);
      pending->timeout_link = NULL;
    }
}

/**
 * Checks to see if a timeout has been added
 *
 * @param pending the pending_call
 * @returns #TRUE if there is a timeout or #FALSE if not
 */
dbus_bool_t 
255
_dbus_pending_call_is_timeout_added_unlocked (DBusPendingCall  *pending)
256 257 258 259 260 261 262 263 264 265 266 267 268 269
{
  _dbus_assert (pending != NULL);

  return pending->timeout_added;
}


/**
 * Sets wether the timeout has been added
 *
 * @param pending the pending_call
 * @param is_added whether or not a timeout is added
 */
void
270 271
_dbus_pending_call_set_timeout_added_unlocked (DBusPendingCall  *pending,
                                               dbus_bool_t       is_added)
272 273 274 275 276 277 278 279 280 281 282
{
  _dbus_assert (pending != NULL);

  pending->timeout_added = is_added;
}


/**
 * Retrives the timeout
 *
 * @param pending the pending_call
283
 * @returns a timeout object or NULL if call has no timeout
284 285
 */
DBusTimeout *
286
_dbus_pending_call_get_timeout_unlocked (DBusPendingCall  *pending)
287 288 289 290 291 292 293 294 295 296 297 298 299
{
  _dbus_assert (pending != NULL);

  return pending->timeout;
}

/**
 * Gets the reply's serial number
 *
 * @param pending the pending_call
 * @returns a serial number for the reply or 0 
 */
dbus_uint32_t 
300
_dbus_pending_call_get_reply_serial_unlocked (DBusPendingCall  *pending)
301 302 303 304 305 306 307 308 309 310 311 312 313
{
  _dbus_assert (pending != NULL);

  return pending->reply_serial;
}

/**
 * Sets the reply's serial number
 *
 * @param pending the pending_call
 * @param serial the serial number 
 */
void
314 315
_dbus_pending_call_set_reply_serial_unlocked  (DBusPendingCall *pending,
                                               dbus_uint32_t serial)
316 317 318 319 320 321 322 323
{
  _dbus_assert (pending != NULL);
  _dbus_assert (pending->reply_serial == 0);

  pending->reply_serial = serial;
}

/**
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
 * Gets the connection associated with this pending call.
 *
 * @param pending the pending_call
 * @returns the connection associated with the pending call
 */
DBusConnection *
_dbus_pending_call_get_connection_and_lock (DBusPendingCall *pending)
{
  _dbus_assert (pending != NULL);
 
  CONNECTION_LOCK (pending->connection);
  return pending->connection;
}

/**
 * Gets the connection associated with this pending call.
340 341 342 343 344
 *
 * @param pending the pending_call
 * @returns the connection associated with the pending call
 */
DBusConnection *
345
_dbus_pending_call_get_connection_unlocked (DBusPendingCall *pending)
346 347 348
{
  _dbus_assert (pending != NULL);
 
349
  return pending->connection;
350 351 352 353 354 355 356 357 358 359 360
}

/**
 * Sets the reply message associated with the pending call to a timeout error
 *
 * @param pending the pending_call
 * @param message the message we are sending the error reply to 
 * @param serial serial number for the reply
 * @return #FALSE on OOM
 */
dbus_bool_t
361 362 363
_dbus_pending_call_set_timeout_error_unlocked (DBusPendingCall *pending,
                                               DBusMessage     *message,
                                               dbus_uint32_t    serial)
364 365 366 367 368
{ 
  DBusList *reply_link;
  DBusMessage *reply;

  reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
369 370 371 372 373
                                  "Did not receive a reply. Possible causes include: "
                                  "the remote application did not send a reply, "
                                  "the message bus security policy blocked the reply, "
                                  "the reply timeout expired, or "
                                  "the network connection was broken.");
374 375 376 377 378 379
  if (reply == NULL)
    return FALSE;

  reply_link = _dbus_list_alloc_link (reply);
  if (reply_link == NULL)
    {
380 381
      /* it's OK to unref this, nothing that could have attached a callback
       * has ever seen it */
382 383 384 385 386 387
      dbus_message_unref (reply);
      return FALSE;
    }

  pending->timeout_link = reply_link;

388
  _dbus_pending_call_set_reply_serial_unlocked (pending, serial);
389 390 391 392
  
  return TRUE;
}

393 394 395 396 397 398 399 400 401 402
/**
 * Increments the reference count on a pending call,
 * while the lock on its connection is already held.
 *
 * @param pending the pending call object
 * @returns the pending call object
 */
DBusPendingCall *
_dbus_pending_call_ref_unlocked (DBusPendingCall *pending)
{
403 404 405 406 407
  dbus_int32_t old_refcount;

  old_refcount = _dbus_atomic_inc (&pending->refcount);
  _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
      "ref_unlocked");
408

409 410 411
  return pending;
}

412

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
static void
_dbus_pending_call_last_unref (DBusPendingCall *pending)
{
  DBusConnection *connection;
  
  /* If we get here, we should be already detached
   * from the connection, or never attached.
   */
  _dbus_assert (!pending->timeout_added);  

  connection = pending->connection;

  /* this assumes we aren't holding connection lock... */
  _dbus_data_slot_list_free (&pending->slot_list);

  if (pending->timeout != NULL)
    _dbus_timeout_unref (pending->timeout);
      
  if (pending->timeout_link)
    {
      dbus_message_unref ((DBusMessage *)pending->timeout_link->data);
      _dbus_list_free_link (pending->timeout_link);
      pending->timeout_link = NULL;
    }

  if (pending->reply)
    {
      dbus_message_unref (pending->reply);
      pending->reply = NULL;
    }
      
  dbus_free (pending);

  dbus_pending_call_free_data_slot (&notify_user_data_slot);

  /* connection lock should not be held. */
  /* Free the connection last to avoid a weird state while
   * calling out to application code where the pending exists
   * but not the connection.
   */
  dbus_connection_unref (connection);
}

/**
 * Decrements the reference count on a pending call,
 * freeing it if the count reaches 0. Assumes
 * connection lock is already held.
 *
 * @param pending the pending call object
 */
void
_dbus_pending_call_unref_and_unlock (DBusPendingCall *pending)
{
466
  dbus_int32_t old_refcount;
467

468 469
  old_refcount = _dbus_atomic_dec (&pending->refcount);
  _dbus_assert (old_refcount > 0);
470 471
  _dbus_pending_call_trace_ref (pending, old_refcount,
      old_refcount - 1, "unref_and_unlock");
472 473

  CONNECTION_UNLOCK (pending->connection);
474 475

  if (old_refcount == 1)
476 477 478
    _dbus_pending_call_last_unref (pending);
}

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
/**
 * Checks whether the pending call has received a reply
 * yet, or not. Assumes connection lock is held.
 *
 * @param pending the pending call
 * @returns #TRUE if a reply has been received
 */
dbus_bool_t
_dbus_pending_call_get_completed_unlocked (DBusPendingCall    *pending)
{
  return pending->completed;
}

static DBusDataSlotAllocator slot_allocator;
_DBUS_DEFINE_GLOBAL_LOCK (pending_call_slots);

/**
 * Stores a pointer on a #DBusPendingCall, along
 * with an optional function to be used for freeing
 * the data when the data is set again, or when
 * the pending call is finalized. The slot number
 * must have been allocated with dbus_pending_call_allocate_data_slot().
 *
 * @param pending the pending_call
 * @param slot the slot number
 * @param data the data to store
 * @param free_data_func finalizer function for the data
 * @returns #TRUE if there was enough memory to store the data
 */
dbus_bool_t
_dbus_pending_call_set_data_unlocked (DBusPendingCall  *pending,
                                     dbus_int32_t      slot,
                                     void             *data,
                                     DBusFreeFunction  free_data_func)
{
  DBusFreeFunction old_free_func;
  void *old_data;
  dbus_bool_t retval;

  retval = _dbus_data_slot_list_set (&slot_allocator,
                                     &pending->slot_list,
                                     slot, data, free_data_func,
                                     &old_free_func, &old_data);

  /* Drop locks to call out to app code */
  CONNECTION_UNLOCK (pending->connection);
  
  if (retval)
    {
      if (old_free_func)
        (* old_free_func) (old_data);
    }

  CONNECTION_LOCK (pending->connection);
  
  return retval;
}

/** @} */

/**
 * @defgroup DBusPendingCall DBusPendingCall
 * @ingroup  DBus
 * @brief Pending reply to a method call message
 *
 * A DBusPendingCall is an object representing an
 * expected reply. A #DBusPendingCall can be created
 * when you send a message that should have a reply.
 *
 * @{
 */

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
/**
 * @def DBUS_TIMEOUT_INFINITE
 *
 * An integer constant representing an infinite timeout. This has the
 * numeric value 0x7fffffff (the largest 32-bit signed integer).
 *
 * For source compatibility with D-Bus versions earlier than 1.4.12, use
 * 0x7fffffff, or INT32_MAX (assuming your platform has it).
 */

/**
 * @def DBUS_TIMEOUT_USE_DEFAULT
 *
 * An integer constant representing a request to use the default timeout.
 * This has numeric value -1.
 *
 * For source compatibility with D-Bus versions earlier than 1.4.12, use a
 * literal -1.
 */

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
/**
 * @typedef DBusPendingCall
 *
 * Opaque data type representing a message pending.
 */

/**
 * Increments the reference count on a pending call.
 *
 * @param pending the pending call object
 * @returns the pending call object
 */
DBusPendingCall *
dbus_pending_call_ref (DBusPendingCall *pending)
{
586 587
  dbus_int32_t old_refcount;

588 589
  _dbus_return_val_if_fail (pending != NULL, NULL);

590 591 592
  old_refcount = _dbus_atomic_inc (&pending->refcount);
  _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount + 1,
      "ref");
593 594 595 596

  return pending;
}

597 598 599 600 601 602 603 604 605
/**
 * Decrements the reference count on a pending call,
 * freeing it if the count reaches 0.
 *
 * @param pending the pending call object
 */
void
dbus_pending_call_unref (DBusPendingCall *pending)
{
606
  dbus_int32_t old_refcount;
607 608 609

  _dbus_return_if_fail (pending != NULL);

610 611 612
  old_refcount = _dbus_atomic_dec (&pending->refcount);
  _dbus_pending_call_trace_ref (pending, old_refcount, old_refcount - 1,
      "unref");
613

614
  if (old_refcount == 1)
615
    _dbus_pending_call_last_unref(pending);
616 617 618 619 620 621 622 623 624 625
}

/**
 * Sets a notification function to be called when the reply is
 * received or the pending call times out.
 *
 * @param pending the pending call
 * @param function notifier function
 * @param user_data data to pass to notifier function
 * @param free_user_data function to free the user data
626
 * @returns #FALSE if not enough memory
627
 */
628
dbus_bool_t
629 630 631 632 633
dbus_pending_call_set_notify (DBusPendingCall              *pending,
                              DBusPendingCallNotifyFunction function,
                              void                         *user_data,
                              DBusFreeFunction              free_user_data)
{
634
  _dbus_return_val_if_fail (pending != NULL, FALSE);
635

636 637
  CONNECTION_LOCK (pending->connection);
  
638
  /* could invoke application code! */
639 640
  if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
                                             user_data, free_user_data))
641 642
    return FALSE;
  
643 644
  pending->function = function;

645 646
  CONNECTION_UNLOCK (pending->connection);
  
647
  return TRUE;
648 649
}

650
/**
651 652 653
 * Cancels the pending call, such that any reply or error received
 * will just be ignored.  Drops the dbus library's internal reference
 * to the #DBusPendingCall so will free the call if nobody else is
654 655 656 657 658 659 660 661
 * holding a reference. However you usually get a reference from
 * dbus_connection_send_with_reply() so probably your app owns a ref
 * also.
 *
 * Note that canceling a pending call will <em>not</em> simulate a
 * timed-out call; if a call times out, then a timeout error reply is
 * received. If you cancel the call, no reply is received unless the
 * the reply was already received before you canceled.
662 663 664 665 666 667
 * 
 * @param pending the pending call
 */
void
dbus_pending_call_cancel (DBusPendingCall *pending)
{
668 669
  _dbus_return_if_fail (pending != NULL);

670 671
  _dbus_connection_remove_pending_call (pending->connection,
                                        pending);
672
}
673

674 675 676
/**
 * Checks whether the pending call has received a reply
 * yet, or not.
677
 *
678
 * @param pending the pending call
679 680
 * @returns #TRUE if a reply has been received
 */
681 682
dbus_bool_t
dbus_pending_call_get_completed (DBusPendingCall *pending)
683
{
684 685
  dbus_bool_t completed;
  
686 687
  _dbus_return_val_if_fail (pending != NULL, FALSE);

688 689 690 691 692
  CONNECTION_LOCK (pending->connection);
  completed = pending->completed;
  CONNECTION_UNLOCK (pending->connection);

  return completed;
693 694
}

695
/**
696 697 698 699
 * Gets the reply, or returns #NULL if none has been received
 * yet. Ownership of the reply message passes to the caller. This
 * function can only be called once per pending call, since the reply
 * message is tranferred to the caller.
700
 * 
701 702 703 704
 * @param pending the pending call
 * @returns the reply message or #NULL.
 */
DBusMessage*
705
dbus_pending_call_steal_reply (DBusPendingCall *pending)
706
{
707 708
  DBusMessage *message;
  
709
  _dbus_return_val_if_fail (pending != NULL, NULL);
710 711
  _dbus_return_val_if_fail (pending->completed, NULL);
  _dbus_return_val_if_fail (pending->reply != NULL, NULL);
712 713

  CONNECTION_LOCK (pending->connection);
714 715 716 717
  
  message = pending->reply;
  pending->reply = NULL;

718
  CONNECTION_UNLOCK (pending->connection);
719 720

  _dbus_message_trace_ref (message, -1, -1, "dbus_pending_call_steal_reply");
721
  return message;
722 723
}

724 725 726 727 728 729
/**
 * Block until the pending call is completed.  The blocking is as with
 * dbus_connection_send_with_reply_and_block(); it does not enter the
 * main loop or process other messages, it simply waits for the reply
 * in question.
 *
730 731 732
 * If the pending call is already completed, this function returns
 * immediately.
 *
733
 * @todo when you start blocking, the timeout is reset, but it should
734
 * really only use time remaining since the pending call was created.
735
 * This requires storing timestamps instead of intervals in the timeout
736 737 738 739 740 741
 *
 * @param pending the pending call
 */
void
dbus_pending_call_block (DBusPendingCall *pending)
{
742 743
  _dbus_return_if_fail (pending != NULL);

744
  _dbus_connection_block_pending_call (pending);
745 746
}

747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
/**
 * Allocates an integer ID to be used for storing application-specific
 * data on any DBusPendingCall. The allocated ID may then be used
 * with dbus_pending_call_set_data() and dbus_pending_call_get_data().
 * The passed-in slot must be initialized to -1, and is filled in
 * with the slot ID. If the passed-in slot is not -1, it's assumed
 * to be already allocated, and its refcount is incremented.
 * 
 * The allocated slot is global, i.e. all DBusPendingCall objects will
 * have a slot with the given integer ID reserved.
 *
 * @param slot_p address of a global variable storing the slot
 * @returns #FALSE on failure (no memory)
 */
dbus_bool_t
dbus_pending_call_allocate_data_slot (dbus_int32_t *slot_p)
{
764 765
  _dbus_return_val_if_fail (slot_p != NULL, FALSE);

766
  return _dbus_data_slot_allocator_alloc (&slot_allocator,
767
                                          &_DBUS_LOCK_NAME (pending_call_slots),
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
                                          slot_p);
}

/**
 * Deallocates a global ID for #DBusPendingCall data slots.
 * dbus_pending_call_get_data() and dbus_pending_call_set_data() may
 * no longer be used with this slot.  Existing data stored on existing
 * DBusPendingCall objects will be freed when the #DBusPendingCall is
 * finalized, but may not be retrieved (and may only be replaced if
 * someone else reallocates the slot).  When the refcount on the
 * passed-in slot reaches 0, it is set to -1.
 *
 * @param slot_p address storing the slot to deallocate
 */
void
dbus_pending_call_free_data_slot (dbus_int32_t *slot_p)
{
785
  _dbus_return_if_fail (slot_p != NULL);
786
  _dbus_return_if_fail (*slot_p >= 0);
787

788 789 790
  _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
}

791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
/**
 * Stores a pointer on a #DBusPendingCall, along
 * with an optional function to be used for freeing
 * the data when the data is set again, or when
 * the pending call is finalized. The slot number
 * must have been allocated with dbus_pending_call_allocate_data_slot().
 *
 * @param pending the pending_call
 * @param slot the slot number
 * @param data the data to store
 * @param free_data_func finalizer function for the data
 * @returns #TRUE if there was enough memory to store the data
 */
dbus_bool_t
dbus_pending_call_set_data (DBusPendingCall  *pending,
                            dbus_int32_t      slot,
                            void             *data,
                            DBusFreeFunction  free_data_func)
{
  dbus_bool_t retval;
  
  _dbus_return_val_if_fail (pending != NULL, FALSE);
  _dbus_return_val_if_fail (slot >= 0, FALSE);

  
  CONNECTION_LOCK (pending->connection);
  retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
  CONNECTION_UNLOCK (pending->connection);
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
  return retval;
}

/**
 * Retrieves data previously set with dbus_pending_call_set_data().
 * The slot must still be allocated (must not have been freed).
 *
 * @param pending the pending_call
 * @param slot the slot to get data from
 * @returns the data, or #NULL if not found
 */
void*
dbus_pending_call_get_data (DBusPendingCall   *pending,
                            dbus_int32_t       slot)
{
  void *res;

  _dbus_return_val_if_fail (pending != NULL, NULL);

838
  CONNECTION_LOCK (pending->connection);
839 840 841
  res = _dbus_data_slot_list_get (&slot_allocator,
                                  &pending->slot_list,
                                  slot);
842
  CONNECTION_UNLOCK (pending->connection);
843 844 845 846

  return res;
}

847
/** @} */