Make the gtk_schedule stuff more robust, only run schedules inside gui_poll and generally cause less issues for the as-yet non-reentrant core.

svn path=/trunk/netsurf/; revision=3366
This commit is contained in:
Daniel Silverstone 2007-06-25 17:32:04 +00:00
parent 1a67793784
commit dbdd24ae58
2 changed files with 96 additions and 39 deletions

View File

@ -308,6 +308,7 @@ void gui_poll(bool active)
g_main_context_remove_poll(0, fd_list[i]); g_main_context_remove_poll(0, fd_list[i]);
free(fd_list[i]); free(fd_list[i]);
} }
schedule_run();
} }

View File

@ -2,64 +2,120 @@
* This file is part of NetSurf, http://netsurf-browser.org/ * This file is part of NetSurf, http://netsurf-browser.org/
* Licensed under the GNU General Public License, * Licensed under the GNU General Public License,
* http://www.opensource.org/licenses/gpl-license * http://www.opensource.org/licenses/gpl-license
* Copyright 2006 Daniel Silverstone <dsilvers@digital-scurf.org> * Copyright 2006-2007 Daniel Silverstone <dsilvers@digital-scurf.org>
*/ */
#include <glib.h> #include <glib.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#include "desktop/browser.h" #include "desktop/browser.h"
#ifdef DEBUG_GTK_SCHEDULE
#include "utils/log.h"
#else
#define LOG(X)
#endif
/** Killable callback closure embodiment. */
typedef struct { typedef struct {
void (*callback)(void *); void (*callback)(void *); /**< The callback function. */
void *p; void *context; /**< The context for the callback. */
int die; bool callback_killed; /**< Whether or not this was killed. */
} _nsgtkcallback; bool callback_fired; /**< Whether or not this has fired yet. */
} _nsgtk_callback_t;
static GList *callbacks; /** List of callbacks which have occurred and are pending running. */
static GList *pending_callbacks = NULL;
/** List of callbacks which are queued to occur in the future. */
static GList *queued_callbacks = NULL;
/** List of callbacks which are about to be run in this ::schedule_run. */
static GList *this_run = NULL;
static gboolean ns_generic_gtk_callback(gpointer data) static gboolean
nsgtk_schedule_generic_callback(gpointer data)
{ {
_nsgtkcallback *cb = (_nsgtkcallback*)(data); _nsgtk_callback_t *cb = (_nsgtk_callback_t *)(data);
if(cb->die) { if (cb->callback_killed) {
/* We got removed before we got fired off */ /* This callback instance has been killed. */
free(cb); LOG(("CB at %p already dead.", cb));
return FALSE; free(cb);
} return FALSE;
cb->callback(cb->p); }
callbacks = g_list_remove(callbacks, cb); LOG(("CB for %p(%p) set pending.", cb->callback, cb->context));
free(cb); /* The callback is alive, so move it to pending. */
return FALSE; cb->callback_fired = true;
queued_callbacks = g_list_remove(queued_callbacks, cb);
pending_callbacks = g_list_append(pending_callbacks, cb);
return FALSE;
} }
void schedule_remove(void (*callback)(void *p), void *p) static void
nsgtk_schedule_kill_callback(void *_target, void *_match)
{ {
_nsgtkcallback *cb; _nsgtk_callback_t *target = (_nsgtk_callback_t *)_target;
GList *l; _nsgtk_callback_t *match = (_nsgtk_callback_t *)_match;
l = callbacks; if ((target->callback == match->callback) &&
while(l) { (target->context == match->context)) {
cb = (_nsgtkcallback*)(l->data); LOG(("Found match for %p(%p), killing.",
if(cb->callback == callback && cb->p == p) { target->callback, target->context));
l = callbacks = g_list_remove(callbacks, cb); target->callback = NULL;
cb->die = 1; target->context = NULL;
} else target->callback_killed = true;
l = g_list_next(l); }
}
} }
void schedule(int t, void (*callback)(void *p), void *p) void
schedule_remove(void (*callback)(void *p), void *p)
{ {
_nsgtkcallback *cb = (_nsgtkcallback*)malloc(sizeof(_nsgtkcallback)); _nsgtk_callback_t cb_match = {
schedule_remove(callback, p); .callback = callback,
cb->callback = callback; .context = p,
cb->p = p; };
cb->die = 0;
callbacks = g_list_prepend(callbacks, cb); g_list_foreach(queued_callbacks,
g_timeout_add(t * 10, ns_generic_gtk_callback, cb); nsgtk_schedule_kill_callback, &cb_match);
g_list_foreach(pending_callbacks,
nsgtk_schedule_kill_callback, &cb_match);
g_list_foreach(this_run,
nsgtk_schedule_kill_callback, &cb_match);
} }
void schedule_run(void) void
schedule(int t, void (*callback)(void *p), void *p)
{ {
/* Nothing to do, the running is done via the gtk mainloop of joy */ const int msec_timeout = t * 100;
_nsgtk_callback_t *cb = malloc(sizeof(_nsgtk_callback_t));
/* Kill any pending schedule of this kind. */
schedule_remove(callback, p);
cb->callback = callback;
cb->context = p;
cb->callback_killed = cb->callback_fired = false;
/* Prepend is faster right now. */
queued_callbacks = g_list_prepend(queued_callbacks, cb);
g_timeout_add(msec_timeout, nsgtk_schedule_generic_callback, cb);
} }
void
schedule_run(void)
{
/* Capture this run of pending callbacks into the list. */
this_run = pending_callbacks;
if (this_run == NULL)
return; /* Nothing to do */
/* Clear the pending list. */
pending_callbacks = NULL;
LOG(("Captured a run of %d callbacks to fire.", g_list_length(this_run)));
/* Run all the callbacks which made it this far. */
while (this_run != NULL) {
_nsgtk_callback_t *cb = (_nsgtk_callback_t *)(this_run->data);
this_run = g_list_remove(this_run, this_run->data);
if (!cb->callback_killed)
cb->callback(cb->context);
free(cb);
}
}