summaryrefslogtreecommitdiff
path: root/src/libs/callback/callback.c
blob: e8ae8e9f04680b2b756c240065f0a07e4071ba42 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 *  Copyright 2006  Serge van den Boom <svdb@stack.nl>
 *
 *  This program 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; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "port.h"
#include "types.h"

#include <assert.h>
#include <stdlib.h>
#include <sys/types.h>

#include "libs/threadlib.h"

typedef struct CallbackLink CallbackLink;

#define CALLBACK_INTERNAL
#include "callback.h"

struct CallbackLink {
	CallbackLink *next;
	CallbackFunction callback;
	CallbackArg arg;
};

static CallbackLink *callbacks;
static CallbackLink **callbacksEnd;
static CallbackLink *const *callbacksProcessEnd;

static Mutex callbackListLock;

static inline void
CallbackList_lock(void) {
	LockMutex(callbackListLock);
}

static inline void
CallbackList_unlock(void) {
	UnlockMutex(callbackListLock);
}

#if 0
static inline bool
CallbackList_isLocked(void) {
	// TODO
}
#endif

void
Callback_init(void) {
	callbacks = NULL;
	callbacksEnd = &callbacks;
	callbacksProcessEnd = &callbacks;
	callbackListLock = CreateMutex("Callback List Lock", SYNC_CLASS_TOPLEVEL);
}

void
Callback_uninit(void) {
	// TODO: cleanup the queue?
	DestroyMutex (callbackListLock);
	callbackListLock = 0;
}

// Callbacks are guaranteed to be called in the order that they are queued.
CallbackID
Callback_add(CallbackFunction callback, CallbackArg arg) {
	CallbackLink *link = malloc(sizeof (CallbackLink));
	link->callback = callback;
	link->arg = arg;
	link->next = NULL;
		
	CallbackList_lock();
	*callbacksEnd = link;
	callbacksEnd = &link->next;
	CallbackList_unlock();
	return (CallbackID) link;
}


static void
CallbackLink_delete(CallbackLink *link) {
	free(link);
}

// Pre: CallbackList is locked.
static CallbackLink **
CallbackLink_find(CallbackLink *link) {
	CallbackLink **ptr;

	//assert(CallbackList_isLocked());
	for (ptr = &callbacks; *ptr != NULL; ptr = &(*ptr)->next) {
		if (*ptr == link)
			return ptr;
	}
	return NULL;
}

bool
Callback_remove(CallbackID id) {
	CallbackLink *link = (CallbackLink *) id;
	CallbackLink **linkPtr;

	CallbackList_lock();

	linkPtr = CallbackLink_find(link);
	if (linkPtr == NULL) {
		CallbackList_unlock();
		return false;
	}

	if (callbacksEnd == &(*linkPtr)->next)
		callbacksEnd = linkPtr;
	if (callbacksProcessEnd == &(*linkPtr)->next)
		callbacksProcessEnd = linkPtr;
	*linkPtr = (*linkPtr)->next;

	CallbackList_unlock();

	CallbackLink_delete(link);
	return true;
}

static inline void
CallbackLink_doCallback(CallbackLink *link) {
	(link->callback)(link->arg);
}

// Call all queued callbacks currently in the queue. Callbacks queued
// from inside the called functions will not be processed until the next
// call of Callback_process().
// It is allowed to remove callbacks from inside the called functions.
// NB: Callback_process() must never be called from more than one thread
//     at the same time. It's the only sensible way to ensure that the
//     callbacks are called in the order in which they were queued.
//     It is however allowed to call Callback_process() from inside the
//     callback function called by Callback_process() itself.
void
Callback_process(void) {
	CallbackLink *link;

	// We set 'callbacksProcessEnd' to callbacksEnd. Callbacks added
	// from inside a callback function will be placed after
	// callbacksProcessEnd, and will hence not be processed this
	// call of Callback_process().
	CallbackList_lock();
	callbacksProcessEnd = callbacksEnd;
	CallbackList_unlock();

	for (;;) {
		CallbackList_lock();
		if (callbacksProcessEnd == &callbacks) {
			CallbackList_unlock();
			break;
		}
		assert(callbacks != NULL);
				// If callbacks == NULL, then callbacksProcessEnd == &callbacks
		link = callbacks;
		callbacks = link->next;
		if (callbacksEnd == &link->next)
			callbacksEnd = &callbacks;
		if (callbacksProcessEnd == &link->next)
			callbacksProcessEnd = &callbacks;
		CallbackList_unlock();

		CallbackLink_doCallback(link);
		CallbackLink_delete(link);
	}
}

bool
Callback_haveMore(void) {
	bool result;

	CallbackList_lock();
	result = (callbacks != NULL);
	CallbackList_unlock();

	return result;
}