summaryrefslogtreecommitdiffstats
path: root/dbus
diff options
context:
space:
mode:
authorHavoc Pennington <hp@redhat.com>2005-04-09 23:50:58 +0000
committerHavoc Pennington <hp@redhat.com>2005-04-09 23:50:58 +0000
commit8475b3a8486f03935ec7afab9eab096577921b3e (patch)
tree32682192d86d23b4d7992b0b47935a491c8f0bd3 /dbus
parent9f13881562a0b3af65f6aa2d14b5f21ff718b66e (diff)
2005-04-09 Havoc Pennington <hp@redhat.com>
* dbus/dbus-message-util.c (_dbus_message_test): fix signedness warning * glib/dbus-glib-tool.c (main): fix warning * glib/dbus-binding-tool-glib.c (generate_glue): fix warning * dbus/dbus-connection.c (dbus_connection_read_write_dispatch): add a new function that can be used in simple applications that don't have a main loop and are willing to block
Diffstat (limited to 'dbus')
-rw-r--r--dbus/dbus-connection.c106
-rw-r--r--dbus/dbus-connection.h2
-rw-r--r--dbus/dbus-message-util.c6
-rw-r--r--dbus/dbus-server.c2
4 files changed, 105 insertions, 11 deletions
diff --git a/dbus/dbus-connection.c b/dbus/dbus-connection.c
index a08aa366..df8de937 100644
--- a/dbus/dbus-connection.c
+++ b/dbus/dbus-connection.c
@@ -2520,6 +2520,28 @@ check_for_reply_unlocked (DBusConnection *connection,
}
/**
+ * 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.
+ *
+ * @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
@@ -2663,13 +2685,8 @@ _dbus_connection_block_pending_call (DBusPendingCall *pending)
* 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
{
@@ -2804,6 +2821,81 @@ dbus_connection_flush (DBusConnection *connection)
}
/**
+ * 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
+ */
+dbus_bool_t
+dbus_connection_read_write_dispatch (DBusConnection *connection,
+ int timeout_milliseconds)
+{
+ 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);
+
+ if (dstatus == DBUS_DISPATCH_DATA_REMAINS)
+ {
+ _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 */
+}
+
+/**
* Returns the first-received message from the incoming message queue,
* leaving it in the queue. If the queue is empty, returns #NULL.
*
diff --git a/dbus/dbus-connection.h b/dbus/dbus-connection.h
index 0dc1b617..2c0b7f2c 100644
--- a/dbus/dbus-connection.h
+++ b/dbus/dbus-connection.h
@@ -99,6 +99,8 @@ dbus_bool_t dbus_connection_get_is_authenticated (DBusConnection
void dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
dbus_bool_t exit_on_disconnect);
void dbus_connection_flush (DBusConnection *connection);
+dbus_bool_t dbus_connection_read_write_dispatch (DBusConnection *connection,
+ int timeout_milliseconds);
DBusMessage* dbus_connection_borrow_message (DBusConnection *connection);
void dbus_connection_return_message (DBusConnection *connection,
DBusMessage *message);
diff --git a/dbus/dbus-message-util.c b/dbus/dbus-message-util.c
index e9540be4..81034da4 100644
--- a/dbus/dbus-message-util.c
+++ b/dbus/dbus-message-util.c
@@ -715,7 +715,7 @@ verify_test_message (DBusMessage *message)
double v_DOUBLE;
dbus_bool_t our_bool;
unsigned char our_byte_1, our_byte_2;
- const dbus_int32_t *our_uint32_array = (void*)0xdeadbeef;
+ const dbus_uint32_t *our_uint32_array = (void*)0xdeadbeef;
int our_uint32_array_len;
dbus_int32_t *our_int32_array = (void*)0xdeadbeef;
int our_int32_array_len;
@@ -911,14 +911,14 @@ _dbus_message_test (const char *test_data_dir)
const char *name2;
const dbus_uint32_t our_uint32_array[] =
{ 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
- const dbus_uint32_t our_int32_array[] =
+ const dbus_int32_t our_int32_array[] =
{ 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
const dbus_uint32_t *v_ARRAY_UINT32 = our_uint32_array;
const dbus_int32_t *v_ARRAY_INT32 = our_int32_array;
#ifdef DBUS_HAVE_INT64
const dbus_uint64_t our_uint64_array[] =
{ 0x12345678, 0x23456781, 0x34567812, 0x45678123 };
- const dbus_uint64_t our_int64_array[] =
+ const dbus_int64_t our_int64_array[] =
{ 0x12345678, -0x23456781, 0x34567812, -0x45678123 };
const dbus_uint64_t *v_ARRAY_UINT64 = our_uint64_array;
const dbus_int64_t *v_ARRAY_INT64 = our_int64_array;
diff --git a/dbus/dbus-server.c b/dbus/dbus-server.c
index 0a3b522d..d1ab496a 100644
--- a/dbus/dbus-server.c
+++ b/dbus/dbus-server.c
@@ -129,7 +129,7 @@ _dbus_server_init_base (DBusServer *server,
init_guid (&server->guid);
- _dbus_string_init_const_len (&guid_raw, server->guid.as_bytes,
+ _dbus_string_init_const_len (&guid_raw, (signed char*) server->guid.as_bytes,
sizeof (server->guid.as_bytes));
if (!_dbus_string_hex_encode (&guid_raw, 0,
&server->guid_hex,