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
36 struct nscallback
37 {
38 struct TimeRequest timereq;
39 struct TimeVal tv; /* time we expect the event to occur */
40 void *restrict callback;
41 void *restrict p;
42 };
43
44 static struct nscallback *tioreq;
45 struct Device *TimerBase;
46 #ifdef __amigaos4__
47 struct TimerIFace *ITimer;
48 #else
49 static struct MsgPort *schedule_msgport = NULL;
50 #endif
51
52 static 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
ami_schedule_remove_timer_event(struct nscallback * nscb)62 static void ami_schedule_remove_timer_event(struct nscallback *nscb)
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
ami_schedule_add_timer_event(struct nscallback * nscb,int t)81 static nserror ami_schedule_add_timer_event(struct nscallback *nscb, int t)
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
110 static struct nscallback *
ami_schedule_locate(void (* callback)(void * p),void * p,bool remove)111 ami_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
ami_schedule_reschedule(struct nscallback * nscb,int t)146 static nserror ami_schedule_reschedule(struct nscallback *nscb, int t)
147 {
148 ami_schedule_remove_timer_event(nscb);
149 if (ami_schedule_add_timer_event(nscb, t) != NSERROR_OK)
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
schedule_remove(void (* callback)(void * p),void * p,bool abort)166 static 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__
175 FreeSysObject(ASOT_IOREQUEST, nscb);
176 #else
177 FreeVec(nscb);
178 #endif
179 pblHeapConstruct(schedule_list);
180 }
181
182 return NSERROR_OK;
183 }
184
schedule_remove_all(void)185 static 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) {
195 ami_schedule_remove_timer_event(nscb);
196 pblIteratorRemove(iterator);
197 #ifdef __amigaos4__
198 FreeSysObject(ASOT_IOREQUEST, nscb);
199 #else
200 FreeVec(nscb);
201 #endif
202 };
203
204 pblIteratorFree(iterator);
205 }
206
ami_schedule_compare(const void * prev,const void * next)207 static 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 */
ami_schedule_dump(void)217 static 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 */
ami_scheduler_run(struct nscallback * nscb)258 static 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
ami_schedule_open_timer(struct MsgPort * msgport)272 static void ami_schedule_open_timer(struct MsgPort *msgport)
273 {
274 #ifdef __amigaos4__
275 tioreq = (struct nscallback *)AllocSysObjectTags(ASOT_IOREQUEST,
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
ami_schedule_close_timer(void)292 static void ami_schedule_close_timer(void)
293 {
294 #ifdef __amigaos4__
295 if(ITimer) DropInterface((struct Interface *)ITimer);
296 #endif
297 CloseDevice((struct IORequest *) tioreq);
298 FreeSysObject(ASOT_IOREQUEST, tioreq);
299 }
300
301 /* exported interface documented in amiga/schedule.h */
ami_schedule_create(struct MsgPort * msgport)302 nserror ami_schedule_create(struct MsgPort *msgport)
303 {
304 ami_schedule_open_timer(msgport);
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 */
ami_schedule_free(void)320 void ami_schedule_free(void)
321 {
322 ami_schedule_dump();
323 schedule_remove_all();
324 pblHeapFree(schedule_list); // this should be empty at this point
325 schedule_list = NULL;
326
327 ami_schedule_close_timer();
328 }
329
330 /* exported function documented in amiga/schedule.h */
ami_schedule(int t,void (* callback)(void * p),void * p)331 nserror 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__
345 nscb = AllocSysObjectTags(ASOT_IOREQUEST,
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
356 if (ami_schedule_add_timer_event(nscb, t) != NSERROR_OK)
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 */
ami_schedule_handle(struct MsgPort * nsmsgport)369 void 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
381