Commit 520802f8 authored by Simon McVittie's avatar Simon McVittie

DBusMainLoop, DBusSocketSet: work in terms of DBusPollable

This requires generic support for keying hash tables by DBusPollable:
there are already implementations for int and uintptr_t keys, but not
for "int or uintptr_t depending on platform", which is what
DBusPollable now means.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=89444
parent 6ac3d6f7
......@@ -322,9 +322,9 @@ setup_reload_pipe (DBusLoop *loop)
exit (1);
}
watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
DBUS_WATCH_READABLE, TRUE,
handle_reload_watch, NULL, NULL);
watch = _dbus_watch_new (DBUS_SOCKET_GET_POLLABLE (reload_pipe[RELOAD_READ_END]),
DBUS_WATCH_READABLE, TRUE,
handle_reload_watch, NULL, NULL);
if (watch == NULL)
{
......
......@@ -149,6 +149,56 @@ void _dbus_hash_table_insert_string_preallocated (DBusHashTable
char *key,
void *value);
#ifdef DBUS_WIN
# define DBUS_HASH_POLLABLE DBUS_HASH_UINTPTR
#else
# define DBUS_HASH_POLLABLE DBUS_HASH_INT
#endif
static inline DBusPollable
_dbus_hash_iter_get_pollable_key (DBusHashIter *iter)
{
#ifdef DBUS_WIN
return _dbus_hash_iter_get_uintptr_key (iter);
#else
return _dbus_hash_iter_get_int_key (iter);
#endif
}
static inline void *
_dbus_hash_table_lookup_pollable (DBusHashTable *table,
DBusPollable key)
{
#ifdef DBUS_WIN
return _dbus_hash_table_lookup_uintptr (table, key);
#else
return _dbus_hash_table_lookup_int (table, key);
#endif
}
static inline dbus_bool_t
_dbus_hash_table_remove_pollable (DBusHashTable *table,
DBusPollable key)
{
#ifdef DBUS_WIN
return _dbus_hash_table_remove_uintptr (table, key);
#else
return _dbus_hash_table_remove_int (table, key);
#endif
}
static inline dbus_bool_t
_dbus_hash_table_insert_pollable (DBusHashTable *table,
DBusPollable key,
void *value)
{
#ifdef DBUS_WIN
return _dbus_hash_table_insert_uintptr (table, key, value);
#else
return _dbus_hash_table_insert_int (table, key, value);
#endif
}
/** @} */
DBUS_END_DECLS
......
......@@ -37,7 +37,7 @@
struct DBusLoop
{
int refcount;
/** fd => dbus_malloc'd DBusList ** of references to DBusWatch */
/** DBusPollable => dbus_malloc'd DBusList ** of references to DBusWatch */
DBusHashTable *watches;
DBusSocketSet *socket_set;
DBusList *timeouts;
......@@ -112,7 +112,7 @@ _dbus_loop_new (void)
if (loop == NULL)
return NULL;
loop->watches = _dbus_hash_table_new (DBUS_HASH_INT, NULL,
loop->watches = _dbus_hash_table_new (DBUS_HASH_POLLABLE, NULL,
free_watch_table_entry);
loop->socket_set = _dbus_socket_set_new (0);
......@@ -168,12 +168,12 @@ _dbus_loop_unref (DBusLoop *loop)
}
static DBusList **
ensure_watch_table_entry (DBusLoop *loop,
int fd)
ensure_watch_table_entry (DBusLoop *loop,
DBusPollable fd)
{
DBusList **watches;
watches = _dbus_hash_table_lookup_int (loop->watches, fd);
watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
if (watches == NULL)
{
......@@ -182,7 +182,7 @@ ensure_watch_table_entry (DBusLoop *loop,
if (watches == NULL)
return watches;
if (!_dbus_hash_table_insert_int (loop->watches, fd, watches))
if (!_dbus_hash_table_insert_pollable (loop->watches, fd, watches))
{
dbus_free (watches);
watches = NULL;
......@@ -193,14 +193,15 @@ ensure_watch_table_entry (DBusLoop *loop,
}
static void
cull_watches_for_invalid_fd (DBusLoop *loop,
int fd)
cull_watches_for_invalid_fd (DBusLoop *loop,
DBusPollable fd)
{
DBusList *link;
DBusList **watches;
_dbus_warn ("invalid request, socket fd %d not open\n", fd);
watches = _dbus_hash_table_lookup_int (loop->watches, fd);
_dbus_warn ("invalid request, socket fd %" DBUS_POLLABLE_FORMAT " not open\n",
DBUS_POLLABLE_PRINTABLE (fd));
watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
if (watches != NULL)
{
......@@ -210,13 +211,13 @@ cull_watches_for_invalid_fd (DBusLoop *loop,
_dbus_watch_invalidate (link->data);
}
_dbus_hash_table_remove_int (loop->watches, fd);
_dbus_hash_table_remove_pollable (loop->watches, fd);
}
static dbus_bool_t
gc_watch_table_entry (DBusLoop *loop,
DBusList **watches,
int fd)
gc_watch_table_entry (DBusLoop *loop,
DBusList **watches,
DBusPollable fd)
{
/* If watches is already NULL we have nothing to do */
if (watches == NULL)
......@@ -226,23 +227,23 @@ gc_watch_table_entry (DBusLoop *loop,
if (*watches != NULL)
return FALSE;
_dbus_hash_table_remove_int (loop->watches, fd);
_dbus_hash_table_remove_pollable (loop->watches, fd);
return TRUE;
}
static void
refresh_watches_for_fd (DBusLoop *loop,
DBusList **watches,
int fd)
refresh_watches_for_fd (DBusLoop *loop,
DBusList **watches,
DBusPollable fd)
{
DBusList *link;
unsigned int flags = 0;
dbus_bool_t interested = FALSE;
_dbus_assert (fd != -1);
_dbus_assert (DBUS_POLLABLE_IS_VALID (fd));
if (watches == NULL)
watches = _dbus_hash_table_lookup_int (loop->watches, fd);
watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
/* we allocated this in the first _dbus_loop_add_watch for the fd, and keep
* it until there are none left */
......@@ -270,11 +271,11 @@ dbus_bool_t
_dbus_loop_add_watch (DBusLoop *loop,
DBusWatch *watch)
{
DBusSocket fd;
DBusPollable fd;
DBusList **watches;
fd = _dbus_watch_get_socket (watch);
_dbus_assert (fd != DBUS_SOCKET_INVALID);
fd = _dbus_watch_get_pollable (watch);
_dbus_assert (DBUS_POLLABLE_IS_VALID (fd));
watches = ensure_watch_table_entry (loop, fd);
......@@ -295,7 +296,7 @@ _dbus_loop_add_watch (DBusLoop *loop,
dbus_watch_get_flags (watch),
dbus_watch_get_enabled (watch)))
{
_dbus_hash_table_remove_int (loop->watches, fd);
_dbus_hash_table_remove_pollable (loop->watches, fd);
return FALSE;
}
}
......@@ -314,7 +315,7 @@ void
_dbus_loop_toggle_watch (DBusLoop *loop,
DBusWatch *watch)
{
refresh_watches_for_fd (loop, NULL, dbus_watch_get_socket (watch));
refresh_watches_for_fd (loop, NULL, _dbus_watch_get_pollable (watch));
}
void
......@@ -323,15 +324,15 @@ _dbus_loop_remove_watch (DBusLoop *loop,
{
DBusList **watches;
DBusList *link;
DBusSocket fd;
DBusPollable fd;
/* This relies on people removing watches before they invalidate them,
* which has been safe since fd.o #33336 was fixed. Assert about it
* so we don't regress. */
fd = _dbus_watch_get_socket (watch);
_dbus_assert (fd != DBUS_SOCKET_INVALID);
fd = _dbus_watch_get_pollable (watch);
_dbus_assert (DBUS_POLLABLE_IS_VALID (fd));
watches = _dbus_hash_table_lookup_int (loop->watches, fd);
watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
if (watches != NULL)
{
......@@ -669,11 +670,11 @@ _dbus_loop_iterate (DBusLoop *loop,
while (_dbus_hash_iter_next (&hash_iter))
{
DBusList **watches;
int fd;
DBusPollable fd;
dbus_bool_t changed;
changed = FALSE;
fd = _dbus_hash_iter_get_int_key (&hash_iter);
fd = _dbus_hash_iter_get_pollable_key (&hash_iter);
watches = _dbus_hash_iter_get_value (&hash_iter);
for (link = _dbus_list_get_first_link (watches);
......@@ -795,8 +796,8 @@ _dbus_loop_iterate (DBusLoop *loop,
if (condition == 0)
continue;
watches = _dbus_hash_table_lookup_int (loop->watches,
ready_fds[i].fd);
watches = _dbus_hash_table_lookup_pollable (loop->watches,
ready_fds[i].fd);
if (watches == NULL)
continue;
......
......@@ -307,7 +307,7 @@ _dbus_server_new_for_socket (DBusSocket *fds,
{
DBusWatch *watch;
watch = _dbus_watch_new (fds[i],
watch = _dbus_watch_new (DBUS_SOCKET_GET_POLLABLE (fds[i]),
DBUS_WATCH_READABLE,
TRUE,
socket_handle_watch, socket_server,
......
......@@ -136,7 +136,7 @@ epoll_events_to_watch_flags (uint32_t events)
static dbus_bool_t
socket_set_epoll_add (DBusSocketSet *set,
int fd,
DBusPollable fd,
unsigned int flags,
dbus_bool_t enabled)
{
......@@ -189,7 +189,7 @@ socket_set_epoll_add (DBusSocketSet *set,
static void
socket_set_epoll_enable (DBusSocketSet *set,
int fd,
DBusPollable fd,
unsigned int flags)
{
DBusSocketSetEpoll *self = socket_set_epoll_cast (set);
......@@ -229,7 +229,7 @@ socket_set_epoll_enable (DBusSocketSet *set,
static void
socket_set_epoll_disable (DBusSocketSet *set,
int fd)
DBusPollable fd)
{
DBusSocketSetEpoll *self = socket_set_epoll_cast (set);
struct epoll_event event;
......@@ -264,7 +264,7 @@ socket_set_epoll_disable (DBusSocketSet *set,
static void
socket_set_epoll_remove (DBusSocketSet *set,
int fd)
DBusPollable fd)
{
DBusSocketSetEpoll *self = socket_set_epoll_cast (set);
int err;
......
......@@ -114,7 +114,7 @@ watch_flags_to_poll_events (unsigned int flags)
static dbus_bool_t
socket_set_poll_add (DBusSocketSet *set,
int fd,
DBusPollable fd,
unsigned int flags,
dbus_bool_t enabled)
{
......@@ -123,7 +123,7 @@ socket_set_poll_add (DBusSocketSet *set,
int i;
for (i = 0; i < self->n_fds; i++)
_dbus_assert (self->fds[i].fd != fd);
_dbus_assert (!DBUS_POLLABLE_EQUALS (self->fds[i].fd, fd));
#endif
if (self->n_reserved >= self->n_allocated)
......@@ -142,8 +142,8 @@ socket_set_poll_add (DBusSocketSet *set,
self->n_allocated += REALLOC_INCREMENT;
}
_dbus_verbose ("before adding fd %d to %p, %d en/%d res/%d alloc\n",
fd, self, self->n_fds, self->n_reserved, self->n_allocated);
_dbus_verbose ("before adding fd %" DBUS_POLLABLE_FORMAT " to %p, %d en/%d res/%d alloc\n",
DBUS_POLLABLE_PRINTABLE (fd), self, self->n_fds, self->n_reserved, self->n_allocated);
_dbus_assert (self->n_reserved >= self->n_fds);
_dbus_assert (self->n_allocated > self->n_reserved);
......@@ -161,7 +161,7 @@ socket_set_poll_add (DBusSocketSet *set,
static void
socket_set_poll_enable (DBusSocketSet *set,
int fd,
DBusPollable fd,
unsigned int flags)
{
DBusSocketSetPoll *self = socket_set_poll_cast (set);
......@@ -169,7 +169,7 @@ socket_set_poll_enable (DBusSocketSet *set,
for (i = 0; i < self->n_fds; i++)
{
if (self->fds[i].fd == fd)
if (DBUS_POLLABLE_EQUALS (self->fds[i].fd, fd))
{
self->fds[i].events = watch_flags_to_poll_events (flags);
return;
......@@ -187,14 +187,14 @@ socket_set_poll_enable (DBusSocketSet *set,
static void
socket_set_poll_disable (DBusSocketSet *set,
int fd)
DBusPollable fd)
{
DBusSocketSetPoll *self = socket_set_poll_cast (set);
int i;
for (i = 0; i < self->n_fds; i++)
{
if (self->fds[i].fd == fd)
if (DBUS_POLLABLE_EQUALS (self->fds[i].fd, fd))
{
if (i != self->n_fds - 1)
{
......@@ -210,15 +210,15 @@ socket_set_poll_disable (DBusSocketSet *set,
static void
socket_set_poll_remove (DBusSocketSet *set,
int fd)
DBusPollable fd)
{
DBusSocketSetPoll *self = socket_set_poll_cast (set);
socket_set_poll_disable (set, fd);
self->n_reserved--;
_dbus_verbose ("after removing fd %d from %p, %d en/%d res/%d alloc\n",
fd, self, self->n_fds, self->n_reserved, self->n_allocated);
_dbus_verbose ("after removing fd %" DBUS_POLLABLE_FORMAT " from %p, %d en/%d res/%d alloc\n",
DBUS_POLLABLE_PRINTABLE (fd), self, self->n_fds, self->n_reserved, self->n_allocated);
_dbus_assert (self->n_fds <= self->n_reserved);
_dbus_assert (self->n_reserved <= self->n_allocated);
......
......@@ -29,9 +29,10 @@
#ifndef DOXYGEN_SHOULD_SKIP_THIS
#include <dbus/dbus.h>
#include <dbus/dbus-sysdeps.h>
typedef struct {
int fd;
DBusPollable fd;
unsigned int flags;
} DBusSocketEvent;
......@@ -41,16 +42,16 @@ typedef struct DBusSocketSetClass DBusSocketSetClass;
struct DBusSocketSetClass {
void (*free) (DBusSocketSet *self);
dbus_bool_t (*add) (DBusSocketSet *self,
int fd,
DBusPollable fd,
unsigned int flags,
dbus_bool_t enabled);
void (*remove) (DBusSocketSet *self,
int fd);
DBusPollable fd);
void (*enable) (DBusSocketSet *self,
int fd,
DBusPollable fd,
unsigned int flags);
void (*disable) (DBusSocketSet *self,
int fd);
DBusPollable fd);
int (*poll) (DBusSocketSet *self,
DBusSocketEvent *revents,
int max_events,
......@@ -71,7 +72,7 @@ _dbus_socket_set_free (DBusSocketSet *self)
static inline dbus_bool_t
_dbus_socket_set_add (DBusSocketSet *self,
int fd,
DBusPollable fd,
unsigned int flags,
dbus_bool_t enabled)
{
......@@ -80,14 +81,14 @@ _dbus_socket_set_add (DBusSocketSet *self,
static inline void
_dbus_socket_set_remove (DBusSocketSet *self,
int fd)
DBusPollable fd)
{
(self->cls->remove) (self, fd);
}
static inline void
_dbus_socket_set_enable (DBusSocketSet *self,
int fd,
DBusPollable fd,
unsigned int flags)
{
(self->cls->enable) (self, fd, flags);
......@@ -95,7 +96,7 @@ _dbus_socket_set_enable (DBusSocketSet *self,
static inline void
_dbus_socket_set_disable (DBusSocketSet *self,
int fd)
DBusPollable fd)
{
(self->cls->disable) (self, fd);
}
......
......@@ -40,7 +40,7 @@
struct DBusWatch
{
int refcount; /**< Reference count */
int fd; /**< File descriptor. */
DBusPollable fd; /**< File descriptor. */
unsigned int flags; /**< Conditions to watch. */
DBusWatchHandler handler; /**< Watch handler. */
......@@ -85,7 +85,7 @@ _dbus_watch_set_oom_last_time (DBusWatch *watch,
* @returns the new DBusWatch object.
*/
DBusWatch*
_dbus_watch_new (int fd,
_dbus_watch_new (DBusPollable fd,
unsigned int flags,
dbus_bool_t enabled,
DBusWatchHandler handler,
......@@ -143,7 +143,7 @@ _dbus_watch_unref (DBusWatch *watch)
watch->refcount -= 1;
if (watch->refcount == 0)
{
if (watch->fd != -1)
if (DBUS_POLLABLE_IS_VALID (watch->fd))
_dbus_warn ("this watch should have been invalidated");
dbus_watch_set_data (watch, NULL, NULL); /* call free_data_function */
......@@ -168,7 +168,7 @@ _dbus_watch_unref (DBusWatch *watch)
void
_dbus_watch_invalidate (DBusWatch *watch)
{
watch->fd = -1;
DBUS_POLLABLE_INVALIDATE (watch->fd);
watch->flags = 0;
}
......@@ -310,10 +310,13 @@ _dbus_watch_list_set_functions (DBusWatchList *watch_list,
{
DBusList *next = _dbus_list_get_next_link (&watch_list->watches,
link);
#ifdef DBUS_ENABLE_VERBOSE_MODE
DBusWatch *watch = link->data;
_dbus_verbose ("Adding a %s watch on fd %d using newly-set add watch function\n",
_dbus_verbose ("Adding a %s watch on fd %" DBUS_POLLABLE_FORMAT " using newly-set add watch function\n",
watch_flags_to_string (dbus_watch_get_flags (link->data)),
dbus_watch_get_socket (link->data));
DBUS_POLLABLE_PRINTABLE (watch->fd));
#endif
if (!(* add_function) (link->data, data))
{
......@@ -325,9 +328,12 @@ _dbus_watch_list_set_functions (DBusWatchList *watch_list,
{
DBusList *next = _dbus_list_get_next_link (&watch_list->watches,
link2);
#ifdef DBUS_ENABLE_VERBOSE_MODE
DBusWatch *watch2 = link2->data;
_dbus_verbose ("Removing watch on fd %d using newly-set remove function because initial add failed\n",
dbus_watch_get_socket (link2->data));
_dbus_verbose ("Removing watch on fd %" DBUS_POLLABLE_FORMAT " using newly-set remove function because initial add failed\n",
DBUS_POLLABLE_PRINTABLE (watch2->fd));
#endif
(* remove_function) (link2->data, data);
......@@ -383,8 +389,8 @@ _dbus_watch_list_add_watch (DBusWatchList *watch_list,
if (watch_list->add_watch_function != NULL)
{
_dbus_verbose ("Adding watch on fd %d\n",
dbus_watch_get_socket (watch));
_dbus_verbose ("Adding watch on fd %" DBUS_POLLABLE_FORMAT "\n",
DBUS_POLLABLE_PRINTABLE (watch->fd));
if (!(* watch_list->add_watch_function) (watch,
watch_list->watch_data))
......@@ -414,8 +420,8 @@ _dbus_watch_list_remove_watch (DBusWatchList *watch_list,
if (watch_list->remove_watch_function != NULL)
{
_dbus_verbose ("Removing watch on fd %d\n",
dbus_watch_get_socket (watch));
_dbus_verbose ("Removing watch on fd %" DBUS_POLLABLE_FORMAT "\n",
DBUS_POLLABLE_PRINTABLE (watch->fd));
(* watch_list->remove_watch_function) (watch,
watch_list->watch_data);
......@@ -446,8 +452,10 @@ _dbus_watch_list_toggle_watch (DBusWatchList *watch_list,
if (watch_list->watch_toggled_function != NULL)
{
_dbus_verbose ("Toggling watch %p on fd %d to %d\n",
watch, dbus_watch_get_socket (watch), watch->enabled);
_dbus_verbose ("Toggling watch %p on fd %" DBUS_POLLABLE_FORMAT " to %d\n",
watch,
DBUS_POLLABLE_PRINTABLE (watch->fd),
watch->enabled);
(* watch_list->watch_toggled_function) (watch,
watch_list->watch_data);
......@@ -598,6 +606,14 @@ _dbus_watch_get_socket (DBusWatch *watch)
return watch->fd;
}
DBusPollable
_dbus_watch_get_pollable (DBusWatch *watch)
{
_dbus_assert (watch != NULL);
return watch->fd;
}
/**
* Gets flags from DBusWatchFlags indicating
* what conditions should be monitored on the
......@@ -653,8 +669,8 @@ dbus_watch_set_data (DBusWatch *watch,
{
_dbus_return_if_fail (watch != NULL);
_dbus_verbose ("Setting watch fd %d data to data = %p function = %p from data = %p function = %p\n",
dbus_watch_get_socket (watch),
_dbus_verbose ("Setting watch fd %" DBUS_POLLABLE_FORMAT " data to data = %p function = %p from data = %p function = %p\n",
DBUS_POLLABLE_PRINTABLE (watch->fd),
data, free_data_function, watch->data, watch->free_data_function);
if (watch->free_data_function != NULL)
......@@ -709,21 +725,21 @@ dbus_watch_handle (DBusWatch *watch,
_dbus_return_val_if_fail (watch != NULL, FALSE);
#ifndef DBUS_DISABLE_CHECKS
if (watch->fd < 0 || watch->flags == 0)
if (!DBUS_POLLABLE_IS_VALID (watch->fd) || watch->flags == 0)
{
_dbus_warn_check_failed ("Watch is invalid, it should have been removed\n");
return TRUE;
}
#endif
_dbus_return_val_if_fail (watch->fd >= 0 /* fails if watch was removed */, TRUE);
_dbus_return_val_if_fail (DBUS_POLLABLE_IS_VALID (watch->fd) /* fails if watch was removed */, TRUE);
_dbus_watch_sanitize_condition (watch, &flags);
if (flags == 0)
{
_dbus_verbose ("After sanitization, watch flags on fd %d were 0\n",
watch->fd);
_dbus_verbose ("After sanitization, watch flags on fd %" DBUS_POLLABLE_FORMAT " were 0\n",
DBUS_POLLABLE_PRINTABLE (watch->fd));
return TRUE;
}
else
......
......@@ -45,7 +45,7 @@ typedef dbus_bool_t (* DBusWatchHandler) (DBusWatch *watch,
void *data);
DBUS_PRIVATE_EXPORT
DBusWatch* _dbus_watch_new (int fd,
DBusWatch* _dbus_watch_new (DBusPollable fd,
unsigned int flags,
dbus_bool_t enabled,
DBusWatchHandler handler,
......@@ -94,8 +94,10 @@ dbus_bool_t _dbus_watch_get_oom_last_time (DBusWatch *watch);
DBUS_PRIVATE_EXPORT
void _dbus_watch_set_oom_last_time (DBusWatch *watch,
dbus_bool_t oom);
DBUS_PRIVATE_EXPORT
DBusSocket _dbus_watch_get_socket (DBusWatch *watch);
DBUS_PRIVATE_EXPORT
DBusPollable _dbus_watch_get_pollable (DBusWatch *watch);
/** @} */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment