2011-01-06 00:02:22 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Vincent Sanders <vince@simtec.co.uk>
|
2011-04-22 14:42:53 +04:00
|
|
|
* Copyright 2011 Ole Loots <ole@monochrom.net>
|
2011-01-06 00:02:22 +03: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/>.
|
|
|
|
*/
|
|
|
|
|
2011-03-21 01:43:07 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-01-06 00:02:22 +03:00
|
|
|
#include <time.h>
|
|
|
|
|
2016-04-19 11:16:09 +03:00
|
|
|
#include "utils/sys_time.h"
|
2014-03-09 20:14:05 +04:00
|
|
|
#include "utils/errors.h"
|
2017-09-07 17:30:08 +03:00
|
|
|
#include "utils/log.h"
|
2014-03-09 20:14:05 +04:00
|
|
|
|
2011-01-06 00:02:22 +03:00
|
|
|
#include "atari/schedule.h"
|
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
#define MS_NOW() ((clock() * 1000) / CLOCKS_PER_SEC)
|
2011-04-22 14:42:53 +04:00
|
|
|
|
2011-01-06 00:02:22 +03:00
|
|
|
/* linked list of scheduled callbacks */
|
|
|
|
static struct nscallback *schedule_list = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scheduled callback.
|
|
|
|
*/
|
|
|
|
struct nscallback
|
|
|
|
{
|
2011-04-22 14:42:53 +04:00
|
|
|
struct nscallback *next;
|
|
|
|
unsigned long timeout;
|
2011-01-06 00:02:22 +03:00
|
|
|
void (*callback)(void *p);
|
|
|
|
void *p;
|
|
|
|
};
|
|
|
|
|
2012-01-07 02:04:23 +04:00
|
|
|
static int max_scheduled;
|
|
|
|
static int cur_scheduled;
|
2011-04-22 14:42:53 +04:00
|
|
|
|
2011-01-06 00:02:22 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
static nserror schedule_remove(void (*callback)(void *p), void *p)
|
2011-01-06 00:02:22 +03:00
|
|
|
{
|
2011-04-22 14:42:53 +04:00
|
|
|
struct nscallback *cur_nscb;
|
|
|
|
struct nscallback *prev_nscb;
|
|
|
|
struct nscallback *unlnk_nscb;
|
2011-01-06 00:02:22 +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;
|
|
|
|
}
|
2011-01-06 00:02:22 +03:00
|
|
|
|
2017-09-07 17:30:08 +03:00
|
|
|
NSLOG(schedule, DEBUG, "removing %p, %p", callback, p);
|
2011-01-06 00:02:22 +03:00
|
|
|
|
2011-04-22 14:42:53 +04:00
|
|
|
cur_nscb = schedule_list;
|
|
|
|
prev_nscb = NULL;
|
2011-01-06 00:02:22 +03:00
|
|
|
|
2011-04-22 14:42:53 +04:00
|
|
|
while (cur_nscb != NULL) {
|
|
|
|
if ((cur_nscb->callback == callback) &&
|
2011-01-06 00:02:22 +03:00
|
|
|
(cur_nscb->p == p)) {
|
2011-04-22 14:42:53 +04:00
|
|
|
/* item to remove */
|
2017-09-07 17:30:08 +03:00
|
|
|
NSLOG(schedule, DEBUG,
|
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
|
|
|
"callback entry %p removing %p(%p)", cur_nscb,
|
|
|
|
cur_nscb->callback, cur_nscb->p);
|
2011-04-22 14:42:53 +04:00
|
|
|
|
|
|
|
/* 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);
|
2012-01-07 02:04:23 +04:00
|
|
|
cur_scheduled--;
|
2011-04-22 14:42:53 +04:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* exported function documented in atari/schedule.h */
|
|
|
|
nserror atari_schedule(int ival, void (*callback)(void *p), void *p)
|
|
|
|
{
|
|
|
|
struct nscallback *nscb;
|
|
|
|
nserror ret;
|
|
|
|
|
|
|
|
/* remove any existing callback of this kind */
|
|
|
|
ret = schedule_remove(callback, p);
|
|
|
|
if ((ival < 0) || (ret != NSERROR_OK)) {
|
|
|
|
return ret;
|
|
|
|
}
|
2014-09-16 01:27:15 +04:00
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
nscb = calloc(1, sizeof(struct nscallback));
|
|
|
|
|
|
|
|
nscb->timeout = MS_NOW() + ival;
|
|
|
|
|
2017-09-07 17:30:08 +03:00
|
|
|
NSLOG(schedule, DEBUG, "adding callback %p for %p(%p) at %d ms", nscb,
|
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
|
|
|
callback, p, nscb->timeout);
|
2014-03-08 18:13:27 +04:00
|
|
|
|
|
|
|
nscb->callback = callback;
|
|
|
|
nscb->p = p;
|
|
|
|
|
|
|
|
/* add to list front */
|
|
|
|
nscb->next = schedule_list;
|
|
|
|
schedule_list = nscb;
|
|
|
|
cur_scheduled++;
|
|
|
|
if( cur_scheduled > max_scheduled ) {
|
|
|
|
max_scheduled = cur_scheduled;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
2011-01-06 00:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
/* exported function documented in atari/schedule.h */
|
|
|
|
int schedule_run(void)
|
2011-01-06 00:02:22 +03:00
|
|
|
{
|
2011-04-22 14:42:53 +04:00
|
|
|
unsigned long nexttime;
|
|
|
|
struct nscallback *cur_nscb;
|
|
|
|
struct nscallback *prev_nscb;
|
|
|
|
struct nscallback *unlnk_nscb;
|
2014-03-08 18:13:27 +04:00
|
|
|
unsigned long now = MS_NOW();
|
2011-01-06 00:02:22 +03:00
|
|
|
|
2011-04-22 14:42:53 +04:00
|
|
|
if (schedule_list == NULL)
|
|
|
|
return -1;
|
2011-01-06 00:02:22 +03:00
|
|
|
|
|
|
|
/* reset enumeration to the start of the list */
|
2011-04-22 14:42:53 +04:00
|
|
|
cur_nscb = schedule_list;
|
|
|
|
prev_nscb = NULL;
|
|
|
|
nexttime = cur_nscb->timeout;
|
|
|
|
|
2012-11-29 00:30:24 +04:00
|
|
|
while (cur_nscb != NULL) {
|
|
|
|
if (now > cur_nscb->timeout) {
|
2011-04-22 14:42:53 +04:00
|
|
|
/* scheduled time */
|
|
|
|
|
|
|
|
/* remove callback */
|
|
|
|
unlnk_nscb = cur_nscb;
|
|
|
|
if (prev_nscb == NULL) {
|
|
|
|
schedule_list = unlnk_nscb->next;
|
|
|
|
} else {
|
|
|
|
prev_nscb->next = unlnk_nscb->next;
|
|
|
|
}
|
2011-01-06 00:02:22 +03:00
|
|
|
|
2017-09-07 17:30:08 +03:00
|
|
|
NSLOG(schedule, DEBUG,
|
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
|
|
|
"callback entry %p running %p(%p)", unlnk_nscb,
|
|
|
|
unlnk_nscb->callback, unlnk_nscb->p);
|
2014-03-08 18:13:27 +04:00
|
|
|
|
2011-04-22 14:42:53 +04:00
|
|
|
/* call callback */
|
|
|
|
unlnk_nscb->callback(unlnk_nscb->p);
|
|
|
|
free(unlnk_nscb);
|
2012-01-07 02:04:23 +04:00
|
|
|
cur_scheduled--;
|
2011-01-06 00:02:22 +03:00
|
|
|
|
2011-04-22 14:42:53 +04:00
|
|
|
/* need to deal with callback modifying the list. */
|
|
|
|
if (schedule_list == NULL) {
|
2017-09-07 17:30:08 +03:00
|
|
|
NSLOG(schedule, DEBUG, "schedule_list == NULL");
|
2014-03-08 18:13:27 +04:00
|
|
|
|
2011-04-22 14:42:53 +04:00
|
|
|
return -1; /* no more callbacks scheduled */
|
|
|
|
}
|
2011-12-07 01:06:41 +04:00
|
|
|
|
2011-04-22 14:42:53 +04:00
|
|
|
/* reset enumeration to the start of the list */
|
|
|
|
cur_nscb = schedule_list;
|
|
|
|
prev_nscb = NULL;
|
|
|
|
nexttime = cur_nscb->timeout;
|
|
|
|
} else {
|
2011-01-06 00:02:22 +03:00
|
|
|
/* if the time to the event is sooner than the
|
2011-12-07 01:06:41 +04:00
|
|
|
* currently recorded soonest event record it
|
2011-01-06 00:02:22 +03:00
|
|
|
*/
|
2012-11-29 00:30:24 +04:00
|
|
|
if (nexttime > cur_nscb->timeout) {
|
2011-04-22 14:42:53 +04:00
|
|
|
nexttime = cur_nscb->timeout;
|
2011-01-06 00:02:22 +03:00
|
|
|
}
|
2011-04-22 14:42:53 +04:00
|
|
|
/* move to next element */
|
|
|
|
prev_nscb = cur_nscb;
|
|
|
|
cur_nscb = prev_nscb->next;
|
|
|
|
}
|
|
|
|
}
|
2011-01-06 00:02:22 +03:00
|
|
|
|
2011-04-22 14:42:53 +04:00
|
|
|
/* make rettime relative to now and convert to ms */
|
2014-03-08 18:13:27 +04:00
|
|
|
nexttime = nexttime - now;
|
|
|
|
|
2017-09-07 17:30:08 +03:00
|
|
|
NSLOG(schedule, DEBUG, "returning time to next event as %ldms",
|
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
|
|
|
nexttime);
|
2014-03-08 18:13:27 +04:00
|
|
|
|
2011-01-06 00:02:22 +03:00
|
|
|
/*return next event time in milliseconds (24days max wait) */
|
2014-03-08 18:13:27 +04:00
|
|
|
return nexttime;
|
2011-01-06 00:02:22 +03:00
|
|
|
}
|
|
|
|
|
2011-04-22 14:42:53 +04:00
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
/* exported function documented in atari/schedule.h */
|
2011-01-06 00:02:22 +03:00
|
|
|
void list_schedule(void)
|
|
|
|
{
|
2011-04-22 14:42:53 +04:00
|
|
|
struct nscallback *cur_nscb;
|
2011-01-06 00:02:22 +03:00
|
|
|
|
2017-09-07 17:30:08 +03:00
|
|
|
NSLOG(schedule, DEBUG, "schedule list at ms clock %ld", MS_NOW());
|
2011-01-06 00:02:22 +03:00
|
|
|
|
2011-04-22 14:42:53 +04:00
|
|
|
cur_nscb = schedule_list;
|
|
|
|
while (cur_nscb != NULL) {
|
2017-09-07 17:30:08 +03:00
|
|
|
NSLOG(schedule, DEBUG, "Schedule %p at %ld", cur_nscb,
|
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
|
|
|
cur_nscb->timeout);
|
2011-04-22 14:42:53 +04:00
|
|
|
cur_nscb = cur_nscb->next;
|
|
|
|
}
|
2017-09-07 17:30:08 +03:00
|
|
|
NSLOG(schedule, DEBUG, "Maxmium callbacks scheduled: %d", max_scheduled);
|
2011-01-06 00:02:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local Variables:
|
|
|
|
* c-basic-offset:8
|
|
|
|
* End:
|
|
|
|
*/
|