4 * Calling routines at set times
6 * © 1994-1998 Straylight
9 /*----- Licensing note ----------------------------------------------------*
11 * This file is part of Straylight's Steel library.
13 * Steel is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * Steel is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with Steel. If not, write to the Free Software Foundation,
25 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 typedef struct alarm__str
38 struct alarm__str *next;
45 static alarm__str *alarm__all;
48 * void alarm_init(void)
56 /* Hmmm.... this *is* an interesting function */
60 * int alarm_timenow(void)
63 * Reports the time right now
66 int alarm_timenow(void)
68 return (_swi(OS_ReadMonotonicTime,_return(0)));
72 * int alarm_timedifference(int t1,int t2)
75 * Tells you the difference between two times. t2 is considered to be later
79 int alarm_timedifference(int t1,int t2)
81 /* --- Humm.... How to deal with wraparound --- *
83 * Answer: Ignore it and subtract -- mod 2^32 arithmetic + 2s complement
84 * sorts it all out for you.
91 * void alarm_set(int at,alarm_handler proc,void *handle)
94 * Sets up `proc' to be called at time `at', being passed `handle'.
97 void alarm_set(int at,alarm_handler proc,void *handle)
99 alarm__str *a=mem_alloc(sizeof(alarm__str));
102 werr(FALSE,msgs_lookup("alarmNEM:Not enough memory for alarm"));
105 /* --- Fill in the structure --- */
111 /* --- Scan the list and put the new node in the right place --- *
113 * Is this the right condition for the times?
116 p=(alarm__str *)&alarm__all;
117 while (p->next && p->next->time<at)
125 * void alarm_remove(int at,void *handle)
128 * Removes an alarm identified by a time and a handle
131 void alarm_remove(int at,void *handle)
136 p=(alarm__str *)&alarm__all;
139 if (p->next->time==at && p->next->handle==handle)
151 * void alarm_removeall(void *handle)
154 * Removes all alarms for the given handle
157 void alarm_removeall(void *handle)
162 p=(alarm__str *)&alarm__all;
165 if (p->next->handle==handle)
177 * BOOL alarm_anypending(void *handle)
180 * Returns TRUE if there are alarms for the given handle
183 BOOL alarm_anypending(void *handle)
185 alarm__str *p=alarm__all;
189 if (p->handle==handle)
197 * BOOL alarm_next(int *when)
200 * Informs the caller (a) if there are any alarms waiting, and (b) when
204 * int *when == where to put the next time for an alarm (unchanged if no
208 * TRUE if there are any alarms left
211 BOOL alarm_next(int *when)
215 *when=alarm__all->time;
223 * void alarm_callnext(void)
226 * Calls the next alarm and removes it from the list
229 void alarm_callnext(void)
237 mem_free(alarm__all);
239 a.proc(a.time,a.handle);