2010-01-31 03:36:02 +03:00
|
|
|
/*
|
|
|
|
* 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 <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "utils/log.h"
|
2011-02-09 02:42:18 +03:00
|
|
|
#include "utils/utils.h"
|
2014-03-09 20:14:05 +04:00
|
|
|
#include "utils/errors.h"
|
|
|
|
|
|
|
|
#include "windows/schedule.h"
|
2010-01-31 03:36:02 +03:00
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
#ifdef DEBUG_SCHEDULER
|
|
|
|
#define SRLOG(x) LOG(x)
|
|
|
|
#else
|
|
|
|
#define SRLOG(x)
|
|
|
|
#endif
|
|
|
|
|
2010-01-31 03:36:02 +03:00
|
|
|
/* linked list of scheduled callbacks */
|
|
|
|
static struct nscallback *schedule_list = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scheduled callback.
|
|
|
|
*/
|
|
|
|
struct nscallback
|
|
|
|
{
|
2011-02-09 02:42:18 +03:00
|
|
|
struct nscallback *next;
|
2010-01-31 03:36:02 +03:00
|
|
|
struct timeval tv;
|
|
|
|
void (*callback)(void *p);
|
|
|
|
void *p;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unschedule a callback.
|
|
|
|
*
|
|
|
|
* \param callback callback function
|
2011-02-09 02:42:18 +03:00
|
|
|
* \param p user parameter, passed to callback function
|
2010-01-31 03:36:02 +03:00
|
|
|
*
|
|
|
|
* All scheduled callbacks matching both callback and p are removed.
|
|
|
|
*/
|
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
static nserror schedule_remove(void (*callback)(void *p), void *p)
|
2010-01-31 03:36:02 +03:00
|
|
|
{
|
2011-02-09 02:42:18 +03:00
|
|
|
struct nscallback *cur_nscb;
|
|
|
|
struct nscallback *prev_nscb;
|
|
|
|
struct nscallback *unlnk_nscb;
|
2010-01-31 03:36:02 +03:00
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
/* check there is something on the list to remove */
|
|
|
|
if (schedule_list == NULL) {
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2010-01-31 03:36:02 +03:00
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
SRLOG(("removing %p, %p", callback, p));
|
2010-01-31 03:36:02 +03:00
|
|
|
|
2011-02-09 02:42:18 +03:00
|
|
|
cur_nscb = schedule_list;
|
|
|
|
prev_nscb = NULL;
|
|
|
|
|
|
|
|
while (cur_nscb != NULL) {
|
|
|
|
if ((cur_nscb->callback == callback) &&
|
|
|
|
(cur_nscb->p == p)) {
|
|
|
|
/* item to remove */
|
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
SRLOG(("callback entry %p removing %p(%p)",
|
2011-02-09 02:42:18 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 18:13:27 +04:00
|
|
|
return NSERROR_OK;
|
2010-01-31 03:36:02 +03:00
|
|
|
}
|
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
/* exported interface documented in windows/schedule.h */
|
|
|
|
nserror win32_schedule(int ival, void (*callback)(void *p), void *p)
|
|
|
|
{
|
|
|
|
struct nscallback *nscb;
|
|
|
|
struct timeval tv;
|
|
|
|
nserror ret;
|
|
|
|
|
|
|
|
ret = schedule_remove(callback, p);
|
|
|
|
if ((ival < 0) || (ret != NSERROR_OK)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
tv.tv_sec = ival / 1000; /* miliseconds to seconds */
|
2014-03-09 20:14:05 +04:00
|
|
|
tv.tv_usec = (ival % 1000) * 1000; /* remainder to microseconds */
|
2014-03-08 18:13:27 +04:00
|
|
|
|
|
|
|
nscb = calloc(1, sizeof(struct nscallback));
|
|
|
|
if (nscb == NULL) {
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
SRLOG(("adding callback %p for %p(%p) at %d cs",
|
|
|
|
nscb, callback, p, ival));
|
|
|
|
|
|
|
|
gettimeofday(&nscb->tv, NULL);
|
|
|
|
timeradd(&nscb->tv, &tv, &nscb->tv);
|
|
|
|
|
|
|
|
nscb->callback = callback;
|
|
|
|
nscb->p = p;
|
|
|
|
|
|
|
|
/* add to list front */
|
|
|
|
nscb->next = schedule_list;
|
|
|
|
schedule_list = nscb;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2011-02-15 14:55:09 +03:00
|
|
|
|
|
|
|
/* exported interface documented in schedule.h */
|
2011-02-09 02:42:18 +03:00
|
|
|
int
|
|
|
|
schedule_run(void)
|
2010-01-31 03:36:02 +03:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
2011-02-09 02:42:18 +03:00
|
|
|
struct timeval nexttime;
|
|
|
|
struct timeval rettime;
|
|
|
|
struct nscallback *cur_nscb;
|
|
|
|
struct nscallback *prev_nscb;
|
|
|
|
struct nscallback *unlnk_nscb;
|
2010-01-31 03:36:02 +03:00
|
|
|
|
2011-02-09 02:42:18 +03:00
|
|
|
if (schedule_list == NULL)
|
|
|
|
return -1;
|
2010-01-31 03:36:02 +03:00
|
|
|
|
2011-02-09 02:42:18 +03:00
|
|
|
/* reset enumeration to the start of the list */
|
|
|
|
cur_nscb = schedule_list;
|
|
|
|
prev_nscb = NULL;
|
|
|
|
nexttime = cur_nscb->tv;
|
2010-01-31 03:36:02 +03:00
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
|
2011-02-09 02:42:18 +03:00
|
|
|
while (cur_nscb != NULL) {
|
|
|
|
if (timercmp(&tv, &cur_nscb->tv, >)) {
|
|
|
|
/* scheduled time */
|
2010-01-31 03:36:02 +03:00
|
|
|
|
2011-02-09 02:42:18 +03:00
|
|
|
/* remove callback */
|
|
|
|
unlnk_nscb = cur_nscb;
|
2010-01-31 03:36:02 +03:00
|
|
|
|
2011-02-09 02:42:18 +03:00
|
|
|
if (prev_nscb == NULL) {
|
|
|
|
schedule_list = unlnk_nscb->next;
|
|
|
|
} else {
|
|
|
|
prev_nscb->next = unlnk_nscb->next;
|
|
|
|
}
|
2010-01-31 03:36:02 +03:00
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
SRLOG(("callback entry %p running %p(%p)",
|
2011-02-09 02:42:18 +03:00
|
|
|
unlnk_nscb, unlnk_nscb->callback, unlnk_nscb->p));
|
|
|
|
/* call callback */
|
|
|
|
unlnk_nscb->callback(unlnk_nscb->p);
|
2010-01-31 03:36:02 +03:00
|
|
|
|
2011-02-09 02:42:18 +03:00
|
|
|
free(unlnk_nscb);
|
|
|
|
|
2011-02-15 14:55:09 +03:00
|
|
|
/* dispatched events can modify the list,
|
|
|
|
* instead of locking we simply reset list
|
|
|
|
* enumeration to the start.
|
|
|
|
*/
|
2011-02-09 02:42:18 +03:00
|
|
|
if (schedule_list == NULL)
|
|
|
|
return -1; /* no more callbacks scheduled */
|
|
|
|
|
|
|
|
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
|
2010-01-31 03:36:02 +03:00
|
|
|
*/
|
2011-02-09 02:42:18 +03:00
|
|
|
if (timercmp(&nexttime, &cur_nscb->tv, >)) {
|
|
|
|
nexttime = cur_nscb->tv;
|
|
|
|
}
|
|
|
|
/* move to next element */
|
|
|
|
prev_nscb = cur_nscb;
|
|
|
|
cur_nscb = prev_nscb->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-15 14:55:09 +03:00
|
|
|
/* make returned time relative to now */
|
2011-02-09 02:42:18 +03:00
|
|
|
timersub(&nexttime, &tv, &rettime);
|
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
SRLOG(("returning time to next event as %ldms",
|
|
|
|
(rettime.tv_sec * 1000) + (rettime.tv_usec / 1000)));
|
|
|
|
|
2011-02-09 02:42:18 +03:00
|
|
|
/* return next event time in milliseconds (24days max wait) */
|
|
|
|
return (rettime.tv_sec * 1000) + (rettime.tv_usec / 1000);
|
2010-01-31 03:36:02 +03:00
|
|
|
}
|
|
|
|
|
2011-02-15 14:55:09 +03:00
|
|
|
/* exported interface documented in schedule.h */
|
2010-01-31 03:36:02 +03:00
|
|
|
void list_schedule(void)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
2011-02-09 02:42:18 +03:00
|
|
|
struct nscallback *cur_nscb;
|
2010-01-31 03:36:02 +03:00
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
|
2011-02-09 02:42:18 +03:00
|
|
|
LOG(("schedule list at %ld:%ld", tv.tv_sec, tv.tv_usec));
|
2010-01-31 03:36:02 +03:00
|
|
|
|
2011-02-09 02:42:18 +03:00
|
|
|
cur_nscb = schedule_list;
|
2010-01-31 03:36:02 +03:00
|
|
|
|
2011-02-09 02:42:18 +03:00
|
|
|
while (cur_nscb != NULL) {
|
|
|
|
LOG(("Schedule %p at %ld:%ld",
|
|
|
|
cur_nscb, cur_nscb->tv.tv_sec, cur_nscb->tv.tv_usec));
|
|
|
|
cur_nscb = cur_nscb->next;
|
|
|
|
}
|
2010-01-31 03:36:02 +03:00
|
|
|
}
|
2011-02-09 02:42:18 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local Variables:
|
|
|
|
* c-basic-offset:8
|
|
|
|
* End:
|
|
|
|
*/
|