2003-03-14 Havoc Pennington <hp@pobox.com>

* bus/loop.c (bus_loop_iterate): add this so we can "run loop
	until no work remains" in test code. (the large diff here
	is just code movement, no actual changes)

	* dbus/dbus-server-debug.c (DEFAULT_INTERVAL): change interval to
	1, no point waiting around for test code.
	(_dbus_server_debug_accept_transport): unref the timeout
	after adding it (right?)

	* dbus/dbus-transport-debug.c (DEFAULT_INTERVAL): ditto
This commit is contained in:
Havoc Pennington 2003-03-14 05:17:19 +00:00
parent 3bea935316
commit f468907fb0
7 changed files with 281 additions and 241 deletions

View file

@ -1,3 +1,16 @@
2003-03-14 Havoc Pennington <hp@pobox.com>
* bus/loop.c (bus_loop_iterate): add this so we can "run loop
until no work remains" in test code. (the large diff here
is just code movement, no actual changes)
* dbus/dbus-server-debug.c (DEFAULT_INTERVAL): change interval to
1, no point waiting around for test code.
(_dbus_server_debug_accept_transport): unref the timeout
after adding it (right?)
* dbus/dbus-transport-debug.c (DEFAULT_INTERVAL): ditto
2003-03-13 Havoc Pennington <hp@redhat.com>
* dbus/dbus-timeout.c (_dbus_timeout_list_set_functions): handle

View file

@ -262,247 +262,269 @@ bus_loop_remove_timeout (DBusTimeout *timeout,
timeout, function, data);
}
/* Returns TRUE if we dispatch any callbacks, which is just used in
* test code as a debug hack
*/
dbus_bool_t
bus_loop_iterate (dbus_bool_t block)
{
dbus_bool_t retval;
DBusPollFD *fds;
int n_fds;
WatchCallback **watches_for_fds;
int i;
DBusList *link;
int n_ready;
int initial_serial;
long timeout;
retval = FALSE;
fds = NULL;
watches_for_fds = NULL;
if (callbacks == NULL)
{
bus_loop_quit ();
goto next_iteration;
}
n_fds = watch_count;
if (n_fds > 0)
{
fds = dbus_new0 (DBusPollFD, n_fds);
while (fds == NULL)
{
bus_wait_for_memory ();
fds = dbus_new0 (DBusPollFD, n_fds);
}
watches_for_fds = dbus_new (WatchCallback*, n_fds);
while (watches_for_fds == NULL)
{
bus_wait_for_memory ();
watches_for_fds = dbus_new (WatchCallback*, n_fds);
}
i = 0;
link = _dbus_list_get_first_link (&callbacks);
while (link != NULL)
{
DBusList *next = _dbus_list_get_next_link (&callbacks, link);
Callback *cb = link->data;
if (cb->type == CALLBACK_WATCH)
{
unsigned int flags;
WatchCallback *wcb = WATCH_CALLBACK (cb);
watches_for_fds[i] = wcb;
flags = dbus_watch_get_flags (wcb->watch);
fds[i].fd = dbus_watch_get_fd (wcb->watch);
if (flags & DBUS_WATCH_READABLE)
fds[i].events |= _DBUS_POLLIN;
if (flags & DBUS_WATCH_WRITABLE)
fds[i].events |= _DBUS_POLLOUT;
++i;
}
link = next;
}
_dbus_assert (i == n_fds);
}
timeout = -1;
if (timeout_count > 0)
{
unsigned long tv_sec;
unsigned long tv_usec;
_dbus_get_current_time (&tv_sec, &tv_usec);
link = _dbus_list_get_first_link (&callbacks);
while (link != NULL)
{
DBusList *next = _dbus_list_get_next_link (&callbacks, link);
Callback *cb = link->data;
if (cb->type == CALLBACK_TIMEOUT)
{
TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
unsigned long interval;
unsigned long elapsed;
if (tcb->last_tv_sec > tv_sec ||
(tcb->last_tv_sec == tv_sec &&
tcb->last_tv_usec > tv_usec))
{
/* Clock went backward, pretend timeout
* was just installed.
*/
tcb->last_tv_sec = tv_sec;
tcb->last_tv_usec = tv_usec;
_dbus_verbose ("System clock went backward\n");
}
interval = dbus_timeout_get_interval (tcb->timeout);
elapsed =
(tv_sec - tcb->last_tv_sec) * 1000 +
(tv_usec - tcb->last_tv_usec) / 1000;
if (interval < elapsed)
timeout = 0;
else if (timeout < 0)
timeout = interval - elapsed;
else
timeout = MIN (((unsigned long)timeout), interval - elapsed);
_dbus_assert (timeout >= 0);
if (timeout == 0)
break; /* it's not going to get shorter... */
}
link = next;
}
}
if (!block)
timeout = 0;
n_ready = _dbus_poll (fds, n_fds, timeout);
initial_serial = callback_list_serial;
if (timeout_count > 0)
{
unsigned long tv_sec;
unsigned long tv_usec;
_dbus_get_current_time (&tv_sec, &tv_usec);
/* It'd be nice to avoid this O(n) thingy here */
link = _dbus_list_get_first_link (&callbacks);
while (link != NULL)
{
DBusList *next = _dbus_list_get_next_link (&callbacks, link);
Callback *cb = link->data;
if (initial_serial != callback_list_serial)
goto next_iteration;
if (exited)
goto next_iteration;
if (cb->type == CALLBACK_TIMEOUT)
{
TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
unsigned long interval;
unsigned long elapsed;
if (tcb->last_tv_sec > tv_sec ||
(tcb->last_tv_sec == tv_sec &&
tcb->last_tv_usec > tv_usec))
{
/* Clock went backward, pretend timeout
* was just installed.
*/
tcb->last_tv_sec = tv_sec;
tcb->last_tv_usec = tv_usec;
_dbus_verbose ("System clock went backward\n");
goto next_timeout;
}
interval = dbus_timeout_get_interval (tcb->timeout);
elapsed =
(tv_sec - tcb->last_tv_sec) * 1000 +
(tv_usec - tcb->last_tv_usec) / 1000;
if (interval <= elapsed)
{
/* Save last callback time and fire this timeout */
tcb->last_tv_sec = tv_sec;
tcb->last_tv_usec = tv_usec;
(* tcb->function) (tcb->timeout,
cb->data);
retval = TRUE;
}
}
next_timeout:
link = next;
}
}
if (n_ready > 0)
{
i = 0;
while (i < n_fds)
{
/* FIXME I think this "restart if we change the watches"
* approach could result in starving watches
* toward the end of the list.
*/
if (initial_serial != callback_list_serial)
goto next_iteration;
if (exited)
goto next_iteration;
if (fds[i].revents != 0)
{
WatchCallback *wcb;
unsigned int condition;
wcb = watches_for_fds[i];
condition = 0;
if (fds[i].revents & _DBUS_POLLIN)
condition |= DBUS_WATCH_READABLE;
if (fds[i].revents & _DBUS_POLLOUT)
condition |= DBUS_WATCH_WRITABLE;
if (fds[i].revents & _DBUS_POLLHUP)
condition |= DBUS_WATCH_HANGUP;
if (fds[i].revents & _DBUS_POLLERR)
condition |= DBUS_WATCH_ERROR;
/* condition may still be 0 if we got some
* weird POLLFOO thing like POLLWRBAND
*/
if (condition != 0)
{
(* wcb->function) (wcb->watch,
condition,
((Callback*)wcb)->data);
retval = TRUE;
}
}
++i;
}
}
next_iteration:
dbus_free (fds);
dbus_free (watches_for_fds);
return retval;
}
void
bus_loop_run (void)
{
while (!exited)
{
DBusPollFD *fds;
int n_fds;
WatchCallback **watches_for_fds;
int i;
DBusList *link;
int n_ready;
int initial_serial;
long timeout;
fds = NULL;
watches_for_fds = NULL;
if (callbacks == NULL)
{
bus_loop_quit ();
goto next_iteration;
}
n_fds = watch_count;
if (n_fds > 0)
{
fds = dbus_new0 (DBusPollFD, n_fds);
while (fds == NULL)
{
bus_wait_for_memory ();
fds = dbus_new0 (DBusPollFD, n_fds);
}
watches_for_fds = dbus_new (WatchCallback*, n_fds);
while (watches_for_fds == NULL)
{
bus_wait_for_memory ();
watches_for_fds = dbus_new (WatchCallback*, n_fds);
}
i = 0;
link = _dbus_list_get_first_link (&callbacks);
while (link != NULL)
{
DBusList *next = _dbus_list_get_next_link (&callbacks, link);
Callback *cb = link->data;
if (cb->type == CALLBACK_WATCH)
{
unsigned int flags;
WatchCallback *wcb = WATCH_CALLBACK (cb);
watches_for_fds[i] = wcb;
flags = dbus_watch_get_flags (wcb->watch);
fds[i].fd = dbus_watch_get_fd (wcb->watch);
if (flags & DBUS_WATCH_READABLE)
fds[i].events |= _DBUS_POLLIN;
if (flags & DBUS_WATCH_WRITABLE)
fds[i].events |= _DBUS_POLLOUT;
++i;
}
link = next;
}
_dbus_assert (i == n_fds);
}
timeout = -1;
if (timeout_count > 0)
{
unsigned long tv_sec;
unsigned long tv_usec;
_dbus_get_current_time (&tv_sec, &tv_usec);
link = _dbus_list_get_first_link (&callbacks);
while (link != NULL)
{
DBusList *next = _dbus_list_get_next_link (&callbacks, link);
Callback *cb = link->data;
if (cb->type == CALLBACK_TIMEOUT)
{
TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
unsigned long interval;
unsigned long elapsed;
if (tcb->last_tv_sec > tv_sec ||
(tcb->last_tv_sec == tv_sec &&
tcb->last_tv_usec > tv_usec))
{
/* Clock went backward, pretend timeout
* was just installed.
*/
tcb->last_tv_sec = tv_sec;
tcb->last_tv_usec = tv_usec;
_dbus_verbose ("System clock went backward\n");
}
interval = dbus_timeout_get_interval (tcb->timeout);
elapsed =
(tv_sec - tcb->last_tv_sec) * 1000 +
(tv_usec - tcb->last_tv_usec) / 1000;
if (interval < elapsed)
timeout = 0;
else if (timeout < 0)
timeout = interval - elapsed;
else
timeout = MIN (((unsigned long)timeout), interval - elapsed);
_dbus_assert (timeout >= 0);
if (timeout == 0)
break; /* it's not going to get shorter... */
}
link = next;
}
}
n_ready = _dbus_poll (fds, n_fds, timeout);
initial_serial = callback_list_serial;
if (timeout_count > 0)
{
unsigned long tv_sec;
unsigned long tv_usec;
_dbus_get_current_time (&tv_sec, &tv_usec);
/* It'd be nice to avoid this O(n) thingy here */
link = _dbus_list_get_first_link (&callbacks);
while (link != NULL)
{
DBusList *next = _dbus_list_get_next_link (&callbacks, link);
Callback *cb = link->data;
if (initial_serial != callback_list_serial)
goto next_iteration;
if (exited)
goto next_iteration;
if (cb->type == CALLBACK_TIMEOUT)
{
TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
unsigned long interval;
unsigned long elapsed;
if (tcb->last_tv_sec > tv_sec ||
(tcb->last_tv_sec == tv_sec &&
tcb->last_tv_usec > tv_usec))
{
/* Clock went backward, pretend timeout
* was just installed.
*/
tcb->last_tv_sec = tv_sec;
tcb->last_tv_usec = tv_usec;
_dbus_verbose ("System clock went backward\n");
goto next_timeout;
}
interval = dbus_timeout_get_interval (tcb->timeout);
elapsed =
(tv_sec - tcb->last_tv_sec) * 1000 +
(tv_usec - tcb->last_tv_usec) / 1000;
if (interval <= elapsed)
{
/* Save last callback time and fire this timeout */
tcb->last_tv_sec = tv_sec;
tcb->last_tv_usec = tv_usec;
(* tcb->function) (tcb->timeout,
cb->data);
}
}
next_timeout:
link = next;
}
}
if (n_ready > 0)
{
i = 0;
while (i < n_fds)
{
/* FIXME I think this "restart if we change the watches"
* approach could result in starving watches
* toward the end of the list.
*/
if (initial_serial != callback_list_serial)
goto next_iteration;
if (exited)
goto next_iteration;
if (fds[i].revents != 0)
{
WatchCallback *wcb;
unsigned int condition;
wcb = watches_for_fds[i];
condition = 0;
if (fds[i].revents & _DBUS_POLLIN)
condition |= DBUS_WATCH_READABLE;
if (fds[i].revents & _DBUS_POLLOUT)
condition |= DBUS_WATCH_WRITABLE;
if (fds[i].revents & _DBUS_POLLHUP)
condition |= DBUS_WATCH_HANGUP;
if (fds[i].revents & _DBUS_POLLERR)
condition |= DBUS_WATCH_ERROR;
/* condition may still be 0 if we got some
* weird POLLFOO thing like POLLWRBAND
*/
if (condition != 0)
(* wcb->function) (wcb->watch,
condition,
((Callback*)wcb)->data);
}
++i;
}
}
next_iteration:
dbus_free (fds);
dbus_free (watches_for_fds);
}
bus_loop_iterate (TRUE);
}
void

View file

@ -48,6 +48,7 @@ void bus_loop_remove_timeout (DBusTimeout *timeout,
void *data);
void bus_loop_run (void);
void bus_loop_quit (void);
dbus_bool_t bus_loop_iterate (dbus_bool_t block);

View file

@ -334,6 +334,7 @@ _dbus_connection_remove_watch (DBusConnection *connection,
* available. Otherwise records the timeout to be added when said
* function is available. Also re-adds the timeout if the
* DBusAddTimeoutFunction changes. May fail due to lack of memory.
* The timeout will fire only one time.
*
* @param connection the connection.
* @param timeout the timeout to add.
@ -1861,7 +1862,8 @@ dbus_connection_set_watch_functions (DBusConnection *connection,
* dbus_timeout_get_interval.
*
* Once a timeout occurs, dbus_timeout_handle should be called to invoke
* the timeout's callback.
* the timeout's callback, and the timeout should be automatically
* removed. i.e. timeouts are one-shot.
*
* @param connection the connection.
* @param add_function function to add a timeout.

View file

@ -43,7 +43,7 @@
/**
* Default timeout interval when reading or writing.
*/
#define DEFAULT_INTERVAL 10
#define DEFAULT_INTERVAL 1
/**
* Opaque object representing a debug server implementation.
@ -249,6 +249,8 @@ _dbus_server_debug_accept_transport (DBusServer *server,
if (!_dbus_server_add_timeout (server, timeout))
goto failed;
_dbus_timeout_unref (timeout);
return TRUE;
failed:

View file

@ -146,7 +146,7 @@ _dbus_server_remove_watch (DBusServer *server,
/**
* Adds a timeout for this server, chaining out to application-provided
* timeout handlers.
* timeout handlers. The timeout will fire only one time.
*
* @param server the server.
* @param timeout the timeout to add.

View file

@ -44,7 +44,7 @@
/**
* Default timeout interval when reading or writing.
*/
#define DEFAULT_INTERVAL 10
#define DEFAULT_INTERVAL 1
/**
* Opaque object representing a debug transport.