2005-06-30 Colin Walters <walters@verbum.org>
[platform/upstream/dbus.git] / dbus / dbus-connection.c
index cac4fc8..10a2d0f 100644 (file)
@@ -38,6 +38,7 @@
 #include "dbus-string.h"
 #include "dbus-pending-call.h"
 #include "dbus-object-tree.h"
+#include "dbus-threads-internal.h"
 
 #ifdef DBUS_DISABLE_CHECKS
 #define TOOK_LOCK_CHECK(connection)
 
 #define CONNECTION_LOCK(connection)   do {                                      \
     if (TRACE_LOCKS) { _dbus_verbose ("  LOCK: %s\n", _DBUS_FUNCTION_NAME); }   \
-    dbus_mutex_lock ((connection)->mutex);                                      \
+    _dbus_mutex_lock ((connection)->mutex);                                      \
     TOOK_LOCK_CHECK (connection);                                               \
   } while (0)
 
 #define CONNECTION_UNLOCK(connection) do {                                              \
     if (TRACE_LOCKS) { _dbus_verbose ("  UNLOCK: %s\n", _DBUS_FUNCTION_NAME);  }        \
     RELEASING_LOCK_CHECK (connection);                                                  \
-    dbus_mutex_unlock ((connection)->mutex);                                            \
+    _dbus_mutex_unlock ((connection)->mutex);                                            \
   } while (0)
 
 #define DISPATCH_STATUS_NAME(s)                                            \
  *
  * When a connection is disconnected, you are guaranteed to get a
  * signal "Disconnected" from the interface
- * #DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL, path
- * #DBUS_PATH_ORG_FREEDESKTOP_LOCAL.
+ * #DBUS_INTERFACE_LOCAL, path
+ * #DBUS_PATH_LOCAL.
  *
  * You may not drop the last reference to a #DBusConnection
  * until that connection has been disconnected.
@@ -188,14 +189,17 @@ struct DBusConnection
 
   DBusMutex *mutex; /**< Lock on the entire DBusConnection */
 
-  DBusCondVar *dispatch_cond;    /**< Protects dispatch() */
-  DBusCondVar *io_path_cond;     /**< Protects transport io path */
+  DBusMutex *dispatch_mutex;     /**< Protects dispatch_acquired */
+  DBusCondVar *dispatch_cond;    /**< Notify when dispatch_acquired is available */
+  DBusMutex *io_path_mutex;      /**< Protects io_path_acquired */
+  DBusCondVar *io_path_cond;     /**< Notify when io_path_acquired is available */
   
   DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */
   DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */
 
-  DBusMessage *message_borrowed; /**< True if the first incoming message has been borrowed */
-  DBusCondVar *message_returned_cond; /**< Used with dbus_connection_borrow_message() */
+  DBusMessage *message_borrowed; /**< Filled in if the first incoming message has been borrowed;
+                                  *   dispatch_acquired will be set by the borrower
+                                  */
   
   int n_outgoing;              /**< Length of outgoing queue. */
   int n_incoming;              /**< Length of incoming queue. */
@@ -229,9 +233,13 @@ struct DBusConnection
                          *   for the global linked list mempool lock
                          */
   DBusObjectTree *objects; /**< Object path handlers registered with this connection */
+
+  char *server_guid; /**< GUID of server if we are in shared_connections, #NULL if server GUID is unknown or connection is private */
+
+  unsigned int shareable : 1; /**< #TRUE if connection can go in shared_connections once we know the GUID */
   
-  unsigned int dispatch_acquired : 1; /**< Someone has dispatch path */
-  unsigned int io_path_acquired : 1;  /**< Someone has transport io path */
+  unsigned int dispatch_acquired : 1; /**< Someone has dispatch path (can drain incoming queue) */
+  unsigned int io_path_acquired : 1;  /**< Someone has transport io path (can use the transport to read/write messages) */
   
   unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */
   
@@ -248,6 +256,8 @@ static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked      (DB
 static void               _dbus_connection_update_dispatch_status_and_unlock (DBusConnection     *connection,
                                                                               DBusDispatchStatus  new_status);
 static void               _dbus_connection_last_unref                        (DBusConnection     *connection);
+static void               _dbus_connection_acquire_dispatch                  (DBusConnection     *connection);
+static void               _dbus_connection_release_dispatch                  (DBusConnection     *connection);
 
 static DBusMessageFilter *
 _dbus_message_filter_ref (DBusMessageFilter *filter)
@@ -896,7 +906,7 @@ _dbus_pending_call_complete_and_unlock (DBusPendingCall *pending,
 /**
  * Acquire the transporter I/O path. This must be done before
  * doing any I/O in the transporter. May sleep and drop the
- * connection mutex while waiting for the I/O path.
+ * IO path mutex while waiting for the I/O path.
  *
  * @param connection the connection.
  * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
@@ -906,40 +916,63 @@ static dbus_bool_t
 _dbus_connection_acquire_io_path (DBusConnection *connection,
                                  int timeout_milliseconds)
 {
-  dbus_bool_t res = TRUE;
+  dbus_bool_t we_acquired;
+  
+  HAVE_LOCK_CHECK (connection);
+
+  /* We don't want the connection to vanish */
+  _dbus_connection_ref_unlocked (connection);
+
+  /* We will only touch io_path_acquired which is protected by our mutex */
+  CONNECTION_UNLOCK (connection);
+  
+  _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
+  _dbus_mutex_lock (connection->io_path_mutex);
 
   _dbus_verbose ("%s start connection->io_path_acquired = %d timeout = %d\n",
                  _DBUS_FUNCTION_NAME, connection->io_path_acquired, timeout_milliseconds);
+
+  we_acquired = FALSE;
   
   if (connection->io_path_acquired)
     {
-#ifndef DBUS_DISABLE_CHECKS
-      connection->have_connection_lock = FALSE;
-#endif
-      
-      if (timeout_milliseconds != -1) 
-       res = dbus_condvar_wait_timeout (connection->io_path_cond,
-                                        connection->mutex,
-                                        timeout_milliseconds);
+      if (timeout_milliseconds != -1)
+        {
+          _dbus_verbose ("%s waiting %d for IO path to be acquirable\n",
+                         _DBUS_FUNCTION_NAME, timeout_milliseconds);
+          _dbus_condvar_wait_timeout (connection->io_path_cond,
+                                      connection->io_path_mutex,
+                                      timeout_milliseconds);
+        }
       else
-       dbus_condvar_wait (connection->io_path_cond, connection->mutex);
-
-#ifndef DBUS_DISABLE_CHECKS
-      connection->have_connection_lock = TRUE;
-#endif
+        {
+          while (connection->io_path_acquired)
+            {
+              _dbus_verbose ("%s waiting for IO path to be acquirable\n", _DBUS_FUNCTION_NAME);
+              _dbus_condvar_wait (connection->io_path_cond, connection->io_path_mutex);
+            }
+        }
     }
   
-  if (res)
+  if (!connection->io_path_acquired)
     {
-      _dbus_assert (!connection->io_path_acquired);
-
+      we_acquired = TRUE;
       connection->io_path_acquired = TRUE;
     }
+  
+  _dbus_verbose ("%s end connection->io_path_acquired = %d we_acquired = %d\n",
+                 _DBUS_FUNCTION_NAME, connection->io_path_acquired, we_acquired);
 
-  _dbus_verbose ("%s end connection->io_path_acquired = %d res = %d\n",
-                 _DBUS_FUNCTION_NAME, connection->io_path_acquired, res);
+  _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
+  _dbus_mutex_unlock (connection->io_path_mutex);
+
+  CONNECTION_LOCK (connection);
   
-  return res;
+  HAVE_LOCK_CHECK (connection);
+
+  _dbus_connection_unref_unlocked (connection);
+  
+  return we_acquired;
 }
 
 /**
@@ -952,15 +985,22 @@ _dbus_connection_acquire_io_path (DBusConnection *connection,
 static void
 _dbus_connection_release_io_path (DBusConnection *connection)
 {
+  HAVE_LOCK_CHECK (connection);
+  
+  _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
+  _dbus_mutex_lock (connection->io_path_mutex);
+  
   _dbus_assert (connection->io_path_acquired);
 
   _dbus_verbose ("%s start connection->io_path_acquired = %d\n",
                  _DBUS_FUNCTION_NAME, connection->io_path_acquired);
   
   connection->io_path_acquired = FALSE;
-  dbus_condvar_wake_one (connection->io_path_cond);
-}
+  _dbus_condvar_wake_one (connection->io_path_cond);
 
+  _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
+  _dbus_mutex_unlock (connection->io_path_mutex);
+}
 
 /**
  * Queues incoming messages and sends outgoing messages for this
@@ -1005,11 +1045,15 @@ _dbus_connection_do_iteration_unlocked (DBusConnection *connection,
   if (_dbus_connection_acquire_io_path (connection,
                                        (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
     {
+      HAVE_LOCK_CHECK (connection);
+      
       _dbus_transport_do_iteration (connection->transport,
                                    flags, timeout_milliseconds);
       _dbus_connection_release_io_path (connection);
     }
 
+  HAVE_LOCK_CHECK (connection);
+
   _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
 }
 
@@ -1030,6 +1074,8 @@ _dbus_connection_new_for_transport (DBusTransport *transport)
   DBusTimeoutList *timeout_list;
   DBusHashTable *pending_replies;
   DBusMutex *mutex;
+  DBusMutex *io_path_mutex;
+  DBusMutex *dispatch_mutex;
   DBusCondVar *message_returned_cond;
   DBusCondVar *dispatch_cond;
   DBusCondVar *io_path_cond;
@@ -1043,6 +1089,8 @@ _dbus_connection_new_for_transport (DBusTransport *transport)
   pending_replies = NULL;
   timeout_list = NULL;
   mutex = NULL;
+  io_path_mutex = NULL;
+  dispatch_mutex = NULL;
   message_returned_cond = NULL;
   dispatch_cond = NULL;
   io_path_cond = NULL;
@@ -1070,24 +1118,32 @@ _dbus_connection_new_for_transport (DBusTransport *transport)
   if (connection == NULL)
     goto error;
 
-  mutex = dbus_mutex_new ();
+  mutex = _dbus_mutex_new ();
   if (mutex == NULL)
     goto error;
+
+  io_path_mutex = _dbus_mutex_new ();
+  if (io_path_mutex == NULL)
+    goto error;
+
+  dispatch_mutex = _dbus_mutex_new ();
+  if (dispatch_mutex == NULL)
+    goto error;
   
-  message_returned_cond = dbus_condvar_new ();
+  message_returned_cond = _dbus_condvar_new ();
   if (message_returned_cond == NULL)
     goto error;
   
-  dispatch_cond = dbus_condvar_new ();
+  dispatch_cond = _dbus_condvar_new ();
   if (dispatch_cond == NULL)
     goto error;
   
-  io_path_cond = dbus_condvar_new ();
+  io_path_cond = _dbus_condvar_new ();
   if (io_path_cond == NULL)
     goto error;
 
-  disconnect_message = dbus_message_new_signal (DBUS_PATH_ORG_FREEDESKTOP_LOCAL,
-                                                DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
+  disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL,
+                                                DBUS_INTERFACE_LOCAL,
                                                 "Disconnected");
   
   if (disconnect_message == NULL)
@@ -1111,8 +1167,9 @@ _dbus_connection_new_for_transport (DBusTransport *transport)
   connection->refcount.value = 1;
   connection->mutex = mutex;
   connection->dispatch_cond = dispatch_cond;
+  connection->dispatch_mutex = dispatch_mutex;
   connection->io_path_cond = io_path_cond;
-  connection->message_returned_cond = message_returned_cond;
+  connection->io_path_mutex = io_path_mutex;
   connection->transport = transport;
   connection->watches = watch_list;
   connection->timeouts = timeout_list;
@@ -1122,6 +1179,7 @@ _dbus_connection_new_for_transport (DBusTransport *transport)
   connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
   connection->objects = objects;
   connection->exit_on_disconnect = FALSE;
+  connection->shareable = FALSE;
 #ifndef DBUS_DISABLE_CHECKS
   connection->generation = _dbus_current_generation;
 #endif
@@ -1151,17 +1209,23 @@ _dbus_connection_new_for_transport (DBusTransport *transport)
     _dbus_list_free_link (disconnect_link);
   
   if (io_path_cond != NULL)
-    dbus_condvar_free (io_path_cond);
+    _dbus_condvar_free (io_path_cond);
   
   if (dispatch_cond != NULL)
-    dbus_condvar_free (dispatch_cond);
+    _dbus_condvar_free (dispatch_cond);
   
   if (message_returned_cond != NULL)
-    dbus_condvar_free (message_returned_cond);
+    _dbus_condvar_free (message_returned_cond);
   
   if (mutex != NULL)
-    dbus_mutex_free (mutex);
+    _dbus_mutex_free (mutex);
 
+  if (io_path_mutex != NULL)
+    _dbus_mutex_free (io_path_mutex);
+
+  if (dispatch_mutex != NULL)
+    _dbus_mutex_free (dispatch_mutex);
+  
   if (connection != NULL)
     dbus_free (connection);
 
@@ -1284,8 +1348,10 @@ _dbus_connection_handle_watch (DBusWatch                   *watch,
   
   CONNECTION_LOCK (connection);
   _dbus_connection_acquire_io_path (connection, -1);
+  HAVE_LOCK_CHECK (connection);
   retval = _dbus_transport_handle_watch (connection->transport,
                                          watch, condition);
+
   _dbus_connection_release_io_path (connection);
 
   HAVE_LOCK_CHECK (connection);
@@ -1302,6 +1368,301 @@ _dbus_connection_handle_watch (DBusWatch                   *watch,
   return retval;
 }
 
+_DBUS_DEFINE_GLOBAL_LOCK (shared_connections);
+static DBusHashTable *shared_connections = NULL;
+
+static void
+shared_connections_shutdown (void *data)
+{
+  _DBUS_LOCK (shared_connections);
+
+  _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) == 0);
+  _dbus_hash_table_unref (shared_connections);
+  shared_connections = NULL;
+  
+  _DBUS_UNLOCK (shared_connections);
+}
+
+static dbus_bool_t
+connection_lookup_shared (DBusAddressEntry  *entry,
+                          DBusConnection   **result)
+{
+  _dbus_verbose ("checking for existing connection\n");
+  
+  *result = NULL;
+  
+  _DBUS_LOCK (shared_connections);
+
+  if (shared_connections == NULL)
+    {
+      _dbus_verbose ("creating shared_connections hash table\n");
+      
+      shared_connections = _dbus_hash_table_new (DBUS_HASH_STRING,
+                                                 dbus_free,
+                                                 NULL);
+      if (shared_connections == NULL)
+        {
+          _DBUS_UNLOCK (shared_connections);
+          return FALSE;
+        }
+
+      if (!_dbus_register_shutdown_func (shared_connections_shutdown, NULL))
+        {
+          _dbus_hash_table_unref (shared_connections);
+          shared_connections = NULL;
+          _DBUS_UNLOCK (shared_connections);
+          return FALSE;
+        }
+
+      _dbus_verbose ("  successfully created shared_connections\n");
+      
+      _DBUS_UNLOCK (shared_connections);
+      return TRUE; /* no point looking up in the hash we just made */
+    }
+  else
+    {
+      const char *guid;
+
+      guid = dbus_address_entry_get_value (entry, "guid");
+      
+      if (guid != NULL)
+        {
+          *result = _dbus_hash_table_lookup_string (shared_connections,
+                                                    guid);
+
+          if (*result)
+            {
+              /* The DBusConnection can't have been disconnected
+               * between the lookup and this code, because the
+               * disconnection will take the shared_connections lock to
+               * remove the connection. It can't have been finalized
+               * since you have to disconnect prior to finalize.
+               *
+               * Thus it's safe to ref the connection.
+               */
+              dbus_connection_ref (*result);
+
+              _dbus_verbose ("looked up existing connection to server guid %s\n",
+                             guid);
+            }
+        }
+      
+      _DBUS_UNLOCK (shared_connections);
+      return TRUE;
+    }
+}
+
+static dbus_bool_t
+connection_record_shared_unlocked (DBusConnection *connection,
+                                   const char     *guid)
+{
+  char *guid_key;
+  char *guid_in_connection;
+
+  /* A separate copy of the key is required in the hash table, because
+   * we don't have a lock on the connection when we are doing a hash
+   * lookup.
+   */
+  
+  _dbus_assert (connection->server_guid == NULL);
+  _dbus_assert (connection->shareable);
+  
+  guid_key = _dbus_strdup (guid);
+  if (guid_key == NULL)
+    return FALSE;
+
+  guid_in_connection = _dbus_strdup (guid);
+  if (guid_in_connection == NULL)
+    {
+      dbus_free (guid_key);
+      return FALSE;
+    }
+  
+  _DBUS_LOCK (shared_connections);
+  _dbus_assert (shared_connections != NULL);
+  
+  if (!_dbus_hash_table_insert_string (shared_connections,
+                                       guid_key, connection))
+    {
+      dbus_free (guid_key);
+      dbus_free (guid_in_connection);
+      _DBUS_UNLOCK (shared_connections);
+      return FALSE;
+    }
+
+  connection->server_guid = guid_in_connection;
+
+  _dbus_verbose ("stored connection to %s to be shared\n",
+                 connection->server_guid);
+  
+  _DBUS_UNLOCK (shared_connections);
+
+  _dbus_assert (connection->server_guid != NULL);
+  
+  return TRUE;
+}
+
+static void
+connection_forget_shared_unlocked (DBusConnection *connection)
+{
+  HAVE_LOCK_CHECK (connection);
+  
+  if (connection->server_guid == NULL)
+    return;
+
+  _dbus_verbose ("dropping connection to %s out of the shared table\n",
+                 connection->server_guid);
+  
+  _DBUS_LOCK (shared_connections);
+
+  if (!_dbus_hash_table_remove_string (shared_connections,
+                                       connection->server_guid))
+    _dbus_assert_not_reached ("connection was not in the shared table");
+  
+  dbus_free (connection->server_guid);
+  connection->server_guid = NULL;
+
+  _DBUS_UNLOCK (shared_connections);
+}
+
+static DBusConnection*
+connection_try_from_address_entry (DBusAddressEntry *entry,
+                                   DBusError        *error)
+{
+  DBusTransport *transport;
+  DBusConnection *connection;
+
+  transport = _dbus_transport_open (entry, error);
+
+  if (transport == NULL)
+    {
+      _DBUS_ASSERT_ERROR_IS_SET (error);
+      return NULL;
+    }
+
+  connection = _dbus_connection_new_for_transport (transport);
+
+  _dbus_transport_unref (transport);
+  
+  if (connection == NULL)
+    {
+      _DBUS_SET_OOM (error);
+      return NULL;
+    }
+
+#ifndef DBUS_DISABLE_CHECKS
+  _dbus_assert (!connection->have_connection_lock);
+#endif
+  return connection;
+}
+
+/*
+ * If the shared parameter is true, then any existing connection will
+ * be used (and if a new connection is created, it will be available
+ * for use by others). If the shared parameter is false, a new
+ * connection will always be created, and the new connection will
+ * never be returned to other callers.
+ *
+ * @param address the address
+ * @param shared whether the connection is shared or private
+ * @param error error return
+ * @returns the connection or #NULL on error
+ */
+static DBusConnection*
+_dbus_connection_open_internal (const char     *address,
+                                dbus_bool_t     shared,
+                                DBusError      *error)
+{
+  DBusConnection *connection;
+  DBusAddressEntry **entries;
+  DBusError tmp_error;
+  DBusError first_error;
+  int len, i;
+
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+
+  _dbus_verbose ("opening %s connection to: %s\n",
+                 shared ? "shared" : "private", address);
+  
+  if (!dbus_parse_address (address, &entries, &len, error))
+    return NULL;
+
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+  
+  connection = NULL;
+
+  dbus_error_init (&tmp_error);
+  dbus_error_init (&first_error);
+  for (i = 0; i < len; i++)
+    {
+      if (shared)
+        {
+          if (!connection_lookup_shared (entries[i], &connection))
+            _DBUS_SET_OOM (&tmp_error);
+        }
+
+      if (connection == NULL)
+        {
+          connection = connection_try_from_address_entry (entries[i],
+                                                          &tmp_error);
+          
+          if (connection != NULL && shared)
+            {
+              const char *guid;
+
+              connection->shareable = TRUE;
+              
+              guid = dbus_address_entry_get_value (entries[i], "guid");
+
+              /* we don't have a connection lock but we know nobody
+               * else has a handle to the connection
+               */
+              
+              if (guid &&
+                  !connection_record_shared_unlocked (connection, guid))
+                {
+                  _DBUS_SET_OOM (&tmp_error);
+                  dbus_connection_close (connection);
+                  dbus_connection_unref (connection);
+                  connection = NULL;
+                }
+
+              /* but as of now the connection is possibly shared
+               * since another thread could have pulled it from the table
+               */
+            }
+        }
+      
+      if (connection)
+       break;
+
+      _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
+      
+      if (i == 0)
+        dbus_move_error (&tmp_error, &first_error);
+      else
+        dbus_error_free (&tmp_error);
+    }
+
+  /* NOTE we don't have a lock on a possibly-shared connection object */
+  
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+  _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
+  
+  if (connection == NULL)
+    {
+      _DBUS_ASSERT_ERROR_IS_SET (&first_error);
+      dbus_move_error (&first_error, error);
+    }
+  else
+    {
+      dbus_error_free (&first_error);
+    }
+  
+  dbus_address_entries_free (entries);
+  return connection;
+}
+
 /** @} */
 
 /**
@@ -1311,16 +1672,19 @@ _dbus_connection_handle_watch (DBusWatch                   *watch,
  */
 
 /**
- * Opens a new connection to a remote address.
+ * Gets a connection to a remote address. If a connection to the given
+ * address already exists, returns the existing connection with its
+ * reference count incremented.  Otherwise, returns a new connection
+ * and saves the new connection for possible re-use if a future call
+ * to dbus_connection_open() asks to connect to the same server.
  *
- * @todo specify what the address parameter is. Right now
- * it's just the name of a UNIX domain socket. It should be
- * something more complex that encodes which transport to use.
+ * Use dbus_connection_open_private() to get a dedicated connection
+ * not shared with other callers of dbus_connection_open().
  *
- * If the open fails, the function returns #NULL, and provides
- * a reason for the failure in the result parameter. Pass
- * #NULL for the result parameter if you aren't interested
- * in the reason for failure.
+ * If the open fails, the function returns #NULL, and provides a
+ * reason for the failure in the error parameter. Pass #NULL for the
+ * error parameter if you aren't interested in the reason for
+ * failure.
  * 
  * @param address the address.
  * @param error address where an error can be returned.
@@ -1331,31 +1695,44 @@ dbus_connection_open (const char     *address,
                       DBusError      *error)
 {
   DBusConnection *connection;
-  DBusTransport *transport;
 
   _dbus_return_val_if_fail (address != NULL, NULL);
   _dbus_return_val_if_error_is_set (error, NULL);
-  
-  transport = _dbus_transport_open (address, error);
-  if (transport == NULL)
-    {
-      _DBUS_ASSERT_ERROR_IS_SET (error);
-      return NULL;
-    }
-  
-  connection = _dbus_connection_new_for_transport (transport);
 
-  _dbus_transport_unref (transport);
-  
-  if (connection == NULL)
-    {
-      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
-      return NULL;
-    }
+  connection = _dbus_connection_open_internal (address,
+                                               TRUE,
+                                               error);
+
+  return connection;
+}
+
+/**
+ * Opens a new, dedicated connection to a remote address. Unlike
+ * dbus_connection_open(), always creates a new connection.
+ * This connection will not be saved or recycled by libdbus.
+ *
+ * If the open fails, the function returns #NULL, and provides a
+ * reason for the failure in the error parameter. Pass #NULL for the
+ * error parameter if you aren't interested in the reason for
+ * failure.
+ * 
+ * @param address the address.
+ * @param error address where an error can be returned.
+ * @returns new connection, or #NULL on failure.
+ */
+DBusConnection*
+dbus_connection_open_private (const char     *address,
+                              DBusError      *error)
+{
+  DBusConnection *connection;
+
+  _dbus_return_val_if_fail (address != NULL, NULL);
+  _dbus_return_val_if_error_is_set (error, NULL);
+
+  connection = _dbus_connection_open_internal (address,
+                                               FALSE,
+                                               error);
 
-#ifndef DBUS_DISABLE_CHECKS
-  _dbus_assert (!connection->have_connection_lock);
-#endif
   return connection;
 }
 
@@ -1418,7 +1795,8 @@ _dbus_connection_last_unref (DBusConnection *connection)
    * you won't get the disconnected message.
    */
   _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
-
+  _dbus_assert (connection->server_guid == NULL);
+  
   /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
   _dbus_object_tree_free_all_unlocked (connection->objects);
   
@@ -1468,7 +1846,7 @@ _dbus_connection_last_unref (DBusConnection *connection)
   _dbus_list_clear (&connection->incoming_messages);
 
   _dbus_counter_unref (connection->outgoing_counter);
-  
+
   _dbus_transport_unref (connection->transport);
 
   if (connection->disconnect_message_link)
@@ -1480,11 +1858,13 @@ _dbus_connection_last_unref (DBusConnection *connection)
 
   _dbus_list_clear (&connection->link_cache);
   
-  dbus_condvar_free (connection->dispatch_cond);
-  dbus_condvar_free (connection->io_path_cond);
-  dbus_condvar_free (connection->message_returned_cond);  
-  
-  dbus_mutex_free (connection->mutex);
+  _dbus_condvar_free (connection->dispatch_cond);
+  _dbus_condvar_free (connection->io_path_cond);
+
+  _dbus_mutex_free (connection->io_path_mutex);
+  _dbus_mutex_free (connection->dispatch_mutex);
+
+  _dbus_mutex_free (connection->mutex);
   
   dbus_free (connection);
 }
@@ -1538,7 +1918,7 @@ dbus_connection_unref (DBusConnection *connection)
  * Any further attempts to send data will result in errors.  This
  * function does not affect the connection's reference count.  It's
  * safe to disconnect a connection more than once; all calls after the
- * first do nothing. It's impossible to "reconnect" a connection, a
+ * first do nothing. It's impossible to "reopen" a connection, a
  * new connection must be created. This function may result in a call
  * to the DBusDispatchStatusFunction set with
  * dbus_connection_set_dispatch_status_function(), as the disconnect
@@ -1547,7 +1927,7 @@ dbus_connection_unref (DBusConnection *connection)
  * @param connection the connection.
  */
 void
-dbus_connection_disconnect (DBusConnection *connection)
+dbus_connection_close (DBusConnection *connection)
 {
   DBusDispatchStatus status;
   
@@ -1557,6 +1937,7 @@ dbus_connection_disconnect (DBusConnection *connection)
   _dbus_verbose ("Disconnecting %p\n", connection);
   
   CONNECTION_LOCK (connection);
+  
   _dbus_transport_disconnect (connection->transport);
 
   _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
@@ -1566,6 +1947,18 @@ dbus_connection_disconnect (DBusConnection *connection)
   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
 }
 
+/** Alias for dbus_connection_close(). This method is DEPRECATED and will be
+ *  removed for 1.0. Change your code to use dbus_connection_close() instead.
+ *
+ * @param connection the connection.
+ * @deprecated
+ */
+void
+dbus_connection_disconnect (DBusConnection *connection)
+{
+  dbus_connection_close (connection);
+}
+
 static dbus_bool_t
 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
 {
@@ -1578,7 +1971,7 @@ _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
  * connections are connected when they are opened.  A connection may
  * become disconnected when the remote application closes its end, or
  * exits; a connection may also be disconnected with
- * dbus_connection_disconnect().
+ * dbus_connection_close().
  *
  * @param connection the connection.
  * @returns #TRUE if the connection is still alive.
@@ -2078,7 +2471,7 @@ dbus_connection_send_with_reply (DBusConnection     *connection,
   if (!_dbus_connection_attach_pending_call_unlocked (connection,
                                                      pending))
     goto error;
-  
   if (!_dbus_connection_send_unlocked_no_update (connection, message, NULL))
     {
       _dbus_connection_detach_pending_call_and_unlock (connection,
@@ -2109,6 +2502,9 @@ dbus_connection_send_with_reply (DBusConnection     *connection,
   return FALSE;
 }
 
+/* This is slightly strange since we can pop a message here without
+ * the dispatch lock.
+ */
 static DBusMessage*
 check_for_reply_unlocked (DBusConnection *connection,
                           dbus_uint32_t   client_serial)
@@ -2136,46 +2532,71 @@ check_for_reply_unlocked (DBusConnection *connection,
 }
 
 /**
- * Blocks a certain time period while waiting for a reply.
- * If no reply arrives, returns #NULL.
+ * When a function that blocks has been called with a timeout, and we
+ * run out of memory, the time to wait for memory is based on the
+ * timeout. If the caller was willing to block a long time we wait a
+ * relatively long time for memory, if they were only willing to block
+ * briefly then we retry for memory at a rapid rate.
  *
- * @todo could use performance improvements (it keeps scanning
- * the whole message queue for example) and has thread issues,
- * see comments in source
+ * @timeout_milliseconds the timeout requested for blocking
+ */
+static void
+_dbus_memory_pause_based_on_timeout (int timeout_milliseconds)
+{
+  if (timeout_milliseconds == -1)
+    _dbus_sleep_milliseconds (1000);
+  else if (timeout_milliseconds < 100)
+    ; /* just busy loop */
+  else if (timeout_milliseconds <= 1000)
+    _dbus_sleep_milliseconds (timeout_milliseconds / 3);
+  else
+    _dbus_sleep_milliseconds (1000);
+}
+
+/**
+ * Blocks until a pending call times out or gets a reply.
  *
  * Does not re-enter the main loop or run filter/path-registered
  * callbacks. The reply to the message will not be seen by
  * filter callbacks.
  *
- * @param connection the connection
- * @param client_serial the reply serial to wait for
- * @param timeout_milliseconds timeout in milliseconds or -1 for default
- * @returns the message that is the reply or #NULL if no reply
+ * Returns immediately if pending call already got a reply.
+ * 
+ * @todo could use performance improvements (it keeps scanning
+ * the whole message queue for example)
+ *
+ * @param pending the pending call we block for a reply on
  */
-DBusMessage*
-_dbus_connection_block_for_reply (DBusConnection     *connection,
-                                  dbus_uint32_t       client_serial,
-                                  int                 timeout_milliseconds)
+void
+_dbus_connection_block_pending_call (DBusPendingCall *pending)
 {
   long start_tv_sec, start_tv_usec;
   long end_tv_sec, end_tv_usec;
   long tv_sec, tv_usec;
   DBusDispatchStatus status;
+  DBusConnection *connection;
+  dbus_uint32_t client_serial;
+  int timeout_milliseconds;
 
-  _dbus_assert (connection != NULL);
-  _dbus_assert (client_serial != 0);
-  _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1);
+  _dbus_assert (pending != NULL);
+
+  if (dbus_pending_call_get_completed (pending))
+    return;
+
+  if (pending->connection == NULL)
+    return; /* call already detached */
+
+  dbus_pending_call_ref (pending); /* necessary because the call could be canceled */
   
-  if (timeout_milliseconds == -1)
-    timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE;
+  connection = pending->connection;
+  client_serial = pending->reply_serial;
 
-  /* it would probably seem logical to pass in _DBUS_INT_MAX
-   * for infinite timeout, but then math below would get
-   * all overflow-prone, so smack that down.
+  /* note that timeout_milliseconds is limited to a smallish value
+   * in _dbus_pending_call_new() so overflows aren't possible
+   * below
    */
-  if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
-    timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
-  
+  timeout_milliseconds = dbus_timeout_get_interval (pending->timeout);
+
   /* Flush message queue */
   dbus_connection_flush (connection);
 
@@ -2192,11 +2613,8 @@ _dbus_connection_block_for_reply (DBusConnection     *connection,
                  client_serial,
                  start_tv_sec, start_tv_usec,
                  end_tv_sec, end_tv_usec);
-  
+
   /* Now we wait... */
-  /* THREAD TODO: This is busted. What if a dispatch() or pop_message
-   * gets the message before we do?
-   */
   /* always block at least once as we know we don't have the reply yet */
   _dbus_connection_do_iteration_unlocked (connection,
                                           DBUS_ITERATION_DO_READING |
@@ -2213,6 +2631,17 @@ _dbus_connection_block_for_reply (DBusConnection     *connection,
 
   status = _dbus_connection_get_dispatch_status_unlocked (connection);
 
+  /* the get_completed() is in case a dispatch() while we were blocking
+   * got the reply instead of us.
+   */
+  if (dbus_pending_call_get_completed (pending))
+    {
+      _dbus_verbose ("Pending call completed by dispatch in %s\n", _DBUS_FUNCTION_NAME);
+      _dbus_connection_update_dispatch_status_and_unlock (connection, status);
+      dbus_pending_call_unref (pending);
+      return;
+    }
+  
   if (status == DBUS_DISPATCH_DATA_REMAINS)
     {
       DBusMessage *reply;
@@ -2221,14 +2650,18 @@ _dbus_connection_block_for_reply (DBusConnection     *connection,
       if (reply != NULL)
         {
           _dbus_verbose ("%s checked for reply\n", _DBUS_FUNCTION_NAME);
-          status = _dbus_connection_get_dispatch_status_unlocked (connection);
 
           _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
+          
+          _dbus_pending_call_complete_and_unlock (pending, reply);
+          dbus_message_unref (reply);
 
-          /* Unlocks, and calls out to user code */
+          CONNECTION_LOCK (connection);
+          status = _dbus_connection_get_dispatch_status_unlocked (connection);
           _dbus_connection_update_dispatch_status_and_unlock (connection, status);
+          dbus_pending_call_unref (pending);
           
-          return reply;
+          return;
         }
     }
   
@@ -2236,8 +2669,14 @@ _dbus_connection_block_for_reply (DBusConnection     *connection,
   
   if (!_dbus_connection_get_is_connected_unlocked (connection))
     {
-      CONNECTION_UNLOCK (connection);
-      return NULL;
+      /* FIXME send a "DBUS_ERROR_DISCONNECTED" instead, just to help
+       * programmers understand what went wrong since the timeout is
+       * confusing
+       */
+      
+      _dbus_pending_call_complete_and_unlock (pending, NULL);
+      dbus_pending_call_unref (pending);
+      return;
     }
   else if (tv_sec < start_tv_sec)
     _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
@@ -2258,13 +2697,8 @@ _dbus_connection_block_for_reply (DBusConnection     *connection,
            * it.
            */
           _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
-          
-          if (timeout_milliseconds < 100)
-            ; /* just busy loop */
-          else if (timeout_milliseconds <= 1000)
-            _dbus_sleep_milliseconds (timeout_milliseconds / 3);
-          else
-            _dbus_sleep_milliseconds (1000);
+
+          _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
         }
       else
         {          
@@ -2281,10 +2715,16 @@ _dbus_connection_block_for_reply (DBusConnection     *connection,
   _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
                  (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
 
-  /* unlocks and calls out to user code */
-  _dbus_connection_update_dispatch_status_and_unlock (connection, status);
+  _dbus_assert (!dbus_pending_call_get_completed (pending));
+  
+  /* unlock and call user code */
+  _dbus_pending_call_complete_and_unlock (pending, NULL);
 
-  return NULL;
+  /* update user code on dispatch status */
+  CONNECTION_LOCK (connection);
+  status = _dbus_connection_get_dispatch_status_unlocked (connection);
+  _dbus_connection_update_dispatch_status_and_unlock (connection, status);
+  dbus_pending_call_unref (pending);
 }
 
 /**
@@ -2309,40 +2749,40 @@ _dbus_connection_block_for_reply (DBusConnection     *connection,
  * @returns the message that is the reply or #NULL with an error code if the
  * function fails.
  */
-DBusMessage *
+DBusMessage*
 dbus_connection_send_with_reply_and_block (DBusConnection     *connection,
                                            DBusMessage        *message,
                                            int                 timeout_milliseconds,
                                            DBusError          *error)
 {
-  dbus_uint32_t client_serial;
   DBusMessage *reply;
+  DBusPendingCall *pending;
   
   _dbus_return_val_if_fail (connection != NULL, NULL);
   _dbus_return_val_if_fail (message != NULL, NULL);
   _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);  
   _dbus_return_val_if_error_is_set (error, NULL);
   
-  if (!dbus_connection_send (connection, message, &client_serial))
+  if (!dbus_connection_send_with_reply (connection, message,
+                                        &pending, timeout_milliseconds))
     {
       _DBUS_SET_OOM (error);
       return NULL;
     }
 
-  reply = _dbus_connection_block_for_reply (connection,
-                                            client_serial,
-                                            timeout_milliseconds);
+  _dbus_assert (pending != NULL);
   
-  if (reply == NULL)
-    {
-      if (dbus_connection_get_is_connected (connection))
-        dbus_set_error (error, DBUS_ERROR_NO_REPLY, "Message did not receive a reply");
-      else
-        dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Disconnected prior to receiving a reply");
+  dbus_pending_call_block (pending);
 
-      return NULL;
-    }
-  else if (dbus_set_error_from_message (error, reply))
+  reply = dbus_pending_call_steal_reply (pending);
+  dbus_pending_call_unref (pending);
+
+  /* call_complete_and_unlock() called from pending_call_block() should
+   * always fill this in.
+   */
+  _dbus_assert (reply != NULL);
+  
+   if (dbus_set_error_from_message (error, reply))
     {
       dbus_message_unref (reply);
       return NULL;
@@ -2392,24 +2832,79 @@ dbus_connection_flush (DBusConnection *connection)
   _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
 }
 
-/* Call with mutex held. Will drop it while waiting and re-acquire
- * before returning
+/**
+ * This function is intended for use with applications that don't want
+ * to write a main loop and deal with #DBusWatch and #DBusTimeout. An
+ * example usage would be:
+ * 
+ * @code
+ *   while (dbus_connection_read_write_dispatch (connection, -1))
+ *     ; // empty loop body
+ * @endcode
+ * 
+ * In this usage you would normally have set up a filter function to look
+ * at each message as it is dispatched. The loop terminates when the last
+ * message from the connection (the disconnected signal) is processed.
+ * 
+ * If there are messages to dispatch, this function will
+ * dbus_connection_dispatch() once, and return. If there are no
+ * messages to dispatch, this function will block until it can read or
+ * write, then read or write, then return.
+ *
+ * The way to think of this function is that it either makes some sort
+ * of progress, or it blocks.
+ *
+ * The return value indicates whether the disconnect message has been
+ * processed, NOT whether the connection is connected. This is
+ * important because even after disconnecting, you want to process any
+ * messages you received prior to the disconnect.
+ *
+ * @param connection the connection
+ * @param timeout_milliseconds max time to block or -1 for infinite
+ * @returns #TRUE if the disconnect message has not been processed
  */
-static void
-_dbus_connection_wait_for_borrowed (DBusConnection *connection)
+dbus_bool_t
+dbus_connection_read_write_dispatch (DBusConnection *connection,
+                                     int             timeout_milliseconds)
 {
-  _dbus_assert (connection->message_borrowed != NULL);
+  DBusDispatchStatus dstatus;
+  dbus_bool_t dispatched_disconnected;
+  
+  _dbus_return_val_if_fail (connection != NULL, FALSE);
+  _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
+  dstatus = dbus_connection_get_dispatch_status (connection);
 
-  while (connection->message_borrowed != NULL)
+  if (dstatus == DBUS_DISPATCH_DATA_REMAINS)
     {
-#ifndef DBUS_DISABLE_CHECKS
-      connection->have_connection_lock = FALSE;
-#endif
-      dbus_condvar_wait (connection->message_returned_cond, connection->mutex);
-#ifndef DBUS_DISABLE_CHECKS
-      connection->have_connection_lock = TRUE;
-#endif
+      _dbus_verbose ("doing dispatch in %s\n", _DBUS_FUNCTION_NAME);
+      dbus_connection_dispatch (connection);
+      CONNECTION_LOCK (connection);
+    }
+  else if (dstatus == DBUS_DISPATCH_NEED_MEMORY)
+    {
+      _dbus_verbose ("pausing for memory in %s\n", _DBUS_FUNCTION_NAME);
+      _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
+      CONNECTION_LOCK (connection);
+    }
+  else
+    {
+      CONNECTION_LOCK (connection);
+      if (_dbus_connection_get_is_connected_unlocked (connection))
+        {
+          _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
+          _dbus_connection_do_iteration_unlocked (connection,
+                                                  DBUS_ITERATION_DO_READING |
+                                                  DBUS_ITERATION_DO_WRITING |
+                                                  DBUS_ITERATION_BLOCK,
+                                                  timeout_milliseconds);
+        }
     }
+  
+  HAVE_LOCK_CHECK (connection);
+  dispatched_disconnected = connection->n_incoming == 0 &&
+    connection->disconnect_message_link == NULL;
+  CONNECTION_UNLOCK (connection);
+  return !dispatched_disconnected; /* TRUE if we have not processed disconnected */
 }
 
 /**
@@ -2423,18 +2918,21 @@ _dbus_connection_wait_for_borrowed (DBusConnection *connection)
  * quickly as possible and don't keep a reference to it after
  * returning it. If you need to keep the message, make a copy of it.
  *
+ * dbus_connection_dispatch() will block if called while a borrowed
+ * message is outstanding; only one piece of code can be playing with
+ * the incoming queue at a time. This function will block if called
+ * during a dbus_connection_dispatch().
+ *
  * @param connection the connection.
  * @returns next message in the incoming queue.
  */
 DBusMessage*
-dbus_connection_borrow_message  (DBusConnection *connection)
+dbus_connection_borrow_message (DBusConnection *connection)
 {
-  DBusMessage *message;
   DBusDispatchStatus status;
+  DBusMessage *message;
 
   _dbus_return_val_if_fail (connection != NULL, NULL);
-  /* can't borrow during dispatch */
-  _dbus_return_val_if_fail (!connection->dispatch_acquired, NULL);
 
   _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
   
@@ -2447,21 +2945,28 @@ dbus_connection_borrow_message  (DBusConnection *connection)
   
   CONNECTION_LOCK (connection);
 
-  if (connection->message_borrowed != NULL)
-    _dbus_connection_wait_for_borrowed (connection);
-  
-  message = _dbus_list_get_first (&connection->incoming_messages);
+  _dbus_connection_acquire_dispatch (connection);
 
-  if (message) 
-    connection->message_borrowed = message;
+  /* While a message is outstanding, the dispatch lock is held */
+  _dbus_assert (connection->message_borrowed == NULL);
+
+  connection->message_borrowed = _dbus_list_get_first (&connection->incoming_messages);
   
+  message = connection->message_borrowed;
+
+  /* Note that we KEEP the dispatch lock until the message is returned */
+  if (message == NULL)
+    _dbus_connection_release_dispatch (connection);
+
   CONNECTION_UNLOCK (connection);
+  
   return message;
 }
 
 /**
  * Used to return a message after peeking at it using
- * dbus_connection_borrow_message().
+ * dbus_connection_borrow_message(). Only called if
+ * message from dbus_connection_borrow_message() was non-#NULL.
  *
  * @param connection the connection
  * @param message the message from dbus_connection_borrow_message()
@@ -2472,15 +2977,16 @@ dbus_connection_return_message (DBusConnection *connection,
 {
   _dbus_return_if_fail (connection != NULL);
   _dbus_return_if_fail (message != NULL);
-  /* can't borrow during dispatch */
-  _dbus_return_if_fail (!connection->dispatch_acquired);
+  _dbus_return_if_fail (message == connection->message_borrowed);
+  _dbus_return_if_fail (connection->dispatch_acquired);
   
   CONNECTION_LOCK (connection);
   
   _dbus_assert (message == connection->message_borrowed);
   
   connection->message_borrowed = NULL;
-  dbus_condvar_wake_all (connection->message_returned_cond);
+
+  _dbus_connection_release_dispatch (connection);
   
   CONNECTION_UNLOCK (connection);
 }
@@ -2502,8 +3008,8 @@ dbus_connection_steal_borrowed_message (DBusConnection *connection,
 
   _dbus_return_if_fail (connection != NULL);
   _dbus_return_if_fail (message != NULL);
-  /* can't borrow during dispatch */
-  _dbus_return_if_fail (!connection->dispatch_acquired);
+  _dbus_return_if_fail (message == connection->message_borrowed);
+  _dbus_return_if_fail (connection->dispatch_acquired);
   
   CONNECTION_LOCK (connection);
  
@@ -2518,7 +3024,8 @@ dbus_connection_steal_borrowed_message (DBusConnection *connection,
                 message, connection->n_incoming);
  
   connection->message_borrowed = NULL;
-  dbus_condvar_wake_all (connection->message_returned_cond);
+
+  _dbus_connection_release_dispatch (connection);
   
   CONNECTION_UNLOCK (connection);
 }
@@ -2531,8 +3038,7 @@ _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
 {
   HAVE_LOCK_CHECK (connection);
   
-  if (connection->message_borrowed != NULL)
-    _dbus_connection_wait_for_borrowed (connection);
+  _dbus_assert (connection->message_borrowed == NULL);
   
   if (connection->n_incoming > 0)
     {
@@ -2595,6 +3101,8 @@ _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
   _dbus_assert (message_link != NULL);
   /* You can't borrow a message while a link is outstanding */
   _dbus_assert (connection->message_borrowed == NULL);
+  /* We had to have the dispatch lock across the pop/putback */
+  _dbus_assert (connection->dispatch_acquired);
 
   _dbus_list_prepend_link (&connection->incoming_messages,
                            message_link);
@@ -2624,6 +3132,11 @@ _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
  * useful in very simple programs that don't share a #DBusConnection
  * with any libraries or other modules.
  *
+ * There is a lock that covers all ways of accessing the incoming message
+ * queue, so dbus_connection_dispatch(), dbus_connection_pop_message(),
+ * dbus_connection_borrow_message(), etc. will all block while one of the others
+ * in the group is running.
+ * 
  * @param connection the connection.
  * @returns next message in the incoming queue.
  */
@@ -2643,40 +3156,52 @@ dbus_connection_pop_message (DBusConnection *connection)
     return NULL;
   
   CONNECTION_LOCK (connection);
-
+  _dbus_connection_acquire_dispatch (connection);
+  HAVE_LOCK_CHECK (connection);
+  
   message = _dbus_connection_pop_message_unlocked (connection);
 
   _dbus_verbose ("Returning popped message %p\n", message);    
-  
+
+  _dbus_connection_release_dispatch (connection);
   CONNECTION_UNLOCK (connection);
   
   return message;
 }
 
 /**
- * Acquire the dispatcher. This must be done before dispatching
- * messages in order to guarantee the right order of
- * message delivery. May sleep and drop the connection mutex
- * while waiting for the dispatcher.
+ * Acquire the dispatcher. This is a separate lock so the main
+ * connection lock can be dropped to call out to application dispatch
+ * handlers.
  *
  * @param connection the connection.
  */
 static void
 _dbus_connection_acquire_dispatch (DBusConnection *connection)
 {
-  if (connection->dispatch_acquired)
+  HAVE_LOCK_CHECK (connection);
+
+  _dbus_connection_ref_unlocked (connection);
+  CONNECTION_UNLOCK (connection);
+  
+  _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
+  _dbus_mutex_lock (connection->dispatch_mutex);
+
+  while (connection->dispatch_acquired)
     {
-#ifndef DBUS_DISABLE_CHECKS
-      connection->have_connection_lock = FALSE;
-#endif
-      dbus_condvar_wait (connection->dispatch_cond, connection->mutex);
-#ifndef DBUS_DISABLE_CHECKS
-      connection->have_connection_lock = TRUE;
-#endif
+      _dbus_verbose ("%s waiting for dispatch to be acquirable\n", _DBUS_FUNCTION_NAME);
+      _dbus_condvar_wait (connection->dispatch_cond, connection->dispatch_mutex);
     }
+  
   _dbus_assert (!connection->dispatch_acquired);
 
   connection->dispatch_acquired = TRUE;
+
+  _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
+  _dbus_mutex_unlock (connection->dispatch_mutex);
+  
+  CONNECTION_LOCK (connection);
+  _dbus_connection_unref_unlocked (connection);
 }
 
 /**
@@ -2689,10 +3214,18 @@ _dbus_connection_acquire_dispatch (DBusConnection *connection)
 static void
 _dbus_connection_release_dispatch (DBusConnection *connection)
 {
+  HAVE_LOCK_CHECK (connection);
+  
+  _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
+  _dbus_mutex_lock (connection->dispatch_mutex);
+  
   _dbus_assert (connection->dispatch_acquired);
 
   connection->dispatch_acquired = FALSE;
-  dbus_condvar_wake_one (connection->dispatch_cond);
+  _dbus_condvar_wake_one (connection->dispatch_cond);
+
+  _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
+  _dbus_mutex_unlock (connection->dispatch_mutex);
 }
 
 static void
@@ -2731,7 +3264,9 @@ _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
             {
               _dbus_verbose ("Sending disconnect message from %s\n",
                              _DBUS_FUNCTION_NAME);
-                             
+
+              connection_forget_shared_unlocked (connection);
+              
               /* We haven't sent the disconnect message already,
                * and all real messages have been queued up.
                */
@@ -2827,6 +3362,53 @@ dbus_connection_get_dispatch_status (DBusConnection *connection)
 }
 
 /**
+* Filter funtion for handling the Peer standard interface
+**/
+static DBusHandlerResult
+_dbus_connection_peer_filter (DBusConnection *connection,
+                              DBusMessage    *message)
+{
+  if (dbus_message_is_method_call (message,
+                                   DBUS_INTERFACE_PEER,
+                                   "Ping"))
+    {
+      DBusMessage *ret;
+      dbus_bool_t sent;
+      
+      ret = dbus_message_new_method_return (message);
+      if (ret == NULL)
+        return DBUS_HANDLER_RESULT_NEED_MEMORY;
+      
+      sent = dbus_connection_send (connection, ret, NULL);
+      dbus_message_unref (ret);
+
+      if (!sent)
+        return DBUS_HANDLER_RESULT_NEED_MEMORY;
+      
+      return DBUS_HANDLER_RESULT_HANDLED;
+    }
+                                   
+  
+  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+/**
+* Processes all builtin filter functions
+*
+* If the spec specifies a standard interface
+* they should be processed from this method
+**/
+static DBusHandlerResult
+_dbus_connection_run_builtin_filters (DBusConnection *connection,
+                                      DBusMessage    *message)
+{
+  /* We just run one filter for now but have the option to run more
+     if the spec calls for it in the future */
+
+  return _dbus_connection_peer_filter (connection, message);
+}
+
+/**
  * Processes data buffered while handling watches, queueing zero or
  * more incoming messages. Then pops the first-received message from
  * the current incoming message queue, runs any handlers for it, and
@@ -2839,16 +3421,12 @@ dbus_connection_get_dispatch_status (DBusConnection *connection)
  *
  * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY
  *
- * @todo right now a message filter gets run on replies to a pending
- * call in here, but not in the case where we block without entering
- * the main loop. Simple solution might be to just have the pending
- * call stuff run before the filters.
- *
  * @todo FIXME what if we call out to application code to handle a
  * message, holding the dispatch lock, and the application code runs
  * the main loop and dispatches again? Probably deadlocks at the
  * moment. Maybe we want a dispatch status of DBUS_DISPATCH_IN_PROGRESS,
- * and then the GSource etc. could handle the situation?
+ * and then the GSource etc. could handle the situation? Right now
+ * our GSource is NO_RECURSE
  * 
  * @param connection the connection
  * @returns dispatch status
@@ -2882,19 +3460,14 @@ dbus_connection_dispatch (DBusConnection *connection)
   _dbus_connection_ref_unlocked (connection);
 
   _dbus_connection_acquire_dispatch (connection);
-  
-  /* This call may drop the lock during the execution (if waiting for
-   * borrowed messages to be returned) but the order of message
-   * dispatch if several threads call dispatch() is still
-   * protected by the lock, since only one will get the lock, and that
-   * one will finish the message dispatching
-   */
+  HAVE_LOCK_CHECK (connection);
+
   message_link = _dbus_connection_pop_message_link_unlocked (connection);
   if (message_link == NULL)
     {
       /* another thread dispatched our stuff */
 
-      _dbus_verbose ("another thread dispatched message\n");
+      _dbus_verbose ("another thread dispatched message (during acquire_dispatch above)\n");
       
       _dbus_connection_release_dispatch (connection);
 
@@ -2919,23 +3492,48 @@ dbus_connection_dispatch (DBusConnection *connection)
                  dbus_message_get_member (message) :
                  "no member",
                  dbus_message_get_signature (message));
-  
-  result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
+  result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+  
+  /* Pending call handling must be first, because if you do
+   * dbus_connection_send_with_reply_and_block() or
+   * dbus_pending_call_block() then no handlers/filters will be run on
+   * the reply. We want consistent semantics in the case where we
+   * dbus_connection_dispatch() the reply.
+   */
+  
   reply_serial = dbus_message_get_reply_serial (message);
   pending = _dbus_hash_table_lookup_int (connection->pending_replies,
                                          reply_serial);
-  
+  if (pending)
+    {
+      _dbus_verbose ("Dispatching a pending reply\n");
+      _dbus_pending_call_complete_and_unlock (pending, message);
+      pending = NULL; /* it's probably unref'd */
+      
+      CONNECTION_LOCK (connection);
+      _dbus_verbose ("pending call completed in dispatch\n");
+      result = DBUS_HANDLER_RESULT_HANDLED;
+      goto out;
+    }
+  result = _dbus_connection_run_builtin_filters (connection, message);
+  if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
+    goto out;
   if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
     {
       _dbus_connection_release_dispatch (connection);
-
+      HAVE_LOCK_CHECK (connection);
+      
       _dbus_connection_failed_pop (connection, message_link);
 
       /* unlocks and calls user code */
       _dbus_connection_update_dispatch_status_and_unlock (connection,
                                                           DBUS_DISPATCH_NEED_MEMORY);
 
+      if (pending)
+        dbus_pending_call_unref (pending);
       dbus_connection_unref (connection);
       
       return DBUS_DISPATCH_NEED_MEMORY;
@@ -2977,40 +3575,11 @@ dbus_connection_dispatch (DBusConnection *connection)
       _dbus_verbose ("No memory in %s\n", _DBUS_FUNCTION_NAME);
       goto out;
     }
-  
-  /* Did a reply we were waiting on get filtered? */
-  if (pending && result == DBUS_HANDLER_RESULT_HANDLED)
-    {
-      /* Queue the timeout immediately! */
-      if (pending->timeout_link)
-       {
-         _dbus_connection_queue_synthesized_message_link (connection,
-                                                          pending->timeout_link);
-         pending->timeout_link = NULL;
-       }
-      else
-       {
-         /* We already queued the timeout? Then it was filtered! */
-         _dbus_warn ("The timeout error with reply serial %d was filtered, so the DBusPendingCall will never stop pending.\n", reply_serial);
-       }
-    }
-  
-  if (result == DBUS_HANDLER_RESULT_HANDLED)
+  else if (result == DBUS_HANDLER_RESULT_HANDLED)
     {
       _dbus_verbose ("filter handled message in dispatch\n");
       goto out;
     }
-  
-  if (pending)
-    {
-      _dbus_pending_call_complete_and_unlock (pending, message);
-
-      pending = NULL;
-      
-      CONNECTION_LOCK (connection);
-      _dbus_verbose ("pending call completed in dispatch\n");
-      goto out;
-    }
 
   /* We're still protected from dispatch() reentrancy here
    * since we acquired the dispatcher
@@ -3125,7 +3694,7 @@ dbus_connection_dispatch (DBusConnection *connection)
       
       if (connection->exit_on_disconnect &&
           dbus_message_is_signal (message,
-                                  DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
+                                  DBUS_INTERFACE_LOCAL,
                                   "Disconnected"))
         {
           _dbus_verbose ("Exiting on Disconnected signal\n");
@@ -3141,6 +3710,7 @@ dbus_connection_dispatch (DBusConnection *connection)
     }
   
   _dbus_connection_release_dispatch (connection);
+  HAVE_LOCK_CHECK (connection);
 
   _dbus_verbose ("%s before final status update\n", _DBUS_FUNCTION_NAME);
   status = _dbus_connection_get_dispatch_status_unlocked (connection);
@@ -3794,6 +4364,43 @@ dbus_connection_unregister_object_path (DBusConnection              *connection,
 }
 
 /**
+ * Gets the user data passed to dbus_connection_register_object_path()
+ * or dbus_connection_register_fallback(). If nothing was registered
+ * at this path, the data is filled in with #NULL.
+ *
+ * @param connection the connection
+ * @param path the path you registered with
+ * @param data_p location to store the user data, or #NULL
+ * @returns #FALSE if not enough memory
+ */
+dbus_bool_t
+dbus_connection_get_object_path_data (DBusConnection *connection,
+                                      const char     *path,
+                                      void          **data_p)
+{
+  char **decomposed_path;
+
+  _dbus_return_val_if_fail (connection != NULL, FALSE);
+  _dbus_return_val_if_fail (path != NULL, FALSE);
+  _dbus_return_val_if_fail (data_p != NULL, FALSE);
+
+  *data_p = NULL;
+  
+  if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
+    return FALSE;
+  
+  CONNECTION_LOCK (connection);
+
+  *data_p = _dbus_object_tree_get_user_data_unlocked (connection->objects, (const char**) decomposed_path);
+
+  CONNECTION_UNLOCK (connection);
+
+  dbus_free_string_array (decomposed_path);
+
+  return TRUE;
+}
+
+/**
  * Lists the registered fallback handlers and object path handlers at
  * the given parent_path. The returned array should be freed with
  * dbus_free_string_array().