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"
30
31#include "utils/log.h"
32}
33
34/** Killable callback closure embodiment. */
35typedef struct {
36 void (*callback)(void *); /**< The callback function. */
37 void *context; /**< The context for the callback. */
38 bool callback_killed; /**< Whether or not this was killed. */
39 bool callback_fired; /**< Whether or not this has fired yet. */
40 bigtime_t timeout;
42
43/** List of all callbacks. */
44static BList *callbacks = NULL;
45
46/** earliest deadline. It's used for select() in gui_poll() */
47bigtime_t earliest_callback_timeout = B_INFINITE_TIMEOUT;
48
49
50static bool
51nsbeos_schedule_kill_callback(void *_target, void *_match)
52{
53 _nsbeos_callback_t *target = (_nsbeos_callback_t *)_target;
54 _nsbeos_callback_t *match = (_nsbeos_callback_t *)_match;
55 if ((target->callback == match->callback) &&
56 (target->context == match->context)) {
57 NSLOG(schedule, DEBUG,
58 "Found match for %p(%p), killing.",
59 target->callback,
60 target->context);
61 target->callback = NULL;
62 target->context = NULL;
63 target->callback_killed = true;
64 }
65 return false;
66}
67
68static void
69schedule_remove(void (*callback)(void *p), void *p)
70{
71 NSLOG(schedule, DEBUG,
72 "schedule_remove() for %p(%p)",
73 callback, p);
74 if (callbacks == NULL)
75 return;
76 _nsbeos_callback_t cb_match;
77 cb_match.callback = callback;
78 cb_match.context = p;
79
80 callbacks->DoForEach(nsbeos_schedule_kill_callback, &cb_match);
81}
82
83nserror beos_schedule(int t, void (*callback)(void *p), void *p)
84{
85 NSLOG(schedule, DEBUG, "t:%d cb:%p p:%p", t, callback, p);
86
87 if (callbacks == NULL) {
88 callbacks = new BList;
89 }
90
91 /* Kill any pending schedule of this kind. */
92 schedule_remove(callback, p);
93
94 if (t < 0) {
95 return NSERROR_OK;
96 }
97
98 bigtime_t timeout = system_time() + t * 1000LL;
100 cb->callback = callback;
101 cb->context = p;
102 cb->callback_killed = cb->callback_fired = false;
103 cb->timeout = timeout;
104 if (earliest_callback_timeout > timeout) {
106 }
107 callbacks->AddItem(cb);
108
109 return NSERROR_OK;
110}
111
112bool
114{
115 NSLOG(schedule, DEBUG, "schedule_run()");
116
117 earliest_callback_timeout = B_INFINITE_TIMEOUT;
118 if (callbacks == NULL)
119 return false; /* Nothing to do */
120
121 bigtime_t now = system_time();
122 int32 i;
123
124 NSLOG(schedule, DEBUG,
125 "Checking %ld callbacks to for deadline.",
126 callbacks->CountItems());
127
128 /* Run all the callbacks which made it this far. */
129 for (i = 0; i < callbacks->CountItems(); ) {
130 _nsbeos_callback_t *cb = (_nsbeos_callback_t *)(callbacks->ItemAt(i));
131 if (cb->timeout > now) {
132 // update next deadline
135 i++;
136 continue;
137 }
138 NSLOG(schedule, DEBUG,
139 "Running callbacks %p(%p).",
140 cb->callback,
141 cb->context);
142
143 if (!cb->callback_killed)
144 cb->callback(cb->context);
145 callbacks->RemoveItem(cb);
146 free(cb);
147 }
148 return true;
149}
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
#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:44
nserror beos_schedule(int t, void(*callback)(void *p), void *p)
Definition: schedule.cpp:83
bool schedule_run(void)
Process events up to current time.
Definition: schedule.cpp:113
static void schedule_remove(void(*callback)(void *p), void *p)
Definition: schedule.cpp:69
static bool nsbeos_schedule_kill_callback(void *_target, void *_match)
Definition: schedule.cpp:51
bigtime_t earliest_callback_timeout
earliest deadline.
Definition: schedule.cpp:47
Killable callback closure embodiment.
Definition: schedule.cpp:35
bool callback_killed
Whether or not this was killed.
Definition: schedule.cpp:38
bool callback_fired
Whether or not this has fired yet.
Definition: schedule.cpp:39
bigtime_t timeout
Definition: schedule.cpp:40
void * context
The context for the callback.
Definition: schedule.cpp:37
void(* callback)(void *)
The callback function.
Definition: schedule.cpp:36