2006-03-09 23:14:13 +03:00
|
|
|
/*
|
2007-06-25 21:32:04 +04:00
|
|
|
* Copyright 2006-2007 Daniel Silverstone <dsilvers@digital-scurf.org>
|
2007-08-08 20:16:03 +04:00
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-03-09 23:14:13 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <stdlib.h>
|
2007-06-25 21:32:04 +04:00
|
|
|
#include <stdbool.h>
|
2006-03-09 23:14:13 +03:00
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
#include "utils/errors.h"
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
#include "utils/log.h"
|
2014-03-08 18:13:27 +04:00
|
|
|
|
2011-01-30 02:40:22 +03:00
|
|
|
#include "gtk/schedule.h"
|
2006-03-09 23:14:13 +03:00
|
|
|
|
2007-06-25 21:32:04 +04:00
|
|
|
|
|
|
|
/** Killable callback closure embodiment. */
|
2006-03-09 23:14:13 +03:00
|
|
|
typedef struct {
|
2019-06-08 15:33:16 +03:00
|
|
|
void (*callback)(void *); /**< The callback function. */
|
|
|
|
void *context; /**< The context for the callback. */
|
|
|
|
bool callback_killed; /**< Whether or not this was killed. */
|
2007-06-25 21:32:04 +04:00
|
|
|
} _nsgtk_callback_t;
|
2006-03-09 23:14:13 +03:00
|
|
|
|
2007-06-25 21:32:04 +04:00
|
|
|
/** 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;
|
2006-03-09 23:14:13 +03:00
|
|
|
|
2007-06-25 21:32:04 +04:00
|
|
|
static gboolean
|
|
|
|
nsgtk_schedule_generic_callback(gpointer data)
|
2006-03-09 23:14:13 +03:00
|
|
|
{
|
2007-06-25 21:32:04 +04:00
|
|
|
_nsgtk_callback_t *cb = (_nsgtk_callback_t *)(data);
|
|
|
|
if (cb->callback_killed) {
|
|
|
|
/* This callback instance has been killed. */
|
2017-09-07 17:30:08 +03:00
|
|
|
NSLOG(schedule, DEBUG, "CB at %p already dead.", cb);
|
2007-06-25 21:32:04 +04:00
|
|
|
}
|
|
|
|
queued_callbacks = g_list_remove(queued_callbacks, cb);
|
|
|
|
pending_callbacks = g_list_append(pending_callbacks, cb);
|
|
|
|
return FALSE;
|
2006-03-09 23:14:13 +03:00
|
|
|
}
|
|
|
|
|
2007-06-25 21:32:04 +04:00
|
|
|
static void
|
|
|
|
nsgtk_schedule_kill_callback(void *_target, void *_match)
|
2006-03-09 23:14:13 +03:00
|
|
|
{
|
2007-06-25 21:32:04 +04:00
|
|
|
_nsgtk_callback_t *target = (_nsgtk_callback_t *)_target;
|
|
|
|
_nsgtk_callback_t *match = (_nsgtk_callback_t *)_match;
|
|
|
|
if ((target->callback == match->callback) &&
|
|
|
|
(target->context == match->context)) {
|
2017-09-07 17:30:08 +03:00
|
|
|
NSLOG(schedule, DEBUG,
|
2019-06-08 15:33:16 +03:00
|
|
|
"Found match for %p(%p), killing.",
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
target->callback, target->context);
|
2007-06-25 21:32:04 +04:00
|
|
|
target->callback = NULL;
|
|
|
|
target->context = NULL;
|
|
|
|
target->callback_killed = true;
|
2019-06-08 15:33:16 +03:00
|
|
|
match->callback_killed = true;
|
2007-06-25 21:32:04 +04:00
|
|
|
}
|
2006-03-09 23:14:13 +03:00
|
|
|
}
|
|
|
|
|
2019-06-08 15:33:16 +03:00
|
|
|
/**
|
|
|
|
* remove a matching callback and context tuple from all lists
|
|
|
|
*
|
|
|
|
* \param callback The callback to match
|
|
|
|
* \param cbctx The callback context to match
|
|
|
|
* \return NSERROR_OK if the tuple was removed from at least one list else NSERROR_NOT_FOUND
|
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
schedule_remove(void (*callback)(void *p), void *cbctx)
|
2006-03-09 23:14:13 +03:00
|
|
|
{
|
2007-06-25 21:32:04 +04:00
|
|
|
_nsgtk_callback_t cb_match = {
|
|
|
|
.callback = callback,
|
2019-06-08 15:33:16 +03:00
|
|
|
.context = cbctx,
|
|
|
|
.callback_killed = false,
|
2007-06-25 21:32:04 +04:00
|
|
|
};
|
2007-08-08 20:16:03 +04:00
|
|
|
|
2007-06-25 21:32:04 +04:00
|
|
|
g_list_foreach(queued_callbacks,
|
|
|
|
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);
|
2019-06-08 15:33:16 +03:00
|
|
|
|
|
|
|
if (cb_match.callback_killed == false) {
|
|
|
|
return NSERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
return NSERROR_OK;
|
2006-03-09 23:14:13 +03:00
|
|
|
}
|
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
/* exported interface documented in gtk/schedule.h */
|
2019-06-08 15:33:16 +03:00
|
|
|
nserror nsgtk_schedule(int t, void (*callback)(void *p), void *cbctx)
|
2006-03-09 23:14:13 +03:00
|
|
|
{
|
2019-06-08 15:33:16 +03:00
|
|
|
_nsgtk_callback_t *cb;
|
|
|
|
nserror res;
|
2014-03-08 18:13:27 +04:00
|
|
|
|
2007-06-25 21:32:04 +04:00
|
|
|
/* Kill any pending schedule of this kind. */
|
2019-06-08 15:33:16 +03:00
|
|
|
res = schedule_remove(callback, cbctx);
|
2014-03-08 18:13:27 +04:00
|
|
|
|
2019-06-08 15:33:16 +03:00
|
|
|
/* only removal */
|
|
|
|
if (t < 0) {
|
|
|
|
return res;
|
|
|
|
}
|
2014-03-08 18:13:27 +04:00
|
|
|
|
2019-06-08 15:33:16 +03:00
|
|
|
cb = malloc(sizeof(_nsgtk_callback_t));
|
2007-06-25 21:32:04 +04:00
|
|
|
cb->callback = callback;
|
2019-06-08 15:33:16 +03:00
|
|
|
cb->context = cbctx;
|
2010-04-14 13:59:23 +04:00
|
|
|
cb->callback_killed = false;
|
2007-06-25 21:32:04 +04:00
|
|
|
/* Prepend is faster right now. */
|
|
|
|
queued_callbacks = g_list_prepend(queued_callbacks, cb);
|
2014-03-08 18:13:27 +04:00
|
|
|
g_timeout_add(t, nsgtk_schedule_generic_callback, cb);
|
|
|
|
|
2019-06-08 15:33:16 +03:00
|
|
|
return NSERROR_OK;
|
2006-03-09 23:14:13 +03:00
|
|
|
}
|
|
|
|
|
2009-02-12 01:33:55 +03:00
|
|
|
bool
|
2007-06-25 21:32:04 +04:00
|
|
|
schedule_run(void)
|
|
|
|
{
|
|
|
|
/* Capture this run of pending callbacks into the list. */
|
|
|
|
this_run = pending_callbacks;
|
2007-08-08 20:16:03 +04:00
|
|
|
|
2007-06-25 21:32:04 +04:00
|
|
|
if (this_run == NULL)
|
2009-02-12 01:33:55 +03:00
|
|
|
return false; /* Nothing to do */
|
2007-08-08 20:16:03 +04:00
|
|
|
|
2007-06-25 21:32:04 +04:00
|
|
|
/* Clear the pending list. */
|
|
|
|
pending_callbacks = NULL;
|
2007-08-08 20:16:03 +04:00
|
|
|
|
2017-09-07 17:30:08 +03:00
|
|
|
NSLOG(schedule, DEBUG,
|
2019-06-08 15:33:16 +03:00
|
|
|
"Captured a run of %d callbacks to fire.",
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
g_list_length(this_run));
|
2007-08-08 20:16:03 +04:00
|
|
|
|
2007-06-25 21:32:04 +04:00
|
|
|
/* 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);
|
|
|
|
}
|
2019-06-08 15:33:16 +03:00
|
|
|
return true;
|
2007-06-25 21:32:04 +04:00
|
|
|
}
|