NetSurf
schedule.c
Go to the documentation of this file.
1/*
2 * Copyright 2008 Vincent Sanders <vince@simtec.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
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 *
47 * All scheduled callbacks matching both callback and p are removed.
48 */
49static nserror schedule_remove(void (*callback)(void *p), void *p)
50{
51 struct nscallback *cur_nscb;
52 struct nscallback *prev_nscb;
53 struct nscallback *unlnk_nscb;
54
55 /* check there is something on the list to remove */
56 if (schedule_list == NULL) {
57 return NSERROR_OK;
58 }
59
60 NSLOG(schedule, DEBUG, "removing %p, %p", callback, p);
61
62 cur_nscb = schedule_list;
63 prev_nscb = NULL;
64
65 while (cur_nscb != NULL) {
66 if ((cur_nscb->callback == callback) &&
67 (cur_nscb->p == p)) {
68 /* item to remove */
69
70 NSLOG(schedule, DEBUG,
71 "callback entry %p removing %p(%p)",
72 cur_nscb, cur_nscb->callback, cur_nscb->p);
73
74 /* remove callback */
75 unlnk_nscb = cur_nscb;
76 cur_nscb = unlnk_nscb->next;
77
78 if (prev_nscb == NULL) {
79 schedule_list = cur_nscb;
80 } else {
81 prev_nscb->next = cur_nscb;
82 }
83 free (unlnk_nscb);
84 } else {
85 /* move to next element */
86 prev_nscb = cur_nscb;
87 cur_nscb = prev_nscb->next;
88 }
89 }
90
91 return NSERROR_OK;
92}
93
94/* exported function documented in framebuffer/schedule.h */
95nserror framebuffer_schedule(int tival, void (*callback)(void *p), void *p)
96{
97 struct nscallback *nscb;
98 struct timeval tv;
99 nserror ret;
100
101 /* ensure uniqueness of the callback and context */
102 ret = schedule_remove(callback, p);
103 if ((tival < 0) || (ret != NSERROR_OK)) {
104 return ret;
105 }
106
107 NSLOG(schedule, DEBUG, "Adding %p(%p) in %d", callback, p, tival);
108
109 tv.tv_sec = tival / 1000; /* miliseconds to seconds */
110 tv.tv_usec = (tival % 1000) * 1000; /* remainder to microseconds */
111
112 nscb = calloc(1, sizeof(struct nscallback));
113
114 gettimeofday(&nscb->tv, NULL);
115 timeradd(&nscb->tv, &tv, &nscb->tv);
116
117 nscb->callback = callback;
118 nscb->p = p;
119
120 /* add to list front */
121 nscb->next = schedule_list;
122 schedule_list = nscb;
123
124 return NSERROR_OK;
125}
126
127/* exported function documented in framebuffer/schedule.h */
129{
130 struct timeval tv;
131 struct timeval nexttime;
132 struct timeval rettime;
133 struct nscallback *cur_nscb;
134 struct nscallback *prev_nscb;
135 struct nscallback *unlnk_nscb;
136
137 if (schedule_list == NULL)
138 return -1;
139
140 /* reset enumeration to the start of the list */
141 cur_nscb = schedule_list;
142 prev_nscb = NULL;
143 nexttime = cur_nscb->tv;
144
145 gettimeofday(&tv, NULL);
146
147 while (cur_nscb != NULL) {
148 if (timercmp(&tv, &cur_nscb->tv, >)) {
149 /* scheduled time */
150
151 /* remove callback */
152 unlnk_nscb = cur_nscb;
153
154 if (prev_nscb == NULL) {
155 schedule_list = unlnk_nscb->next;
156 } else {
157 prev_nscb->next = unlnk_nscb->next;
158 }
159
160 unlnk_nscb->callback(unlnk_nscb->p);
161
162 free(unlnk_nscb);
163
164 /* need to deal with callback modifying the list. */
165 if (schedule_list == NULL)
166 return -1; /* no more callbacks scheduled */
167
168 /* reset enumeration to the start of the list */
169 cur_nscb = schedule_list;
170 prev_nscb = NULL;
171 nexttime = cur_nscb->tv;
172 } else {
173 /* if the time to the event is sooner than the
174 * currently recorded soonest event record it
175 */
176 if (timercmp(&nexttime, &cur_nscb->tv, >)) {
177 nexttime = cur_nscb->tv;
178 }
179 /* move to next element */
180 prev_nscb = cur_nscb;
181 cur_nscb = prev_nscb->next;
182 }
183 }
184
185 /* make rettime relative to now */
186 timersub(&nexttime, &tv, &rettime);
187
188 NSLOG(schedule, DEBUG,
189 "returning time to next event as %ldms",
190 (rettime.tv_sec * 1000) + (rettime.tv_usec / 1000));
191
192 /* return next event time in milliseconds (24days max wait) */
193 return (rettime.tv_sec * 1000) + (rettime.tv_usec / 1000);
194}
195
197{
198 struct timeval tv;
199 struct nscallback *cur_nscb;
200
201 gettimeofday(&tv, NULL);
202
203 NSLOG(netsurf, INFO, "schedule list at %ld:%ld", tv.tv_sec,
204 tv.tv_usec);
205
206 cur_nscb = schedule_list;
207
208 while (cur_nscb != NULL) {
209 NSLOG(netsurf, INFO, "Schedule %p at %ld:%ld", cur_nscb,
210 cur_nscb->tv.tv_sec, cur_nscb->tv.tv_usec);
211 cur_nscb = cur_nscb->next;
212 }
213}
214
215
216/*
217 * Local Variables:
218 * c-basic-offset:8
219 * End:
220 */
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
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_OK
No error.
Definition: errors.h:30
static struct nscallback * schedule_list
Definition: schedule.c:28
static nserror schedule_remove(void(*callback)(void *p), void *p)
Unschedule a callback.
Definition: schedule.c:49
nserror framebuffer_schedule(int tival, void(*callback)(void *p), void *p)
Schedule a callback.
Definition: schedule.c:95
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
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.