summaryrefslogtreecommitdiff
path: root/frontends/riscos/schedule.c
blob: 54308b7a9da0896bb9fe91f94451b7a784f094d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright 2004 James Bursa <bursa@users.sourceforge.net>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file
 * Scheduled callback queue (implementation).
 *
 * The queue is simply implemented as a linked list.
 */

#include <stdbool.h>
#include <stdlib.h>

#include "oslib/os.h"
#include "utils/log.h"

#include "riscos/gui.h"


/** Entry in the queue of scheduled callbacks. */
struct sched_entry {
	/** Preferred time for callback. */
	os_t time;
	/** Function to call at the specified time. */
	void (*callback)(void *p);
	/** User parameter for callback. */
	void *p;
	/** Next (later) entry in queue. */
	struct sched_entry *next;
};

/** Queue of scheduled callbacks (sentinel at head). */
static struct sched_entry sched_queue = { 0, 0, 0, 0 };

/** Items have been scheduled. */
bool sched_active = false;
/** Time of soonest scheduled event (valid only if sched_active is true). */
os_t sched_time;

/**
 * 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)
{
	struct sched_entry *entry, *prev;

	prev = &sched_queue;

	for (entry = prev->next; entry; entry = prev->next) {
		if (entry->callback != callback || entry->p != p) {
			prev = entry;
			continue;
		}

		prev->next = entry->next;
		free(entry);

		/* There can only ever be one match, and we've found it */
		break;
	}

	if (sched_queue.next) {
		sched_active = true;
		sched_time = sched_queue.next->time;
	} else {
		sched_active = false;
	}

	return NSERROR_OK;
}

/* exported function documented in riscos/gui.h */
nserror riscos_schedule(int t, void (*callback)(void *p), void *p)
{
	struct sched_entry *entry;
	struct sched_entry *queue;
	os_t time;
	nserror ret;

	ret = schedule_remove(callback, p);
	if ((t < 0) || (ret != NSERROR_OK)) {
		return ret;
	}

	t = t / 10; /* convert to centiseconds */

	time = os_read_monotonic_time() + t;

	entry = malloc(sizeof *entry);
	if (!entry) {
		LOG("malloc failed");
		return NSERROR_NOMEM;
	}

	entry->time = time;
	entry->callback = callback;
	entry->p = p;

	for (queue = &sched_queue;
			queue->next && queue->next->time <= time;
			queue = queue->next)
		;
	entry->next = queue->next;
	queue->next = entry;

	sched_active = true;
	sched_time = sched_queue.next->time;

	return NSERROR_OK;
}


/* exported function documented in riscos/gui.h */
bool schedule_run(void)
{
	struct sched_entry *entry;
	os_t now;

	now = os_read_monotonic_time();

	while (sched_queue.next && sched_queue.next->time <= now) {
		void (*callback)(void *p);
		void *p;

		entry = sched_queue.next;
		callback = entry->callback;
		p = entry->p;
		sched_queue.next = entry->next;
		free(entry);
		/* The callback may call riscos_schedule(), so leave
		 * the queue in a safe state.
		 */
		callback(p);
	}

	if (sched_queue.next) {
		sched_active = true;
		sched_time = sched_queue.next->time;
	} else
		sched_active = false;

        return sched_active;
}