NetSurf
schedule.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2008 François Revol <mmu_man@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#define __STDBOOL_H__ 1
20#include <stdlib.h>
21#include <stdbool.h>
22#include <OS.h>
23#include <List.h>
24
25extern "C" {
26#include "utils/errors.h"
27#include "beos/schedule.h"
28#include "netsurf/inttypes.h"
31
32#include "utils/log.h"
33}
34
35/** Killable callback closure embodiment. */
36typedef struct {
37 void (*callback)(void *); /**< The callback function. */
38 void *context; /**< The context for the callback. */
39 bool callback_killed; /**< Whether or not this was killed. */
40 bool callback_fired; /**< Whether or not this has fired yet. */
41 bigtime_t timeout;
43
44/** List of all callbacks. */
45static BList *callbacks = NULL;
46
47/** earliest deadline. It's used for select() in gui_poll() */
48bigtime_t earliest_callback_timeout = B_INFINITE_TIMEOUT;
49
50
51static bool
52nsbeos_schedule_kill_callback(void *_target, void *_match)
53{
54 _nsbeos_callback_t *target = (_nsbeos_callback_t *)_target;
55 _nsbeos_callback_t *match = (_nsbeos_callback_t *)_match;
56 if ((target->callback == match->callback) &&
57 (target->context == match->context)) {
58 NSLOG(schedule, DEBUG,
59 "Found match for %p(%p), killing.",
60 target->callback,
61 target->context);
62 target->callback = NULL;
63 target->context = NULL;
64 target->callback_killed = true;
65 }
66 return false;
67}
68
69static void
70schedule_remove(void (*callback)(void *p), void *p)
71{
72 NSLOG(schedule, DEBUG,
73 "schedule_remove() for %p(%p)",
74 callback, p);
75 if (callbacks == NULL)
76 return;
77 _nsbeos_callback_t cb_match;
78 cb_match.callback = callback;
79 cb_match.context = p;
80
81 callbacks->DoForEach(nsbeos_schedule_kill_callback, &cb_match);
82}
83
84nserror beos_schedule(int t, void (*callback)(void *p), void *p)
85{
86 NSLOG(schedule, DEBUG, "t:%d cb:%p p:%p", t, callback, p);
87
88 if (callbacks == NULL) {
89 callbacks = new BList;
90 }
91
92 /* Kill any pending schedule of this kind. */
93 schedule_remove(callback, p);
94
95 if (t < 0) {
96 return NSERROR_OK;
97 }
98
99 bigtime_t timeout = system_time() + t * 1000LL;
101 cb->callback = callback;
102 cb->context = p;
103 cb->callback_killed = cb->callback_fired = false;
104 cb->timeout = timeout;
105 if (earliest_callback_timeout > timeout) {
107 }
108 callbacks->AddItem(cb);
109
110 return NSERROR_OK;
111}
112
113bool
115{
116 NSLOG(schedule, DEBUG, "schedule_run()");
117
118 earliest_callback_timeout = B_INFINITE_TIMEOUT;
119 if (callbacks == NULL)
120 return false; /* Nothing to do */
121
122 bigtime_t now = system_time();
123 int32 i;
124
125 NSLOG(schedule, DEBUG,
126 "Checking %" PRId32 " callbacks to for deadline.",
127 callbacks->CountItems());
128
129 /* Run all the callbacks which made it this far. */
130 for (i = 0; i < callbacks->CountItems(); ) {
131 _nsbeos_callback_t *cb = (_nsbeos_callback_t *)(callbacks->ItemAt(i));
132 if (cb->timeout > now) {
133 // update next deadline
136 i++;
137 continue;
138 }
139 NSLOG(schedule, DEBUG,
140 "Running callbacks %p(%p).",
141 cb->callback,
142 cb->context);
143
144 if (!cb->callback_killed)
145 cb->callback(cb->context);
146 callbacks->RemoveItem(cb);
147 free(cb);
148 }
149 return true;
150}
Browser window creation and manipulation interface.
Declaration of content enumerations.
Error codes.
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_OK
No error.
Definition: errors.h:30
Netsurf additional integer type formatting macros.
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
int32_t int32
Definition: os3support.h:183
static BList * callbacks
List of all callbacks.
Definition: schedule.cpp:45
nserror beos_schedule(int t, void(*callback)(void *p), void *p)
Definition: schedule.cpp:84
bool schedule_run(void)
Process events up to current time.
Definition: schedule.cpp:114
static void schedule_remove(void(*callback)(void *p), void *p)
Definition: schedule.cpp:70
static bool nsbeos_schedule_kill_callback(void *_target, void *_match)
Definition: schedule.cpp:52
bigtime_t earliest_callback_timeout
earliest deadline.
Definition: schedule.cpp:48
Killable callback closure embodiment.
Definition: schedule.cpp:36
bool callback_killed
Whether or not this was killed.
Definition: schedule.cpp:39
bool callback_fired
Whether or not this has fired yet.
Definition: schedule.cpp:40
bigtime_t timeout
Definition: schedule.cpp:41
void * context
The context for the callback.
Definition: schedule.cpp:38
void(* callback)(void *)
The callback function.
Definition: schedule.cpp:37