NetSurf
schedule.c
Go to the documentation of this file.
1/*
2 * Copyright 2012 Vincent Sanders <vince@netsurf-browser.co.uk>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <time.h>
20#include <stdlib.h>
21
22#include "utils/sys_time.h"
23#include "utils/log.h"
24
25#include "monkey/schedule.h"
26
27/* linked list of scheduled callbacks */
28static struct nscallback *schedule_list = NULL;
29
30/**
31 * scheduled callback.
32 */
33struct nscallback
34{
35 struct nscallback *next;
36 struct timeval tv;
37 void (*callback)(void *p);
38 void *p;
39};
40
41/**
42 * Unschedule a callback.
43 *
44 * \param callback callback function
45 * \param p user parameter, passed to callback function
46 * \return NSERROR_OK if callback found and removed else NSERROR_NOT_FOUND
47 *
48 * All scheduled callbacks matching both callback and p are removed.
49 */
50static nserror schedule_remove(void (*callback)(void *p), void *p)
51{
52 struct nscallback *cur_nscb;
53 struct nscallback *prev_nscb;
54 struct nscallback *unlnk_nscb;
55 bool removed = false;
56
57 /* check there is something on the list to remove */
58 if (schedule_list == NULL) {
59 return NSERROR_NOT_FOUND;
60 }
61
62 NSLOG(schedule, DEBUG, "removing %p, %p", callback, p);
63
64 cur_nscb = schedule_list;
65 prev_nscb = NULL;
66
67 while (cur_nscb != NULL) {
68 if ((cur_nscb->callback == callback) &&
69 (cur_nscb->p == p)) {
70 /* item to remove */
71
72 NSLOG(schedule, DEBUG, "callback entry %p removing %p(%p)",
73 cur_nscb, cur_nscb->callback, cur_nscb->p);
74
75 /* remove callback */
76 unlnk_nscb = cur_nscb;
77 cur_nscb = unlnk_nscb->next;
78
79 if (prev_nscb == NULL) {
80 schedule_list = cur_nscb;
81 } else {
82 prev_nscb->next = cur_nscb;
83 }
84 free (unlnk_nscb);
85 removed = true;
86 } else {
87 /* move to next element */
88 prev_nscb = cur_nscb;
89 cur_nscb = prev_nscb->next;
90 }
91 }
92
93 if (removed == false) {
94 return NSERROR_NOT_FOUND;
95 }
96 return NSERROR_OK;
97}
98
99/* exported function documented in monkey/schedule.h */
100nserror monkey_schedule(int tival, void (*callback)(void *p), void *p)
101{
102 struct nscallback *nscb;
103 struct timeval tv;
104 nserror ret;
105
106 /* ensure uniqueness of the callback and context */
107 ret = schedule_remove(callback, p);
108 if (tival < 0) {
109 return ret;
110 }
111
112 NSLOG(schedule, DEBUG, "Adding %p(%p) in %d", callback, p, tival);
113
114 tv.tv_sec = tival / 1000; /* miliseconds to seconds */
115 tv.tv_usec = (tival % 1000) * 1000; /* remainder to microseconds */
116
117 nscb = calloc(1, sizeof(struct nscallback));
118
119 gettimeofday(&nscb->tv, NULL);
120 timeradd(&nscb->tv, &tv, &nscb->tv);
121
122 nscb->callback = callback;
123 nscb->p = p;
124
125 /* add to list front */
126 nscb->next = schedule_list;
127 schedule_list = nscb;
128
129 return NSERROR_OK;
130}
131
132/* exported function documented in monkey/schedule.h */
134{
135 struct timeval tv;
136 struct timeval nexttime;
137 struct timeval rettime;
138 struct nscallback *cur_nscb;
139 struct nscallback *prev_nscb;
140 struct nscallback *unlnk_nscb;
141
142 if (schedule_list == NULL)
143 return -1;
144
145 /* reset enumeration to the start of the list */
146 cur_nscb = schedule_list;
147 prev_nscb = NULL;
148 nexttime = cur_nscb->tv;
149
150 gettimeofday(&tv, NULL);
151
152 while (cur_nscb != NULL) {
153 if (timercmp(&tv, &cur_nscb->tv, >)) {
154 /* scheduled time */
155
156 /* remove callback */
157 unlnk_nscb = cur_nscb;
158
159 if (prev_nscb == NULL) {
160 schedule_list = unlnk_nscb->next;
161 } else {
162 prev_nscb->next = unlnk_nscb->next;
163 }
164
165 unlnk_nscb->callback(unlnk_nscb->p);
166
167 free(unlnk_nscb);
168
169 /* need to deal with callback modifying the list. */
170 if (schedule_list == NULL)
171 return -1; /* no more callbacks scheduled */
172
173 /* reset enumeration to the start of the list */
174 cur_nscb = schedule_list;
175 prev_nscb = NULL;
176 nexttime = cur_nscb->tv;
177 } else {
178 /* if the time to the event is sooner than the
179 * currently recorded soonest event record it
180 */
181 if (timercmp(&nexttime, &cur_nscb->tv, >)) {
182 nexttime = cur_nscb->tv;
183 }
184 /* move to next element */
185 prev_nscb = cur_nscb;
186 cur_nscb = prev_nscb->next;
187 }
188 }
189
190 /* make rettime relative to now */
191 timersub(&nexttime, &tv, &rettime);
192
193 NSLOG(schedule, DEBUG, "returning time to next event as %ldms",
194 (long)((rettime.tv_sec * 1000) + (rettime.tv_usec / 1000)));
195
196 /* return next event time in milliseconds (24days max wait) */
197 return (rettime.tv_sec * 1000) + (rettime.tv_usec / 1000);
198}
199
201{
202 struct timeval tv;
203 struct nscallback *cur_nscb;
204
205 gettimeofday(&tv, NULL);
206
207 NSLOG(netsurf, INFO, "schedule list at %lld:%ld",
208 (long long)tv.tv_sec, tv.tv_usec);
209
210 cur_nscb = schedule_list;
211
212 while (cur_nscb != NULL) {
213 NSLOG(netsurf, INFO, "Schedule %p at %lld:%ld", cur_nscb,
214 (long long)cur_nscb->tv.tv_sec, cur_nscb->tv.tv_usec);
215 cur_nscb = cur_nscb->next;
216 }
217}
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_NOT_FOUND
Requested item not found.
Definition: errors.h:34
@ NSERROR_OK
No error.
Definition: errors.h:30
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
static struct nscallback * schedule_list
Definition: schedule.c:28
void monkey_schedule_list(void)
Log a list of all scheduled callbacks.
Definition: schedule.c:200
nserror monkey_schedule(int tival, void(*callback)(void *p), void *p)
Schedule a callback.
Definition: schedule.c:100
int monkey_schedule_run(void)
Process scheduled callbacks up to current time.
Definition: schedule.c:133
static nserror schedule_remove(void(*callback)(void *p), void *p)
Unschedule a callback.
Definition: schedule.c:50
scheduled callback.
Definition: schedule.c:37
struct nscallback * next
Definition: schedule.c:40
struct TimeVal tv
Definition: schedule.c:39
void *restrict p
Definition: schedule.c:41
void *restrict callback
Definition: schedule.c:40
BSD style timeval macros.
#define timercmp(a, aa, cmp)
Definition: sys_time.h:66
#define timeradd(a, aa, result)
Definition: sys_time.h:42
#define timersub(a, aa, result)
Definition: sys_time.h:54
Interface to time operations.