2004-03-21 15:46:56 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2004 James Bursa <bursa@users.sourceforge.net>
|
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/>.
|
2004-03-21 15:46:56 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* Scheduled callback queue (implementation).
|
|
|
|
*
|
|
|
|
* The queue is simply implemented as a linked list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
2014-03-08 18:13:27 +04:00
|
|
|
|
2004-03-21 15:46:56 +03:00
|
|
|
#include "oslib/os.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
#include "utils/log.h"
|
2014-03-08 18:13:27 +04:00
|
|
|
|
|
|
|
#include "riscos/gui.h"
|
2004-03-21 15:46:56 +03:00
|
|
|
|
|
|
|
|
|
|
|
/** Entry in the queue of scheduled callbacks. */
|
|
|
|
struct sched_entry {
|
|
|
|
/** Preferred time for callback. */
|
|
|
|
os_t time;
|
|
|
|
/** Function to call at the specified time. */
|
|
|
|
void (*callback)(void *p);
|
|
|
|
/** User parameter for callback. */
|
|
|
|
void *p;
|
|
|
|
/** Next (later) entry in queue. */
|
|
|
|
struct sched_entry *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Queue of scheduled callbacks (sentinel at head). */
|
|
|
|
static struct sched_entry sched_queue = { 0, 0, 0, 0 };
|
|
|
|
|
|
|
|
/** Items have been scheduled. */
|
|
|
|
bool sched_active = false;
|
|
|
|
/** Time of soonest scheduled event (valid only if sched_active is true). */
|
|
|
|
os_t sched_time;
|
|
|
|
|
|
|
|
/**
|
2014-03-08 18:13:27 +04:00
|
|
|
* Unschedule a callback.
|
2004-03-21 15:46:56 +03:00
|
|
|
*
|
|
|
|
* \param callback callback function
|
|
|
|
* \param p user parameter, passed to callback function
|
|
|
|
*
|
2014-03-08 18:13:27 +04:00
|
|
|
* All scheduled callbacks matching both callback and p are removed.
|
2004-03-21 15:46:56 +03:00
|
|
|
*/
|
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
static nserror schedule_remove(void (*callback)(void *p), void *p)
|
|
|
|
{
|
|
|
|
struct sched_entry *entry, *next;
|
|
|
|
|
|
|
|
for (entry = &sched_queue; entry->next; entry = entry->next) {
|
|
|
|
if (entry->next->callback != callback || entry->next->p != p)
|
|
|
|
continue;
|
|
|
|
next = entry->next;
|
|
|
|
entry->next = entry->next->next;
|
|
|
|
free(next);
|
|
|
|
if (!entry->next)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sched_queue.next) {
|
|
|
|
sched_active = true;
|
|
|
|
sched_time = sched_queue.next->time;
|
|
|
|
} else {
|
|
|
|
sched_active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* exported function documented in riscos/gui.h */
|
|
|
|
nserror riscos_schedule(int t, void (*callback)(void *p), void *p)
|
2004-03-21 15:46:56 +03:00
|
|
|
{
|
|
|
|
struct sched_entry *entry;
|
|
|
|
struct sched_entry *queue;
|
|
|
|
os_t time;
|
2014-03-08 18:13:27 +04:00
|
|
|
nserror ret;
|
2006-11-27 18:35:18 +03:00
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
ret = schedule_remove(callback, p);
|
|
|
|
if ((t < 0) || (ret != NSERROR_OK)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
t = t / 10; /* convert to centiseconds */
|
2004-03-21 15:46:56 +03:00
|
|
|
|
|
|
|
time = os_read_monotonic_time() + t;
|
|
|
|
|
|
|
|
entry = malloc(sizeof *entry);
|
|
|
|
if (!entry) {
|
|
|
|
LOG(("malloc failed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->time = time;
|
|
|
|
entry->callback = callback;
|
|
|
|
entry->p = p;
|
|
|
|
|
|
|
|
for (queue = &sched_queue;
|
|
|
|
queue->next && queue->next->time <= time;
|
|
|
|
queue = queue->next)
|
|
|
|
;
|
|
|
|
entry->next = queue->next;
|
|
|
|
queue->next = entry;
|
|
|
|
|
|
|
|
sched_active = true;
|
|
|
|
sched_time = sched_queue.next->time;
|
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
return NSERROR_OK;
|
2004-03-21 15:46:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
/* exported function documented in riscos/gui.h */
|
2009-02-12 01:33:55 +03:00
|
|
|
bool schedule_run(void)
|
2004-03-21 15:46:56 +03:00
|
|
|
{
|
|
|
|
struct sched_entry *entry;
|
2004-07-09 14:58:45 +04:00
|
|
|
void (*callback)(void *p);
|
|
|
|
void *p;
|
2004-03-21 15:46:56 +03:00
|
|
|
os_t now;
|
|
|
|
|
|
|
|
now = os_read_monotonic_time();
|
|
|
|
|
|
|
|
while (sched_queue.next && sched_queue.next->time <= now) {
|
|
|
|
entry = sched_queue.next;
|
2004-07-09 14:58:45 +04:00
|
|
|
callback = entry->callback;
|
|
|
|
p = entry->p;
|
2004-03-21 15:46:56 +03:00
|
|
|
sched_queue.next = entry->next;
|
|
|
|
free(entry);
|
2014-03-08 18:13:27 +04:00
|
|
|
/* The callback may call riscos_schedule(), so leave
|
|
|
|
* the queue in a safe state.
|
|
|
|
*/
|
2004-07-09 14:58:45 +04:00
|
|
|
callback(p);
|
2004-03-21 15:46:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sched_queue.next) {
|
|
|
|
sched_active = true;
|
|
|
|
sched_time = sched_queue.next->time;
|
|
|
|
} else
|
|
|
|
sched_active = false;
|
2009-02-12 01:33:55 +03:00
|
|
|
|
|
|
|
return sched_active;
|
2004-03-21 15:46:56 +03:00
|
|
|
}
|