NetSurf
frontends
riscos
schedule.c
Go to the documentation of this file.
1
/*
2
* Copyright 2004 James Bursa <bursa@users.sourceforge.net>
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
/** \file
20
* Scheduled callback queue (implementation).
21
*
22
* The queue is simply implemented as a linked list.
23
*/
24
25
#include <stdbool.h>
26
#include <stdlib.h>
27
28
#include "oslib/os.h"
29
#include "
utils/log.h
"
30
31
#include "
riscos/gui.h
"
32
33
34
/** Entry in the queue of scheduled callbacks. */
35
struct
sched_entry
{
36
/** Preferred time for callback. */
37
os_t
time
;
38
/** Function to call at the specified time. */
39
void (*
callback
)(
void
*
p
);
40
/** User parameter for callback. */
41
void
*
p
;
42
/** Next (later) entry in queue. */
43
struct
sched_entry
*
next
;
44
};
45
46
/** Queue of scheduled callbacks (sentinel at head). */
47
static
struct
sched_entry
sched_queue
= { 0, 0, 0, 0 };
48
49
/** Items have been scheduled. */
50
bool
sched_active
=
false
;
51
/** Time of soonest scheduled event (valid only if sched_active is true). */
52
os_t
sched_time
;
53
54
/**
55
* Unschedule a callback.
56
*
57
* \param callback callback function
58
* \param p user parameter, passed to callback function
59
*
60
* All scheduled callbacks matching both callback and p are removed.
61
*/
62
63
static
nserror
schedule_remove
(
void
(*
callback
)(
void
*
p
),
void
*
p
)
64
{
65
struct
sched_entry
*entry, *prev;
66
67
prev = &
sched_queue
;
68
69
for
(entry = prev->
next
; entry; entry = prev->
next
) {
70
if
(entry->
callback
!=
callback
|| entry->
p
!=
p
) {
71
prev = entry;
72
continue
;
73
}
74
75
prev->
next
= entry->
next
;
76
free(entry);
77
78
/* There can only ever be one match, and we've found it */
79
break
;
80
}
81
82
if
(
sched_queue
.
next
) {
83
sched_active
=
true
;
84
sched_time
=
sched_queue
.
next
->
time
;
85
}
else
{
86
sched_active
=
false
;
87
}
88
89
return
NSERROR_OK
;
90
}
91
92
/* exported function documented in riscos/gui.h */
93
nserror
riscos_schedule
(
int
t,
void
(*
callback
)(
void
*
p
),
void
*
p
)
94
{
95
struct
sched_entry
*entry;
96
struct
sched_entry
*queue;
97
os_t
time
;
98
nserror
ret;
99
100
ret =
schedule_remove
(
callback
,
p
);
101
if
((t < 0) || (ret !=
NSERROR_OK
)) {
102
return
ret;
103
}
104
105
t = t / 10;
/* convert to centiseconds */
106
107
time
= os_read_monotonic_time() + t;
108
109
entry = malloc(
sizeof
*entry);
110
if
(!entry) {
111
NSLOG
(netsurf, INFO,
"malloc failed"
);
112
return
NSERROR_NOMEM
;
113
}
114
115
entry->
time
=
time
;
116
entry->
callback
=
callback
;
117
entry->
p
=
p
;
118
119
for
(queue = &
sched_queue
;
120
queue->
next
&& queue->
next
->
time
<=
time
;
121
queue = queue->
next
)
122
;
123
entry->
next
= queue->
next
;
124
queue->
next
= entry;
125
126
sched_active
=
true
;
127
sched_time
=
sched_queue
.
next
->
time
;
128
129
return
NSERROR_OK
;
130
}
131
132
133
/* exported function documented in riscos/gui.h */
134
bool
schedule_run
(
void
)
135
{
136
struct
sched_entry
*entry;
137
os_t now;
138
139
now = os_read_monotonic_time();
140
141
while
(
sched_queue
.
next
&&
sched_queue
.
next
->
time
<= now) {
142
void (*
callback
)(
void
*
p
);
143
void
*
p
;
144
145
entry =
sched_queue
.
next
;
146
callback
= entry->
callback
;
147
p
= entry->
p
;
148
sched_queue
.
next
= entry->
next
;
149
free(entry);
150
/* The callback may call riscos_schedule(), so leave
151
* the queue in a safe state.
152
*/
153
callback
(
p
);
154
}
155
156
if
(
sched_queue
.
next
) {
157
sched_active
=
true
;
158
sched_time
=
sched_queue
.
next
->
time
;
159
}
else
160
sched_active
=
false
;
161
162
return
sched_active
;
163
}
schedule_run
int schedule_run(void)
Process events up to current time.
Definition:
schedule.c:137
nserror
nserror
Enumeration of error codes.
Definition:
errors.h:29
NSERROR_NOMEM
@ NSERROR_NOMEM
Memory exhaustion.
Definition:
errors.h:32
NSERROR_OK
@ NSERROR_OK
No error.
Definition:
errors.h:30
log.h
NSLOG
#define NSLOG(catname, level, logmsg, args...)
Definition:
log.h:116
gui.h
sched_time
os_t sched_time
Time of soonest scheduled event (valid only if sched_active is true).
Definition:
schedule.c:52
riscos_schedule
nserror riscos_schedule(int t, void(*callback)(void *p), void *p)
Schedule a callback.
Definition:
schedule.c:93
sched_active
bool sched_active
Items have been scheduled.
Definition:
schedule.c:50
sched_queue
static struct sched_entry sched_queue
Queue of scheduled callbacks (sentinel at head).
Definition:
schedule.c:47
schedule_remove
static nserror schedule_remove(void(*callback)(void *p), void *p)
Unschedule a callback.
Definition:
schedule.c:63
sched_entry
Entry in the queue of scheduled callbacks.
Definition:
schedule.c:35
sched_entry::callback
void(* callback)(void *p)
Function to call at the specified time.
Definition:
schedule.c:39
sched_entry::next
struct sched_entry * next
Next (later) entry in queue.
Definition:
schedule.c:43
sched_entry::p
void * p
User parameter for callback.
Definition:
schedule.c:41
sched_entry::time
os_t time
Preferred time for callback.
Definition:
schedule.c:37
Generated on Tue Nov 12 2024 23:39:51 for NetSurf by
1.9.4