summaryrefslogtreecommitdiff
path: root/amiga/schedule.c
diff options
context:
space:
mode:
Diffstat (limited to 'amiga/schedule.c')
-rwxr-xr-xamiga/schedule.c171
1 files changed, 109 insertions, 62 deletions
diff --git a/amiga/schedule.c b/amiga/schedule.c
index 9aa74881f..6222917cf 100755
--- a/amiga/schedule.c
+++ b/amiga/schedule.c
@@ -39,7 +39,15 @@ struct nscallback
PblHeap *schedule_list;
-static void ami_remove_timer_event(struct nscallback *nscb)
+/**
+ * Remove timer event
+ *
+ * \param nscb callback
+ *
+ * The timer event for the callback is aborted
+ */
+
+static void ami_schedule_remove_timer_event(struct nscallback *nscb)
{
if(!nscb) return;
@@ -54,48 +62,112 @@ static void ami_remove_timer_event(struct nscallback *nscb)
}
/**
- * Unschedule a callback.
+ * Add timer event
+ *
+ * \param nscb callback
+ * \param t time in ms
+ *
+ * NetSurf will be signalled in t ms for this event.
+ */
+
+static nserror ami_schedule_add_timer_event(struct nscallback *nscb, int t)
+{
+ struct TimeVal tv;
+ ULONG time_us = t * 1000; /* t converted to µs */
+
+ nscb->tv.Seconds = time_us / 1000000;
+ nscb->tv.Microseconds = time_us % 1000000;
+
+ GetSysTime(&tv);
+ AddTime(&nscb->tv,&tv); // now contains time when event occurs
+
+ if(nscb->treq = AllocVecTagList(sizeof(struct TimeRequest), NULL)) {
+ *nscb->treq = *tioreq;
+ nscb->treq->Request.io_Command=TR_ADDREQUEST;
+ nscb->treq->Time.Seconds=nscb->tv.Seconds; // secs
+ nscb->treq->Time.Microseconds=nscb->tv.Microseconds; // micro
+ SendIO((struct IORequest *)nscb->treq);
+ } else {
+ return NSERROR_NOMEM;
+ }
+
+ return NSERROR_OK;
+}
+
+/**
+ * Locate a scheduled callback
*
* \param callback callback function
* \param p user parameter, passed to callback function
+ * \param remove remove callback from the heap
*
- * All scheduled callbacks matching both callback and p are removed.
+ * A scheduled callback matching both callback and p is returned, or NULL if none present.
*/
-static nserror schedule_remove(void (*callback)(void *p), void *p)
+static struct nscallback *ami_schedule_locate(void (*callback)(void *p), void *p, bool remove)
{
PblIterator *iterator;
struct nscallback *nscb;
- bool restoreheap = false;
-
- /* check there is something on the list to remove */
- if (schedule_list == NULL)
- {
- return NSERROR_OK;
- }
+ bool found_cb = false;
- if(pblHeapIsEmpty(schedule_list))
- {
- return NSERROR_OK;
- }
+ /* check there is something on the list */
+ if (schedule_list == NULL) return NULL;
+ if(pblHeapIsEmpty(schedule_list)) return NULL;
iterator = pblHeapIterator(schedule_list);
- while ((nscb = pblIteratorNext(iterator)) != -1)
- {
- if((nscb->callback == callback) && (nscb->p == p))
- {
- ami_remove_timer_event(nscb);
- pblIteratorRemove(iterator);
- FreeVec(nscb);
- restoreheap = true;
+ while ((nscb = pblIteratorNext(iterator)) != -1) {
+ if ((nscb->callback == callback) && (nscb->p == p)) {
+ if (remove == true) pblIteratorRemove(iterator);
+ found_cb = true;
+ break;
}
};
pblIteratorFree(iterator);
- if(restoreheap)
- {
+ if (found_cb == true) return nscb;
+ else return NULL;
+}
+
+/**
+ * Reschedule a callback.
+ *
+ * \param nscb callback
+ * \param t time in ms
+ *
+ * The nscallback will be rescheduled for t ms.
+ */
+
+static nserror ami_schedule_reschedule(struct nscallback *nscb, int t)
+{
+ ami_schedule_remove_timer_event(nscb);
+ if (ami_schedule_add_timer_event(nscb, t) != NSERROR_OK)
+ return NSERROR_NOMEM;
+
+ pblHeapConstruct(schedule_list);
+ return NSERROR_OK;
+}
+
+/**
+ * Unschedule a callback.
+ *
+ * \param callback callback function
+ * \param p user parameter, passed to callback function
+ *
+ * All scheduled callbacks matching both callback and p are removed.
+ */
+
+static nserror schedule_remove(void (*callback)(void *p), void *p)
+{
+ PblIterator *iterator;
+ struct nscallback *nscb;
+
+ nscb = ami_schedule_locate(callback, p, true);
+
+ if(nscb != NULL) {
+ ami_schedule_remove_timer_event(nscb);
+ FreeVec(nscb);
pblHeapConstruct(schedule_list);
}
@@ -113,7 +185,7 @@ static void schedule_remove_all(void)
while ((nscb = pblIteratorNext(iterator)) != -1)
{
- ami_remove_timer_event(nscb);
+ ami_schedule_remove_timer_event(nscb);
pblIteratorRemove(iterator);
FreeVec(nscb);
};
@@ -155,7 +227,7 @@ void schedule_run(BOOL poll)
callback = nscb->callback;
p = nscb->p;
- ami_remove_timer_event(nscb);
+ ami_schedule_remove_timer_event(nscb);
pblHeapRemoveFirst(schedule_list);
FreeVec(nscb);
callback(p);
@@ -200,11 +272,7 @@ void ami_schedule_open_timer(void)
/* exported function documented in amiga/schedule.h */
void ami_schedule_close_timer(void)
{
- if(ITimer)
- {
- DropInterface((struct Interface *)ITimer);
- }
-
+ if(ITimer) DropInterface((struct Interface *)ITimer);
CloseDevice((struct IORequest *) tioreq);
FreeSysObject(ASOT_IOREQUEST,tioreq);
FreeSysObject(ASOT_PORT,msgport);
@@ -214,41 +282,19 @@ void ami_schedule_close_timer(void)
nserror ami_schedule(int t, void (*callback)(void *p), void *p)
{
struct nscallback *nscb;
- struct TimeVal tv;
- ULONG time_us = 0;
-
- if(schedule_list == NULL)
- {
- return NSERROR_INIT_FAILED;
- }
- if(t < 0)
- {
- return schedule_remove(callback, p);
+ if(schedule_list == NULL) return NSERROR_INIT_FAILED;
+ if (t < 0) return schedule_remove(callback, p);
+
+ if (nscb = ami_schedule_locate(callback, p, false)) {
+ return ami_schedule_reschedule(nscb, t);
}
nscb = AllocVecTagList(sizeof(struct nscallback), NULL);
- if(!nscb)
- {
- return NSERROR_NOMEM;
- }
-
- time_us = t * 1000; /* t converted to µs */
-
- nscb->tv.Seconds = time_us / 1000000;
- nscb->tv.Microseconds = time_us % 1000000;
+ if(!nscb) return NSERROR_NOMEM;
- GetSysTime(&tv);
- AddTime(&nscb->tv,&tv); // now contains time when event occurs
-
- if(nscb->treq = AllocVecTagList(sizeof(struct TimeRequest), NULL))
- {
- *nscb->treq = *tioreq;
- nscb->treq->Request.io_Command=TR_ADDREQUEST;
- nscb->treq->Time.Seconds=nscb->tv.Seconds; // secs
- nscb->treq->Time.Microseconds=nscb->tv.Microseconds; // micro
- SendIO((struct IORequest *)nscb->treq);
- }
+ if (ami_schedule_add_timer_event(nscb, t) != NSERROR_OK)
+ return NSERROR_NOMEM;
nscb->callback = callback;
nscb->p = p;
@@ -257,3 +303,4 @@ nserror ami_schedule(int t, void (*callback)(void *p), void *p)
return NSERROR_OK;
}
+