(list-character-sets): Completely
[bpt/emacs.git] / src / atimer.c
CommitLineData
e12489f9
GM
1/* Asynchronous timers.
2 Copyright (C) 2000 Free Software Foundation, Inc.
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
20
21#include <config.h>
22#include <lisp.h>
23#include <signal.h>
24#include <syssignal.h>
25#include <systime.h>
26#include <blockinput.h>
27#include <atimer.h>
28#include <stdio.h>
29
30#ifdef HAVE_UNISTD_H
31#include <unistd.h>
32#endif
33
34#ifdef HAVE_SYS_TIME_H
35#include <sys/time.h>
36#endif
37
38/* The ubiquitous min/max macros. */
39
40#define max(X, Y) ((X) > (Y) ? (X) : (Y))
41#define min(X, Y) ((X) < (Y) ? (X) : (Y))
42
43/* Free-list of atimer structures. */
44
45static struct atimer *free_atimers;
46
9c2e3e8d
GM
47/* List of currently not running timers due to a call to
48 lock_atimer. */
49
50static struct atimer *stopped_atimers;
51
e12489f9
GM
52/* List of active atimers, sorted by expiration time. The timer that
53 will become ripe next is always at the front of this list. */
54
55static struct atimer *atimers;
56
57/* Non-zero means alarm_signal_handler has found ripe timers but
58 interrupt_input_blocked was non-zero. In this case, timer
59 functions are not called until the next UNBLOCK_INPUT because timer
60 functions are expected to call X, and X cannot be assumed to be
61 reentrant. */
62
63int pending_atimers;
64
65/* Block/unblock SIGALRM.. */
66
67#define BLOCK_ATIMERS sigblock (sigmask (SIGALRM))
68#define UNBLOCK_ATIMERS sigunblock (sigmask (SIGALRM))
69
70/* Function prototypes. */
71
72static void set_alarm P_ ((void));
73static void schedule_atimer P_ ((struct atimer *));
74
75
76/* Start a new atimer of type TYPE. TIME specifies when the timer is
77 ripe. FN is the function to call when the timer fires.
78 CLIENT_DATA is stored in the client_data member of the atimer
79 structure returned and so made available to FN when it is called.
80
81 If TYPE is ATIMER_ABSOLUTE, TIME is the absolute time at which the
82 timer fires.
83
84 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the
85 future.
86
87 In both cases, the timer is automatically freed after it has fired.
88
89 If TYPE is ATIMER_CONTINUOUS, the timer fires every TIME s/us.
90
91 Value is a pointer to the atimer started. It can be used in calls
92 to cancel_atimer; don't free it yourself. */
93
94struct atimer *
95start_atimer (type, time, fn, client_data)
96 enum atimer_type type;
97 EMACS_TIME time;
98 atimer_callback fn;
99 void *client_data;
100{
101 struct atimer *t;
102
9c2e3e8d
GM
103 /* May not be called when some timers are stopped. */
104 if (stopped_atimers)
105 abort ();
106
e12489f9
GM
107 /* Round TIME up to the next full second if we don't have
108 itimers. */
109#ifndef HAVE_SETITIMER
110 if (EMACS_USECS (time) != 0)
111 {
4ce94f99
EZ
112 EMACS_SET_USECS (time, 0);
113 EMACS_SET_SECS (time, EMACS_SECS (time) + 1);
e12489f9
GM
114 }
115#endif /* not HAVE_SETITIMER */
116
117 /* Get an atimer structure from the free-list, or allocate
118 a new one. */
119 if (free_atimers)
120 {
121 t = free_atimers;
122 free_atimers = t->next;
123 }
124 else
125 t = (struct atimer *) xmalloc (sizeof *t);
126
127 /* Fill the atimer structure. */
128 bzero (t, sizeof *t);
129 t->type = type;
130 t->fn = fn;
131 t->client_data = client_data;
132
133 BLOCK_ATIMERS;
134
135 /* Compute the timer's expiration time. */
136 switch (type)
137 {
138 case ATIMER_ABSOLUTE:
139 t->expiration = time;
140 break;
141
142 case ATIMER_RELATIVE:
143 EMACS_GET_TIME (t->expiration);
144 EMACS_ADD_TIME (t->expiration, t->expiration, time);
145 break;
146
147 case ATIMER_CONTINUOUS:
148 EMACS_GET_TIME (t->expiration);
149 EMACS_ADD_TIME (t->expiration, t->expiration, time);
150 t->interval = time;
151 break;
152 }
153
154 /* Insert the timer in the list of active atimers. */
155 schedule_atimer (t);
156 UNBLOCK_ATIMERS;
157
158 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
159 set_alarm ();
160
161 return t;
162}
163
164
165/* Cancel and free atimer TIMER. */
166
167void
168cancel_atimer (timer)
169 struct atimer *timer;
170{
171 struct atimer *t, *prev;
94870977 172 struct atimer **list;
9c2e3e8d 173
e12489f9
GM
174 BLOCK_ATIMERS;
175
94870977
GM
176 /* If we've stopped all other timers except TIMER, we can
177 just reset the list of active atimers to null. */
178 if (stopped_atimers && timer == atimers)
e12489f9 179 {
94870977
GM
180 timer->next = free_atimers;
181 free_atimers = timer;
182 atimers = NULL;
183 }
184 else
185 {
186 /* See if TIMER is active or stopped. */
187 list = stopped_atimers ? &stopped_atimers : &atimers;
188 for (t = *list, prev = 0; t && t != timer; t = t->next)
189 ;
190
191 /* If it is, take it off the list of its list, and put in on the
192 free-list. We don't bother to arrange for setting a
193 different alarm time, since a too early one doesn't hurt. */
194 if (t)
195 {
196 if (prev)
197 prev->next = t->next;
198 else
199 *list = t->next;
200
201 t->next = free_atimers;
202 free_atimers = t;
203 }
e12489f9
GM
204 }
205
206 UNBLOCK_ATIMERS;
207}
208
209
9c2e3e8d
GM
210/* Stop all timers except timer T. T null means stop all timers.
211 This function may only be called when all timers are running. Two
212 calls of this function in a row will lead to an abort. You may not
213 call cancel_atimer or start_atimer while timers are stopped. */
214
215void
216stop_other_atimers (t)
217 struct atimer *t;
218{
219 BLOCK_ATIMERS;
220
221 if (stopped_atimers)
222 abort ();
223
224 if (t)
225 {
2503c8b1
GM
226 struct atimer *p, *prev;
227
228 /* See if T is active. */
229 for (p = atimers, prev = 0; p && p != t; p = p->next)
230 ;
231
232 if (p == t)
233 {
234 if (prev)
235 prev->next = t->next;
236 else
237 atimers = t->next;
238 t->next = NULL;
239 }
240 else
241 /* T is not active. Let's handle this like T == 0. */
242 t = NULL;
9c2e3e8d
GM
243 }
244
245 stopped_atimers = atimers;
246 atimers = t;
247 UNBLOCK_ATIMERS;
248}
249
250
251/* Run all timers again, if some have been stopped with a call to
252 stop_other_atimers. */
253
254void
255run_all_atimers ()
256{
257 if (stopped_atimers)
258 {
259 struct atimer *t = atimers;
260 BLOCK_ATIMERS;
261 atimers = stopped_atimers;
262 stopped_atimers = NULL;
263 if (t)
264 schedule_atimer (t);
265 UNBLOCK_ATIMERS;
266 }
267}
268
269
270/* A version of run_all_timers suitable for a record_unwind_protect. */
271
272Lisp_Object
273unwind_stop_other_atimers (dummy)
274 Lisp_Object dummy;
275{
276 run_all_atimers ();
277 return Qnil;
278}
279
280
e12489f9
GM
281/* Arrange for a SIGALRM to arrive when the next timer is ripe. */
282
283static void
284set_alarm ()
285{
e12489f9
GM
286#if defined (USG) && !defined (POSIX_SIGNALS)
287 /* USG systems forget handlers when they are used;
288 must reestablish each time. */
289 signal (SIGALRM, alarm_signal_handler);
290#endif /* USG */
291
292 if (atimers)
293 {
294 EMACS_TIME now, time;
295#ifdef HAVE_SETITIMER
296 struct itimerval it;
297#endif
298
299 /* Determine s/us till the next timer is ripe. */
300 EMACS_GET_TIME (now);
301 EMACS_SUB_TIME (time, atimers->expiration, now);
302
303#ifdef HAVE_SETITIMER
304 /* Don't set the interval to 0; this disables the timer. */
305 if (EMACS_TIME_LE (atimers->expiration, now))
306 {
307 EMACS_SET_SECS (time, 0);
308 EMACS_SET_USECS (time, 1000);
309 }
310
311 bzero (&it, sizeof it);
312 it.it_value = time;
313 setitimer (ITIMER_REAL, &it, 0);
314#else /* not HAVE_SETITIMER */
315 alarm (max (EMACS_SECS (time), 1));
316#endif /* not HAVE_SETITIMER */
317 }
318}
319
320
321/* Insert timer T into the list of active atimers `atimers', keeping
322 the list sorted by expiration time. T must not be in this list
323 already. */
324
325static void
326schedule_atimer (t)
327 struct atimer *t;
328{
329 struct atimer *a = atimers, *prev = NULL;
330
331 /* Look for the first atimer that is ripe after T. */
332 while (a && EMACS_TIME_GT (t->expiration, a->expiration))
333 prev = a, a = a->next;
334
335 /* Insert T in front of the atimer found, if any. */
336 if (prev)
337 prev->next = t;
338 else
339 atimers = t;
340
341 t->next = a;
342}
343
344
345/* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
346 SIGALRM. */
347
348SIGTYPE
349alarm_signal_handler (signo)
350 int signo;
351{
352 EMACS_TIME now;
353
354 EMACS_GET_TIME (now);
355 pending_atimers = 0;
356
357 while (atimers
358 && (pending_atimers = interrupt_input_blocked) == 0
359 && EMACS_TIME_LE (atimers->expiration, now))
360 {
361 struct atimer *t;
362
363 t = atimers;
364 atimers = atimers->next;
365 t->fn (t);
366
367 if (t->type == ATIMER_CONTINUOUS)
368 {
369 EMACS_ADD_TIME (t->expiration, now, t->interval);
370 schedule_atimer (t);
371 }
372 else
373 {
374 t->next = free_atimers;
375 free_atimers = t;
376 }
377
378 EMACS_GET_TIME (now);
379 }
380
381#if defined (USG) && !defined (POSIX_SIGNALS)
382 /* USG systems forget handlers when they are used;
383 must reestablish each time. */
384 signal (SIGALRM, alarm_signal_handler);
385#endif /* USG */
386
387 set_alarm ();
388}
389
390
391/* Call alarm_signal_handler for pending timers. */
392
393void
394do_pending_atimers ()
395{
396 if (pending_atimers)
397 {
398 BLOCK_ATIMERS;
399 alarm_signal_handler (SIGALRM);
400 UNBLOCK_ATIMERS;
401 }
402}
403
404
405/* Turn alarms on/off. This seems to be temporarily necessary on
406 some systems like HPUX (see process.c). */
407
408void
409turn_on_atimers (on)
410 int on;
411{
412 if (on)
413 {
414 signal (SIGALRM, alarm_signal_handler);
415 set_alarm ();
416 }
417 else
418 alarm (0);
419}
420
421
422void
423init_atimer ()
424{
425 free_atimers = atimers = NULL;
426 pending_atimers = 0;
427 signal (SIGALRM, alarm_signal_handler);
428}