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