2011-03-12 18:08:07 +03:00
|
|
|
/*
|
2016-02-12 16:36:12 +03:00
|
|
|
* Copyright 2012 Vincent Sanders <vince@netsurf-browser.co.uk>
|
2011-03-12 18:08:07 +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/>.
|
|
|
|
*/
|
|
|
|
|
2016-02-12 16:36:12 +03:00
|
|
|
#include <time.h>
|
2011-03-12 18:08:07 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2016-04-19 11:16:09 +03:00
|
|
|
#include "utils/sys_time.h"
|
2016-02-12 16:36:12 +03:00
|
|
|
#include "utils/log.h"
|
2014-02-03 21:00:10 +04:00
|
|
|
|
|
|
|
#include "monkey/schedule.h"
|
2011-03-12 18:08:07 +03:00
|
|
|
|
2016-02-12 16:36:12 +03:00
|
|
|
/* linked list of scheduled callbacks */
|
|
|
|
static struct nscallback *schedule_list = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scheduled callback.
|
|
|
|
*/
|
|
|
|
struct nscallback
|
2011-03-12 18:08:07 +03:00
|
|
|
{
|
2017-06-10 00:04:58 +03:00
|
|
|
struct nscallback *next;
|
|
|
|
struct timeval tv;
|
|
|
|
void (*callback)(void *p);
|
|
|
|
void *p;
|
2016-02-12 16:36:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unschedule a callback.
|
|
|
|
*
|
|
|
|
* \param callback callback function
|
|
|
|
* \param p user parameter, passed to callback function
|
2019-06-08 15:33:16 +03:00
|
|
|
* \return NSERROR_OK if callback found and removed else NSERROR_NOT_FOUND
|
2016-02-12 16:36:12 +03:00
|
|
|
*
|
|
|
|
* All scheduled callbacks matching both callback and p are removed.
|
|
|
|
*/
|
|
|
|
static nserror schedule_remove(void (*callback)(void *p), void *p)
|
|
|
|
{
|
2017-06-10 00:04:58 +03:00
|
|
|
struct nscallback *cur_nscb;
|
|
|
|
struct nscallback *prev_nscb;
|
|
|
|
struct nscallback *unlnk_nscb;
|
2019-06-08 15:33:16 +03:00
|
|
|
bool removed = false;
|
2017-06-10 00:04:58 +03:00
|
|
|
|
|
|
|
/* check there is something on the list to remove */
|
|
|
|
if (schedule_list == NULL) {
|
2019-06-08 15:33:16 +03:00
|
|
|
return NSERROR_NOT_FOUND;
|
2017-06-10 00:04:58 +03:00
|
|
|
}
|
|
|
|
|
2017-09-07 17:30:08 +03:00
|
|
|
NSLOG(schedule, DEBUG, "removing %p, %p", callback, p);
|
2017-06-10 00:04:58 +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 */
|
|
|
|
|
2017-09-07 17:30:08 +03:00
|
|
|
NSLOG(schedule, DEBUG, "callback entry %p removing %p(%p)",
|
2017-06-10 00:04:58 +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);
|
2019-06-08 15:33:16 +03:00
|
|
|
removed = true;
|
2017-06-10 00:04:58 +03:00
|
|
|
} else {
|
|
|
|
/* move to next element */
|
|
|
|
prev_nscb = cur_nscb;
|
|
|
|
cur_nscb = prev_nscb->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 15:33:16 +03:00
|
|
|
if (removed == false) {
|
|
|
|
return NSERROR_NOT_FOUND;
|
|
|
|
}
|
2017-06-10 00:04:58 +03:00
|
|
|
return NSERROR_OK;
|
2011-03-12 18:08:07 +03:00
|
|
|
}
|
|
|
|
|
2017-06-10 00:16:06 +03:00
|
|
|
/* exported function documented in monkey/schedule.h */
|
2016-02-12 16:36:12 +03:00
|
|
|
nserror monkey_schedule(int tival, void (*callback)(void *p), void *p)
|
2011-03-12 18:08:07 +03:00
|
|
|
{
|
2017-06-10 00:04:58 +03:00
|
|
|
struct nscallback *nscb;
|
|
|
|
struct timeval tv;
|
|
|
|
nserror ret;
|
2016-02-12 16:36:12 +03:00
|
|
|
|
2017-06-10 00:04:58 +03:00
|
|
|
/* ensure uniqueness of the callback and context */
|
|
|
|
ret = schedule_remove(callback, p);
|
2019-06-08 15:33:16 +03:00
|
|
|
if (tival < 0) {
|
2017-06-10 00:04:58 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2016-02-12 16:36:12 +03:00
|
|
|
|
2017-09-07 17:30:08 +03:00
|
|
|
NSLOG(schedule, DEBUG, "Adding %p(%p) in %d", callback, p, tival);
|
2016-02-12 16:36:12 +03:00
|
|
|
|
2017-06-10 00:04:58 +03:00
|
|
|
tv.tv_sec = tival / 1000; /* miliseconds to seconds */
|
|
|
|
tv.tv_usec = (tival % 1000) * 1000; /* remainder to microseconds */
|
2016-02-12 16:36:12 +03:00
|
|
|
|
2017-06-10 00:04:58 +03:00
|
|
|
nscb = calloc(1, sizeof(struct nscallback));
|
2016-02-12 16:36:12 +03:00
|
|
|
|
2017-06-10 00:04:58 +03:00
|
|
|
gettimeofday(&nscb->tv, NULL);
|
|
|
|
timeradd(&nscb->tv, &tv, &nscb->tv);
|
2016-02-12 16:36:12 +03:00
|
|
|
|
2017-06-10 00:04:58 +03:00
|
|
|
nscb->callback = callback;
|
|
|
|
nscb->p = p;
|
2016-02-12 16:36:12 +03:00
|
|
|
|
2017-06-10 00:04:58 +03:00
|
|
|
/* add to list front */
|
|
|
|
nscb->next = schedule_list;
|
|
|
|
schedule_list = nscb;
|
2016-02-12 16:36:12 +03:00
|
|
|
|
2017-06-10 00:04:58 +03:00
|
|
|
return NSERROR_OK;
|
2011-03-12 18:08:07 +03:00
|
|
|
}
|
|
|
|
|
2017-06-10 00:16:06 +03:00
|
|
|
/* exported function documented in monkey/schedule.h */
|
2016-02-12 16:36:12 +03:00
|
|
|
int monkey_schedule_run(void)
|
2011-03-12 18:08:07 +03:00
|
|
|
{
|
2017-06-10 00:04:58 +03:00
|
|
|
struct timeval tv;
|
|
|
|
struct timeval nexttime;
|
|
|
|
struct timeval 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;
|
2016-02-12 16:36:12 +03:00
|
|
|
nexttime = cur_nscb->tv;
|
|
|
|
|
2017-06-10 00:04:58 +03:00
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
|
|
|
|
while (cur_nscb != NULL) {
|
|
|
|
if (timercmp(&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 (timercmp(&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 */
|
|
|
|
timersub(&nexttime, &tv, &rettime);
|
|
|
|
|
2017-09-07 17:30:08 +03:00
|
|
|
NSLOG(schedule, DEBUG, "returning time to next event as %ldms",
|
2017-09-08 22:47:22 +03:00
|
|
|
(long)((rettime.tv_sec * 1000) + (rettime.tv_usec / 1000)));
|
2017-06-10 00:04:58 +03:00
|
|
|
|
|
|
|
/* return next event time in milliseconds (24days max wait) */
|
|
|
|
return (rettime.tv_sec * 1000) + (rettime.tv_usec / 1000);
|
2011-03-12 18:08:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-12 16:36:12 +03:00
|
|
|
void monkey_schedule_list(void)
|
2011-03-12 18:08:07 +03:00
|
|
|
{
|
2017-06-10 00:04:58 +03:00
|
|
|
struct timeval tv;
|
|
|
|
struct nscallback *cur_nscb;
|
2014-03-08 18:13:27 +04:00
|
|
|
|
2017-06-10 00:04:58 +03:00
|
|
|
gettimeofday(&tv, NULL);
|
2014-03-08 18:13:27 +04:00
|
|
|
|
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
|
|
|
NSLOG(netsurf, INFO, "schedule list at %lld:%ld",
|
|
|
|
(long long)tv.tv_sec, tv.tv_usec);
|
2014-03-08 18:13:27 +04:00
|
|
|
|
2017-06-10 00:04:58 +03:00
|
|
|
cur_nscb = schedule_list;
|
2014-03-08 18:13:27 +04:00
|
|
|
|
2017-06-10 00:04:58 +03:00
|
|
|
while (cur_nscb != NULL) {
|
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
|
|
|
NSLOG(netsurf, INFO, "Schedule %p at %lld:%ld", cur_nscb,
|
|
|
|
(long long)cur_nscb->tv.tv_sec, cur_nscb->tv.tv_usec);
|
2017-06-10 00:04:58 +03:00
|
|
|
cur_nscb = cur_nscb->next;
|
|
|
|
}
|
2011-03-12 18:08:07 +03:00
|
|
|
}
|