00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "dbus-internals.h"
00025 #include "dbus-connection-internal.h"
00026 #include "dbus-pending-call-internal.h"
00027 #include "dbus-pending-call.h"
00028 #include "dbus-list.h"
00029 #include "dbus-threads.h"
00030 #include "dbus-test.h"
00031 #include "dbus-connection.h"
00032
00052 #define CONNECTION_LOCK(connection) _dbus_connection_lock(connection)
00053
00056 #define CONNECTION_UNLOCK(connection) _dbus_connection_unlock(connection)
00057
00061 struct DBusPendingCall
00062 {
00063 DBusAtomic refcount;
00065 DBusDataSlotList slot_list;
00067 DBusPendingCallNotifyFunction function;
00069 DBusConnection *connection;
00070 DBusMessage *reply;
00071 DBusTimeout *timeout;
00073 DBusList *timeout_link;
00075 dbus_uint32_t reply_serial;
00077 unsigned int completed : 1;
00078 unsigned int timeout_added : 1;
00079 };
00080
00081 static dbus_int32_t notify_user_data_slot = -1;
00082
00091 DBusPendingCall*
00092 _dbus_pending_call_new_unlocked (DBusConnection *connection,
00093 int timeout_milliseconds,
00094 DBusTimeoutHandler timeout_handler)
00095 {
00096 DBusPendingCall *pending;
00097 DBusTimeout *timeout;
00098
00099 _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
00100
00101 if (timeout_milliseconds == -1)
00102 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
00103
00104
00105
00106
00107
00108
00109 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
00110 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
00111
00112 if (!dbus_pending_call_allocate_data_slot (¬ify_user_data_slot))
00113 return NULL;
00114
00115 pending = dbus_new0 (DBusPendingCall, 1);
00116
00117 if (pending == NULL)
00118 {
00119 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
00120 return NULL;
00121 }
00122
00123 timeout = _dbus_timeout_new (timeout_milliseconds,
00124 timeout_handler,
00125 pending, NULL);
00126
00127 if (timeout == NULL)
00128 {
00129 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
00130 dbus_free (pending);
00131 return NULL;
00132 }
00133
00134 pending->refcount.value = 1;
00135 pending->connection = connection;
00136 _dbus_connection_ref_unlocked (pending->connection);
00137
00138 pending->timeout = timeout;
00139
00140
00141 _dbus_data_slot_list_init (&pending->slot_list);
00142
00143 return pending;
00144 }
00145
00154 void
00155 _dbus_pending_call_set_reply_unlocked (DBusPendingCall *pending,
00156 DBusMessage *message)
00157 {
00158 if (message == NULL)
00159 {
00160 message = pending->timeout_link->data;
00161 _dbus_list_clear (&pending->timeout_link);
00162 }
00163 else
00164 dbus_message_ref (message);
00165
00166 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n",
00167 message,
00168 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ?
00169 "method return" :
00170 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ?
00171 "error" : "other type",
00172 pending->reply_serial);
00173
00174 _dbus_assert (pending->reply == NULL);
00175 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message));
00176 pending->reply = message;
00177 }
00178
00186 void
00187 _dbus_pending_call_complete (DBusPendingCall *pending)
00188 {
00189 _dbus_assert (!pending->completed);
00190
00191 pending->completed = TRUE;
00192
00193 if (pending->function)
00194 {
00195 void *user_data;
00196 user_data = dbus_pending_call_get_data (pending,
00197 notify_user_data_slot);
00198
00199 (* pending->function) (pending, user_data);
00200 }
00201 }
00202
00210 void
00211 _dbus_pending_call_queue_timeout_error_unlocked (DBusPendingCall *pending,
00212 DBusConnection *connection)
00213 {
00214 _dbus_assert (connection == pending->connection);
00215
00216 if (pending->timeout_link)
00217 {
00218 _dbus_connection_queue_synthesized_message_link (connection,
00219 pending->timeout_link);
00220 pending->timeout_link = NULL;
00221 }
00222 }
00223
00230 dbus_bool_t
00231 _dbus_pending_call_is_timeout_added_unlocked (DBusPendingCall *pending)
00232 {
00233 _dbus_assert (pending != NULL);
00234
00235 return pending->timeout_added;
00236 }
00237
00238
00245 void
00246 _dbus_pending_call_set_timeout_added_unlocked (DBusPendingCall *pending,
00247 dbus_bool_t is_added)
00248 {
00249 _dbus_assert (pending != NULL);
00250
00251 pending->timeout_added = is_added;
00252 }
00253
00254
00261 DBusTimeout *
00262 _dbus_pending_call_get_timeout_unlocked (DBusPendingCall *pending)
00263 {
00264 _dbus_assert (pending != NULL);
00265
00266 return pending->timeout;
00267 }
00268
00275 dbus_uint32_t
00276 _dbus_pending_call_get_reply_serial_unlocked (DBusPendingCall *pending)
00277 {
00278 _dbus_assert (pending != NULL);
00279
00280 return pending->reply_serial;
00281 }
00282
00289 void
00290 _dbus_pending_call_set_reply_serial_unlocked (DBusPendingCall *pending,
00291 dbus_uint32_t serial)
00292 {
00293 _dbus_assert (pending != NULL);
00294 _dbus_assert (pending->reply_serial == 0);
00295
00296 pending->reply_serial = serial;
00297 }
00298
00305 DBusConnection *
00306 _dbus_pending_call_get_connection_and_lock (DBusPendingCall *pending)
00307 {
00308 _dbus_assert (pending != NULL);
00309
00310 CONNECTION_LOCK (pending->connection);
00311 return pending->connection;
00312 }
00313
00320 DBusConnection *
00321 _dbus_pending_call_get_connection_unlocked (DBusPendingCall *pending)
00322 {
00323 _dbus_assert (pending != NULL);
00324
00325 return pending->connection;
00326 }
00327
00336 dbus_bool_t
00337 _dbus_pending_call_set_timeout_error_unlocked (DBusPendingCall *pending,
00338 DBusMessage *message,
00339 dbus_uint32_t serial)
00340 {
00341 DBusList *reply_link;
00342 DBusMessage *reply;
00343
00344 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY,
00345 "The reply timeout expired. Server is taking "
00346 "a long time to respond.");
00347 if (reply == NULL)
00348 return FALSE;
00349
00350 reply_link = _dbus_list_alloc_link (reply);
00351 if (reply_link == NULL)
00352 {
00353 dbus_message_unref (reply);
00354 return FALSE;
00355 }
00356
00357 pending->timeout_link = reply_link;
00358
00359 _dbus_pending_call_set_reply_serial_unlocked (pending, serial);
00360
00361 return TRUE;
00362 }
00363
00371 DBusPendingCall *
00372 _dbus_pending_call_ref_unlocked (DBusPendingCall *pending)
00373 {
00374 pending->refcount.value += 1;
00375
00376 return pending;
00377 }
00378
00379
00380 static void
00381 _dbus_pending_call_last_unref (DBusPendingCall *pending)
00382 {
00383 DBusConnection *connection;
00384
00385
00386
00387
00388 _dbus_assert (!pending->timeout_added);
00389
00390 connection = pending->connection;
00391
00392
00393 _dbus_data_slot_list_free (&pending->slot_list);
00394
00395 if (pending->timeout != NULL)
00396 _dbus_timeout_unref (pending->timeout);
00397
00398 if (pending->timeout_link)
00399 {
00400 dbus_message_unref ((DBusMessage *)pending->timeout_link->data);
00401 _dbus_list_free_link (pending->timeout_link);
00402 pending->timeout_link = NULL;
00403 }
00404
00405 if (pending->reply)
00406 {
00407 dbus_message_unref (pending->reply);
00408 pending->reply = NULL;
00409 }
00410
00411 dbus_free (pending);
00412
00413 dbus_pending_call_free_data_slot (¬ify_user_data_slot);
00414
00415
00416
00417
00418
00419
00420 dbus_connection_unref (connection);
00421 }
00422
00430 void
00431 _dbus_pending_call_unref_and_unlock (DBusPendingCall *pending)
00432 {
00433 dbus_bool_t last_unref;
00434
00435 _dbus_assert (pending->refcount.value > 0);
00436
00437 pending->refcount.value -= 1;
00438 last_unref = pending->refcount.value == 0;
00439
00440 CONNECTION_UNLOCK (pending->connection);
00441 if (last_unref)
00442 _dbus_pending_call_last_unref (pending);
00443 }
00444
00452 dbus_bool_t
00453 _dbus_pending_call_get_completed_unlocked (DBusPendingCall *pending)
00454 {
00455 return pending->completed;
00456 }
00457
00458 static DBusDataSlotAllocator slot_allocator;
00459 _DBUS_DEFINE_GLOBAL_LOCK (pending_call_slots);
00460
00474 dbus_bool_t
00475 _dbus_pending_call_set_data_unlocked (DBusPendingCall *pending,
00476 dbus_int32_t slot,
00477 void *data,
00478 DBusFreeFunction free_data_func)
00479 {
00480 DBusFreeFunction old_free_func;
00481 void *old_data;
00482 dbus_bool_t retval;
00483
00484 retval = _dbus_data_slot_list_set (&slot_allocator,
00485 &pending->slot_list,
00486 slot, data, free_data_func,
00487 &old_free_func, &old_data);
00488
00489
00490 CONNECTION_UNLOCK (pending->connection);
00491
00492 if (retval)
00493 {
00494 if (old_free_func)
00495 (* old_free_func) (old_data);
00496 }
00497
00498 CONNECTION_LOCK (pending->connection);
00499
00500 return retval;
00501 }
00502
00529 DBusPendingCall *
00530 dbus_pending_call_ref (DBusPendingCall *pending)
00531 {
00532 _dbus_return_val_if_fail (pending != NULL, NULL);
00533
00534
00535
00536
00537 #ifdef DBUS_HAVE_ATOMIC_INT
00538 _dbus_atomic_inc (&pending->refcount);
00539 #else
00540 CONNECTION_LOCK (pending->connection);
00541 _dbus_assert (pending->refcount.value > 0);
00542
00543 pending->refcount.value += 1;
00544 CONNECTION_UNLOCK (pending->connection);
00545 #endif
00546
00547 return pending;
00548 }
00549
00556 void
00557 dbus_pending_call_unref (DBusPendingCall *pending)
00558 {
00559 dbus_bool_t last_unref;
00560
00561 _dbus_return_if_fail (pending != NULL);
00562
00563
00564
00565
00566 #ifdef DBUS_HAVE_ATOMIC_INT
00567 last_unref = (_dbus_atomic_dec (&pending->refcount) == 1);
00568 #else
00569 CONNECTION_LOCK (pending->connection);
00570 _dbus_assert (pending->refcount.value > 0);
00571 pending->refcount.value -= 1;
00572 last_unref = pending->refcount.value == 0;
00573 CONNECTION_UNLOCK (pending->connection);
00574 #endif
00575
00576 if (last_unref)
00577 _dbus_pending_call_last_unref(pending);
00578 }
00579
00590 dbus_bool_t
00591 dbus_pending_call_set_notify (DBusPendingCall *pending,
00592 DBusPendingCallNotifyFunction function,
00593 void *user_data,
00594 DBusFreeFunction free_user_data)
00595 {
00596 _dbus_return_val_if_fail (pending != NULL, FALSE);
00597
00598 CONNECTION_LOCK (pending->connection);
00599
00600
00601 if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot,
00602 user_data, free_user_data))
00603 return FALSE;
00604
00605 pending->function = function;
00606
00607 CONNECTION_UNLOCK (pending->connection);
00608
00609 return TRUE;
00610 }
00611
00627 void
00628 dbus_pending_call_cancel (DBusPendingCall *pending)
00629 {
00630 _dbus_return_if_fail (pending != NULL);
00631
00632 _dbus_connection_remove_pending_call (pending->connection,
00633 pending);
00634 }
00635
00643 dbus_bool_t
00644 dbus_pending_call_get_completed (DBusPendingCall *pending)
00645 {
00646 dbus_bool_t completed;
00647
00648 _dbus_return_val_if_fail (pending != NULL, FALSE);
00649
00650 CONNECTION_LOCK (pending->connection);
00651 completed = pending->completed;
00652 CONNECTION_UNLOCK (pending->connection);
00653
00654 return completed;
00655 }
00656
00666 DBusMessage*
00667 dbus_pending_call_steal_reply (DBusPendingCall *pending)
00668 {
00669 DBusMessage *message;
00670
00671 _dbus_return_val_if_fail (pending != NULL, NULL);
00672 _dbus_return_val_if_fail (pending->completed, NULL);
00673 _dbus_return_val_if_fail (pending->reply != NULL, NULL);
00674
00675 CONNECTION_LOCK (pending->connection);
00676
00677 message = pending->reply;
00678 pending->reply = NULL;
00679
00680 CONNECTION_UNLOCK (pending->connection);
00681
00682 return message;
00683 }
00684
00700 void
00701 dbus_pending_call_block (DBusPendingCall *pending)
00702 {
00703 _dbus_return_if_fail (pending != NULL);
00704
00705 _dbus_connection_block_pending_call (pending);
00706 }
00707
00722 dbus_bool_t
00723 dbus_pending_call_allocate_data_slot (dbus_int32_t *slot_p)
00724 {
00725 _dbus_return_val_if_fail (slot_p != NULL, FALSE);
00726
00727 return _dbus_data_slot_allocator_alloc (&slot_allocator,
00728 &_DBUS_LOCK_NAME (pending_call_slots),
00729 slot_p);
00730 }
00731
00743 void
00744 dbus_pending_call_free_data_slot (dbus_int32_t *slot_p)
00745 {
00746 _dbus_return_if_fail (slot_p != NULL);
00747 _dbus_return_if_fail (*slot_p >= 0);
00748
00749 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
00750 }
00751
00765 dbus_bool_t
00766 dbus_pending_call_set_data (DBusPendingCall *pending,
00767 dbus_int32_t slot,
00768 void *data,
00769 DBusFreeFunction free_data_func)
00770 {
00771 dbus_bool_t retval;
00772
00773 _dbus_return_val_if_fail (pending != NULL, FALSE);
00774 _dbus_return_val_if_fail (slot >= 0, FALSE);
00775
00776
00777 CONNECTION_LOCK (pending->connection);
00778 retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func);
00779 CONNECTION_UNLOCK (pending->connection);
00780 return retval;
00781 }
00782
00791 void*
00792 dbus_pending_call_get_data (DBusPendingCall *pending,
00793 dbus_int32_t slot)
00794 {
00795 void *res;
00796
00797 _dbus_return_val_if_fail (pending != NULL, NULL);
00798
00799 CONNECTION_LOCK (pending->connection);
00800 res = _dbus_data_slot_list_get (&slot_allocator,
00801 &pending->slot_list,
00802 slot);
00803 CONNECTION_UNLOCK (pending->connection);
00804
00805 return res;
00806 }
00807
00808 int _dbus_pending_get_error (DBusPendingCall *pending)
00809 {
00810 return (pending)? _dbus_connection_get_error (pending->connection) : 0;
00811 }
00812
00815 #ifdef DBUS_BUILD_TESTS
00816
00823 dbus_bool_t
00824 _dbus_pending_call_test (const char *test_data_dir)
00825 {
00826
00827 return TRUE;
00828 }
00829 #endif