1 /*****************************************************************************/
9 /* (C) 2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
45 /*****************************************************************************/
47 /*****************************************************************************/
52 Callback* Next; /* Next entry in list */
53 unsigned Ticks; /* Remaining ticks */
54 CallbackFunc UserFunc; /* User function */
55 void* UserData; /* User data */
58 /* Delta list that keeps existing callbacks */
59 static Callback* List = 0;
63 /*****************************************************************************/
64 /* Routines that handle the delta list */
65 /*****************************************************************************/
69 static void InsertCallback (Callback* C, unsigned Ticks)
70 /* Insert the callback C into the delta list */
72 /* Search for the insertion point */
75 while ((N = *L) != 0) {
76 /* Check if the next callback in the list has a higher wait time */
77 if (N->Ticks > Ticks) {
78 /* Insert before this callback */
82 /* Insert behind this callback */
88 /* Insert the new task */
96 static void RemoveCallback (Callback* C)
97 /* Remove a callback from the list. If the callback is not in the list, this
102 Callback** L = &List;
103 while ((N = *L) != 0) {
105 /* Found, remove it */
107 /* Adjust the counter of the following callback */
108 C->Next->Ticks += C->Ticks;
117 /* Callback was not found */
118 Internal ("RemoveCallback: Callback not found in list!");
123 /*****************************************************************************/
125 /*****************************************************************************/
129 Callback* NewCallback (unsigned Ticks, CallbackFunc Func, void* Data)
130 /* Create a callback for function F to be called in Ticks ticks. */
132 /* Allocate memory */
133 Callback* C = xmalloc (sizeof (Callback));
135 /* Initialize the fields */
139 /* Insert the callback into the delta list */
140 InsertCallback (C, Ticks);
142 /* Return the new callback */
148 void FreeCallback (Callback* C)
149 /* Delete a callback (remove from the queue) */
151 /* Remove the callback from the list */
160 void HandleCallbacks (unsigned TicksSinceLastCall)
161 /* Handle the callback queue */
165 /* Check if this one is due */
166 if (List->Ticks <= TicksSinceLastCall) {
168 /* Calclulate the tick offset */
169 int TickOffs = ((int) List->Ticks) - ((int) TicksSinceLastCall);
171 /* Retrieve the first callback from the list */
175 /* Call the user function */
176 C->UserFunc (TickOffs, C->UserData);
178 /* Delete the callback */
183 List->Ticks -= TicksSinceLastCall;