NetSurf
schedule.c
Go to the documentation of this file.
1/*
2 * Copyright 2008 - 2016 Chris Young <chris@unsatisfactorysoftware.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 "amiga/os3support.h"
20
21#include <proto/dos.h>
22#include <proto/exec.h>
23#include <proto/timer.h>
24#include <proto/utility.h> /* For Amiga2Date */
25
26#include <stdio.h>
27#include <stdbool.h>
28#include <pbl.h>
29
30#include "utils/errors.h"
31#include "utils/log.h"
32
33#include "amiga/memory.h"
34#include "amiga/schedule.h"
35
37{
39 struct TimeVal tv; /* time we expect the event to occur */
40 void *restrict callback;
41 void *restrict p;
42};
43
44static struct nscallback *tioreq;
45struct Device *TimerBase;
46#ifdef __amigaos4__
47struct TimerIFace *ITimer;
48#else
49static struct MsgPort *schedule_msgport = NULL;
50#endif
51
52static PblHeap *schedule_list;
53
54/**
55 * Remove timer event
56 *
57 * \param nscb callback
58 *
59 * The timer event for the callback is aborted
60 */
61
63{
64 if(!nscb) return;
65
66 if(CheckIO((struct IORequest *)nscb)==NULL)
67 AbortIO((struct IORequest *)nscb);
68
69 WaitIO((struct IORequest *)nscb);
70}
71
72/**
73 * Add timer event
74 *
75 * \param nscb callback
76 * \param t time in ms
77 *
78 * NetSurf will be signalled in t ms for this event.
79 */
80
82{
83 struct TimeVal tv;
84 ULONG time_us = t * 1000; /* t converted to µs */
85
86 tv.Seconds = time_us / 1000000;
87 tv.Microseconds = time_us % 1000000;
88
89 GetSysTime(&nscb->tv);
90 AddTime(&nscb->tv, &tv); // now contains time when event occurs (for debug and heap sorting)
91
92 nscb->timereq.Request.io_Command = TR_ADDREQUEST;
93 nscb->timereq.Time.Seconds = tv.Seconds; // secs
94 nscb->timereq.Time.Microseconds = tv.Microseconds; // micro
95 SendIO((struct IORequest *)nscb);
96
97 return NSERROR_OK;
98}
99
100/**
101 * Locate a scheduled callback
102 *
103 * \param callback callback function
104 * \param p user parameter, passed to callback function
105 * \param remove remove callback from the heap
106 *
107 * A scheduled callback matching both callback and p is returned, or NULL if none present.
108 */
109
110static struct nscallback *
111ami_schedule_locate(void (*callback)(void *p), void *p, bool remove)
112{
113 PblIterator *iterator;
114 struct nscallback *nscb;
115 bool found_cb = false;
116
117 /* check there is something on the list */
118 if (schedule_list == NULL) return NULL;
119 if(pblHeapIsEmpty(schedule_list)) return NULL;
120
121 iterator = pblHeapIterator(schedule_list);
122
123 while ((nscb = pblIteratorNext(iterator)) != (void *)-1) {
124 if ((nscb->callback == callback) && (nscb->p == p)) {
125 if (remove == true) pblIteratorRemove(iterator);
126 found_cb = true;
127 break;
128 }
129 };
130
131 pblIteratorFree(iterator);
132
133 if (found_cb == true) return nscb;
134 else return NULL;
135}
136
137/**
138 * Reschedule a callback.
139 *
140 * \param nscb callback
141 * \param t time in ms
142 *
143 * The nscallback will be rescheduled for t ms.
144 */
145
146static nserror ami_schedule_reschedule(struct nscallback *nscb, int t)
147{
150 return NSERROR_NOMEM;
151
152 pblHeapConstruct(schedule_list);
153 return NSERROR_OK;
154}
155
156/**
157 * Unschedule a callback.
158 *
159 * \param callback callback function
160 * \param p user parameter, passed to callback function
161 * \param abort abort pending timer
162 *
163 * All scheduled callbacks matching both callback and p are removed.
164 */
165
166static nserror schedule_remove(void (*callback)(void *p), void *p, bool abort)
167{
168 struct nscallback *nscb;
169
170 nscb = ami_schedule_locate(callback, p, true);
171
172 if(nscb != NULL) {
173 if(abort == true) ami_schedule_remove_timer_event(nscb);
174#ifdef __amigaos4__
176#else
177 FreeVec(nscb);
178#endif
179 pblHeapConstruct(schedule_list);
180 }
181
182 return NSERROR_OK;
183}
184
185static void schedule_remove_all(void)
186{
187 PblIterator *iterator;
188 struct nscallback *nscb;
189
190 if(pblHeapIsEmpty(schedule_list)) return;
191
192 iterator = pblHeapIterator(schedule_list);
193
194 while ((nscb = pblIteratorNext(iterator)) != (void *)-1) {
196 pblIteratorRemove(iterator);
197#ifdef __amigaos4__
199#else
200 FreeVec(nscb);
201#endif
202 };
203
204 pblIteratorFree(iterator);
205}
206
207static int ami_schedule_compare(const void *prev, const void *next)
208{
209 struct nscallback *nscb1 = *(struct nscallback **)prev;
210 struct nscallback *nscb2 = *(struct nscallback **)next;
211
212 /**\todo a heap probably isn't the best idea now */
213 return CmpTime(&nscb1->tv, &nscb2->tv);
214}
215
216/* Outputs all scheduled events to the log */
217static void ami_schedule_dump(void)
218{
219 PblIterator *iterator;
220 struct nscallback *nscb;
221 struct ClockData clockdata;
222
223 if(pblHeapIsEmpty(schedule_list)) return;
224
225 struct TimeVal tv;
226 GetSysTime(&tv);
227 Amiga2Date(tv.Seconds, &clockdata);
228
229 NSLOG(netsurf, INFO, "Current time = %d-%d-%d %d:%d:%d.%lu",
230 clockdata.mday, clockdata.month, clockdata.year,
231 clockdata.hour, clockdata.min, clockdata.sec, tv.Microseconds);
232 NSLOG(netsurf, INFO, "Events remaining in queue:");
233
234 iterator = pblHeapIterator(schedule_list);
235
236 while ((nscb = pblIteratorNext(iterator)) != (void *)-1) {
237 Amiga2Date(nscb->tv.Seconds, &clockdata);
238 NSLOG(netsurf, INFO,
239 "nscb: %p, at %d-%d-%d %d:%d:%d.%lu, callback: %p, %p",
240 nscb, clockdata.mday, clockdata.month, clockdata.year,
241 clockdata.hour, clockdata.min, clockdata.sec,
242 nscb->tv.Microseconds, nscb->callback, nscb->p);
243 if(CheckIO((struct IORequest *)nscb) == NULL) {
244 NSLOG(netsurf, INFO, "-> ACTIVE");
245 } else {
246 NSLOG(netsurf, INFO, "-> COMPLETE");
247 }
248 };
249
250 pblIteratorFree(iterator);
251}
252
253/**
254 * Process signalled event
255 *
256 * This implementation only processes the callback that arrives in the message from timer.device.
257 */
258static bool ami_scheduler_run(struct nscallback *nscb)
259{
260 void (*callback)(void *p);
261 void *p;
262
263 callback = nscb->callback;
264 p = nscb->p;
265
266 schedule_remove(callback, p, false); /* this does a lookup as we don't know if we're the first item on the heap */
267
268 callback(p);
269 return true;
270}
271
272static void ami_schedule_open_timer(struct MsgPort *msgport)
273{
274#ifdef __amigaos4__
276 ASOIOR_Size, sizeof(struct nscallback),
277 ASOIOR_ReplyPort, msgport,
278 ASO_NoTrack, FALSE,
279 TAG_DONE);
280#else
281 tioreq = (struct nscallback *)CreateIORequest(msgport, sizeof(struct nscallback));
282#endif
283
284 OpenDevice("timer.device", UNIT_VBLANK, (struct IORequest *)tioreq, 0);
285
286 TimerBase = (struct Device *)tioreq->timereq.Request.io_Device;
287#ifdef __amigaos4__
288 ITimer = (struct TimerIFace *)GetInterface((struct Library *)TimerBase, "main", 1, NULL);
289#endif
290}
291
293{
294#ifdef __amigaos4__
295 if(ITimer) DropInterface((struct Interface *)ITimer);
296#endif
297 CloseDevice((struct IORequest *) tioreq);
299}
300
301/* exported interface documented in amiga/schedule.h */
302nserror ami_schedule_create(struct MsgPort *msgport)
303{
305#ifndef __amigaos4__
306 schedule_msgport = msgport;
307#endif
308 schedule_list = pblHeapNew();
309
310 if (schedule_list == NULL) {
311 return NSERROR_NOMEM;
312 }
313
314 pblHeapSetCompareFunction(schedule_list, ami_schedule_compare);
315
316 return NSERROR_OK;
317}
318
319/* exported interface documented in amiga/schedule.h */
321{
324 pblHeapFree(schedule_list); // this should be empty at this point
325 schedule_list = NULL;
326
328}
329
330/* exported function documented in amiga/schedule.h */
331nserror ami_schedule(int t, void (*callback)(void *p), void *p)
332{
333 struct nscallback *nscb;
334
335 if(t == 0) t = 1;
336
337 if(schedule_list == NULL) return NSERROR_INIT_FAILED;
338 if(t < 0) return schedule_remove(callback, p, true);
339
340 if ((nscb = ami_schedule_locate(callback, p, false))) {
341 return ami_schedule_reschedule(nscb, t);
342 }
343
344#ifdef __amigaos4__
346 ASOIOR_Duplicate, tioreq,
347 TAG_DONE);
348 if(nscb == NULL) return NSERROR_NOMEM;
349#else
350 if(schedule_msgport == NULL) return NSERROR_NOMEM;
351 nscb = AllocVec(sizeof(struct nscallback), MEMF_PUBLIC | MEMF_CLEAR);
352 if(nscb == NULL) return NSERROR_NOMEM;
353 *nscb = *tioreq;
354#endif
355
357 return NSERROR_NOMEM;
358
359 nscb->callback = callback;
360 nscb->p = p;
361
362 pblHeapInsert(schedule_list, nscb);
363
364 return NSERROR_OK;
365
366}
367
368/* exported interface documented in amiga/schedule.h */
369void ami_schedule_handle(struct MsgPort *nsmsgport)
370{
371 /* nsmsgport is the NetSurf message port that
372 * timer.device is sending messages to. */
373
374 struct nscallback *timermsg;
375
376 while((timermsg = (struct nscallback *)GetMsg(nsmsgport))) {
377 ami_scheduler_run(timermsg);
378 };
379}
380
static nserror ami_schedule_reschedule(struct nscallback *nscb, int t)
Reschedule a callback.
Definition: schedule.c:146
void ami_schedule_free(void)
Finalise amiga scheduler.
Definition: schedule.c:320
static void ami_schedule_dump(void)
Definition: schedule.c:217
static struct nscallback * tioreq
Definition: schedule.c:44
static void ami_schedule_open_timer(struct MsgPort *msgport)
Definition: schedule.c:272
static nserror schedule_remove(void(*callback)(void *p), void *p, bool abort)
Unschedule a callback.
Definition: schedule.c:166
static void schedule_remove_all(void)
Definition: schedule.c:185
static int ami_schedule_compare(const void *prev, const void *next)
Definition: schedule.c:207
static bool ami_scheduler_run(struct nscallback *nscb)
Process signalled event.
Definition: schedule.c:258
static void ami_schedule_remove_timer_event(struct nscallback *nscb)
Remove timer event.
Definition: schedule.c:62
nserror ami_schedule(int t, void(*callback)(void *p), void *p)
Schedule a callback.
Definition: schedule.c:331
struct Device * TimerBase
Definition: schedule.c:45
static nserror ami_schedule_add_timer_event(struct nscallback *nscb, int t)
Add timer event.
Definition: schedule.c:81
static PblHeap * schedule_list
Definition: schedule.c:52
void ami_schedule_handle(struct MsgPort *nsmsgport)
Handle a message received from the scheduler process.
Definition: schedule.c:369
static struct MsgPort * schedule_msgport
Definition: schedule.c:49
static void ami_schedule_close_timer(void)
Definition: schedule.c:292
static struct nscallback * ami_schedule_locate(void(*callback)(void *p), void *p, bool remove)
Locate a scheduled callback.
Definition: schedule.c:111
nserror ami_schedule_create(struct MsgPort *msgport)
Initialise amiga scheduler.
Definition: schedule.c:302
Error codes.
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_INIT_FAILED
Initialisation failed.
Definition: errors.h:38
@ NSERROR_NOMEM
Memory exhaustion.
Definition: errors.h:32
@ NSERROR_OK
No error.
Definition: errors.h:30
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
void FreeSysObject(ULONG type, APTR obj)
Definition: os3support.c:350
Minimal compatibility header for AmigaOS 3.
#define AllocSysObjectTags(A, B, C, D)
Definition: os3support.h:157
#define ASO_NoTrack
Definition: os3support.h:64
@ ASOT_IOREQUEST
Definition: os3support.h:227
struct TimeVal Time
Definition: os3support.h:197
struct IORequest Request
Definition: os3support.h:196
uint32 Seconds
Definition: os3support.h:190
uint32 Microseconds
Definition: os3support.h:191
scheduled callback.
Definition: schedule.c:37
struct TimeRequest timereq
Definition: schedule.c:38
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