main: Clean up main loop integration

Remove do/while loops in favour of for loops, and downgrade some
warnings and messages statements to debug.
This commit is contained in:
Bastien Nocera
2018-05-25 15:45:02 +02:00
parent 78ffe68426
commit 4234902671

View File

@ -63,18 +63,19 @@ static gboolean source_prepare(GSource *source, gint *timeout)
static gboolean source_check(GSource *source) static gboolean source_check(GSource *source)
{ {
struct fdsource *_fdsource = (struct fdsource *) source; struct fdsource *_fdsource = (struct fdsource *) source;
GSList *elem = _fdsource->pollfds; GSList *l;
struct timeval tv; struct timeval tv;
int r; int r;
if (!elem) if (!_fdsource->pollfds)
return FALSE; return FALSE;
do { for (l = _fdsource->pollfds; l != NULL; l = l->next) {
GPollFD *pollfd = elem->data; GPollFD *pollfd = l->data;
if (pollfd->revents) if (pollfd->revents)
return TRUE; return TRUE;
} while ((elem = g_slist_next(elem))); }
r = fp_get_next_timeout(&tv); r = fp_get_next_timeout(&tv);
if (r == 1 && !timerisset(&tv)) if (r == 1 && !timerisset(&tv))
@ -101,15 +102,18 @@ static gboolean source_dispatch(GSource *source, GSourceFunc callback,
static void source_finalize(GSource *source) static void source_finalize(GSource *source)
{ {
struct fdsource *_fdsource = (struct fdsource *) source; struct fdsource *_fdsource = (struct fdsource *) source;
GSList *elem = _fdsource->pollfds; GSList *l;
if (elem) if (!_fdsource->pollfds)
do { return;
GPollFD *pollfd = elem->data;
g_source_remove_poll((GSource *) _fdsource, pollfd); for (l = _fdsource->pollfds; l != NULL; l = l->next) {
g_slice_free(GPollFD, pollfd); GPollFD *pollfd = l->data;
_fdsource->pollfds = g_slist_delete_link(_fdsource->pollfds, elem);
} while ((elem = g_slist_next(elem))); g_source_remove_poll((GSource *) _fdsource, pollfd);
g_slice_free(GPollFD, pollfd);
_fdsource->pollfds = g_slist_delete_link(_fdsource->pollfds, l);
}
g_slist_free(_fdsource->pollfds); g_slist_free(_fdsource->pollfds);
} }
@ -125,7 +129,9 @@ static struct fdsource *fdsource = NULL;
static void pollfd_add(int fd, short events) static void pollfd_add(int fd, short events)
{ {
GPollFD *pollfd = g_slice_new(GPollFD); GPollFD *pollfd;
pollfd = g_slice_new(GPollFD);
pollfd->fd = fd; pollfd->fd = fd;
pollfd->events = 0; pollfd->events = 0;
pollfd->revents = 0; pollfd->revents = 0;
@ -140,30 +146,32 @@ static void pollfd_add(int fd, short events)
static void pollfd_added_cb(int fd, short events) static void pollfd_added_cb(int fd, short events)
{ {
g_message("now monitoring fd %d", fd); g_debug("now monitoring fd %d", fd);
pollfd_add(fd, events); pollfd_add(fd, events);
} }
static void pollfd_removed_cb(int fd) static void pollfd_removed_cb(int fd)
{ {
GSList *elem = fdsource->pollfds; GSList *l;
g_message("no longer monitoring fd %d", fd);
if (!elem) { g_debug("no longer monitoring fd %d", fd);
g_warning("cannot remove from list as list is empty?");
if (!fdsource->pollfds) {
g_debug("cannot remove from list as list is empty?");
return; return;
} }
do { for (l = fdsource->pollfds; l != NULL; l = l->next) {
GPollFD *pollfd = elem->data; GPollFD *pollfd = l->data;
if (pollfd->fd != fd) if (pollfd->fd != fd)
continue; continue;
g_source_remove_poll((GSource *) fdsource, pollfd); g_source_remove_poll((GSource *) fdsource, pollfd);
g_slice_free(GPollFD, pollfd); g_slice_free(GPollFD, pollfd);
fdsource->pollfds = g_slist_delete_link(fdsource->pollfds, elem); fdsource->pollfds = g_slist_delete_link(fdsource->pollfds, l);
return; return;
} while ((elem = g_slist_next(elem))); }
g_error("couldn't find fd %d in list\n", fd); g_error("couldn't find fd %d in list\n", fd);
} }
@ -173,8 +181,9 @@ static int setup_pollfds(void)
size_t numfds; size_t numfds;
size_t i; size_t i;
struct fp_pollfd *fpfds; struct fp_pollfd *fpfds;
GSource *gsource = g_source_new(&sourcefuncs, sizeof(struct fdsource)); GSource *gsource;
gsource = g_source_new(&sourcefuncs, sizeof(struct fdsource));
fdsource = (struct fdsource *) gsource; fdsource = (struct fdsource *) gsource;
fdsource->pollfds = NULL; fdsource->pollfds = NULL;