diff options
Diffstat (limited to 'frontends/kolibrios/schedule.c')
-rw-r--r-- | frontends/kolibrios/schedule.c | 214 |
1 files changed, 214 insertions, 0 deletions
diff --git a/frontends/kolibrios/schedule.c b/frontends/kolibrios/schedule.c new file mode 100644 index 000000000..0426478fb --- /dev/null +++ b/frontends/kolibrios/schedule.c @@ -0,0 +1,214 @@ +/* + * Copyright 2008 Vincent Sanders <vince@simtec.co.uk> + * + * 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/>. + */ + +#include <time.h> +#include <stdlib.h> + +#include "utils/sys_time.h" +#include "utils/log.h" +#include <kos32sys.h> +#include "framebuffer/schedule.h" + +#ifdef DEBUG_SCHEDULER +#define SRLOG(x...) LOG(x) +#else +#define SRLOG(x...) ((void) 0) +#endif + +/* linked list of scheduled callbacks */ +static struct nscallback *schedule_list = NULL; + +/** + * scheduled callback. + */ +struct nscallback +{ + struct nscallback *next; + uint32_t tv; + void (*callback)(void *p); + void *p; +}; + +/** + * Unschedule a callback. + * + * \param callback callback function + * \param p user parameter, passed to callback function + * + * All scheduled callbacks matching both callback and p are removed. + */ +static nserror schedule_remove(void (*callback)(void *p), void *p) +{ + struct nscallback *cur_nscb; + struct nscallback *prev_nscb; + struct nscallback *unlnk_nscb; + + /* check there is something on the list to remove */ + if (schedule_list == NULL) { + return NSERROR_OK; + } + + SRLOG("removing %p, %p", callback, p); + + cur_nscb = schedule_list; + prev_nscb = NULL; + + while (cur_nscb != NULL) { + if ((cur_nscb->callback == callback) && + (cur_nscb->p == p)) { + /* item to remove */ + + SRLOG("callback entry %p removing %p(%p)", + cur_nscb, cur_nscb->callback, cur_nscb->p); + + /* remove callback */ + unlnk_nscb = cur_nscb; + cur_nscb = unlnk_nscb->next; + + if (prev_nscb == NULL) { + schedule_list = cur_nscb; + } else { + prev_nscb->next = cur_nscb; + } + free (unlnk_nscb); + } else { + /* move to next element */ + prev_nscb = cur_nscb; + cur_nscb = prev_nscb->next; + } + } + + return NSERROR_OK; +} + +/* exported function documented in framebuffer/schedule.h */ +nserror framebuffer_schedule(int tival, void (*callback)(void *p), void *p) +{ + struct nscallback *nscb; + nserror ret; + + /* ensure uniqueness of the callback and context */ + ret = schedule_remove(callback, p); + if ((tival < 0) || (ret != NSERROR_OK)) { + return ret; + } + + LOG("Adding %p(%p) in %d", callback, p, tival); + + nscb = calloc(1, sizeof(struct nscallback)); + nscb->tv = get_tick_count() + tival / 10; + nscb->callback = callback; + nscb->p = p; + + /* add to list front */ + nscb->next = schedule_list; + schedule_list = nscb; + + return NSERROR_OK; +} + +/* exported function documented in framebuffer/schedule.h */ +int schedule_run(void) +{ + uint32_t tv; + uint32_t nexttime; + uint32_t rettime; + struct nscallback *cur_nscb; + struct nscallback *prev_nscb; + struct nscallback *unlnk_nscb; + + if (schedule_list == NULL) + return -1; + + /* reset enumeration to the start of the list */ + cur_nscb = schedule_list; + prev_nscb = NULL; + nexttime = cur_nscb->tv; + + tv = get_tick_count(); + + while (cur_nscb != NULL) { + if (tv > cur_nscb->tv) { + /* scheduled time */ + + /* remove callback */ + unlnk_nscb = cur_nscb; + + if (prev_nscb == NULL) { + schedule_list = unlnk_nscb->next; + } else { + prev_nscb->next = unlnk_nscb->next; + } + + unlnk_nscb->callback(unlnk_nscb->p); + + free(unlnk_nscb); + + /* need to deal with callback modifying the list. */ + if (schedule_list == NULL) + return -1; /* no more callbacks scheduled */ + + /* reset enumeration to the start of the list */ + cur_nscb = schedule_list; + prev_nscb = NULL; + nexttime = cur_nscb->tv; + } else { + /* if the time to the event is sooner than the + * currently recorded soonest event record it + */ + if (nexttime > cur_nscb->tv) { + nexttime = cur_nscb->tv; + } + /* move to next element */ + prev_nscb = cur_nscb; + cur_nscb = prev_nscb->next; + } + } + + /* make rettime relative to now */ + rettime = tv - nexttime; + + /* SRLOG("returning time to next event as %ldms",(rettime.tv_sec * 1000) + (rettime.tv_usec / 1000)); */ + + /* return next event time in milliseconds (24days max wait) */ + return rettime * 10; +} + +void list_schedule(void) +{ + uint32_t tv; + struct nscallback *cur_nscb; + + tv = get_tick_count(); + + LOG("schedule list at %u", tv); + + cur_nscb = schedule_list; + + while (cur_nscb != NULL) { + LOG("Schedule %p at %ld", cur_nscb, cur_nscb->tv); + cur_nscb = cur_nscb->next; + } +} + + +/* + * Local Variables: + * c-basic-offset:8 + * End: + */ |