(start_atimer): Don't abort when timers are stopped.
[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 *));
50b1039f
GM
74static struct atimer *append_atimer_lists P_ ((struct atimer *,
75 struct atimer *));
e12489f9
GM
76
77
78/* Start a new atimer of type TYPE. TIME specifies when the timer is
79 ripe. FN is the function to call when the timer fires.
80 CLIENT_DATA is stored in the client_data member of the atimer
81 structure returned and so made available to FN when it is called.
82
83 If TYPE is ATIMER_ABSOLUTE, TIME is the absolute time at which the
84 timer fires.
85
86 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the
87 future.
88
89 In both cases, the timer is automatically freed after it has fired.
90
91 If TYPE is ATIMER_CONTINUOUS, the timer fires every TIME s/us.
92
93 Value is a pointer to the atimer started. It can be used in calls
94 to cancel_atimer; don't free it yourself. */
95
96struct atimer *
97start_atimer (type, time, fn, client_data)
98 enum atimer_type type;
99 EMACS_TIME time;
100 atimer_callback fn;
101 void *client_data;
102{
103 struct atimer *t;
104
105 /* Round TIME up to the next full second if we don't have
106 itimers. */
107#ifndef HAVE_SETITIMER
108 if (EMACS_USECS (time) != 0)
109 {
4ce94f99
EZ
110 EMACS_SET_USECS (time, 0);
111 EMACS_SET_SECS (time, EMACS_SECS (time) + 1);
e12489f9
GM
112 }
113#endif /* not HAVE_SETITIMER */
114
115 /* Get an atimer structure from the free-list, or allocate
116 a new one. */
117 if (free_atimers)
118 {
119 t = free_atimers;
120 free_atimers = t->next;
121 }
122 else
123 t = (struct atimer *) xmalloc (sizeof *t);
124
125 /* Fill the atimer structure. */
126 bzero (t, sizeof *t);
127 t->type = type;
128 t->fn = fn;
129 t->client_data = client_data;
130
131 BLOCK_ATIMERS;
132
133 /* Compute the timer's expiration time. */
134 switch (type)
135 {
136 case ATIMER_ABSOLUTE:
137 t->expiration = time;
138 break;
139
140 case ATIMER_RELATIVE:
141 EMACS_GET_TIME (t->expiration);
142 EMACS_ADD_TIME (t->expiration, t->expiration, time);
143 break;
144
145 case ATIMER_CONTINUOUS:
146 EMACS_GET_TIME (t->expiration);
147 EMACS_ADD_TIME (t->expiration, t->expiration, time);
148 t->interval = time;
149 break;
150 }
151
152 /* Insert the timer in the list of active atimers. */
153 schedule_atimer (t);
154 UNBLOCK_ATIMERS;
155
156 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
157 set_alarm ();
158
159 return t;
160}
161
162
163/* Cancel and free atimer TIMER. */
164
165void
166cancel_atimer (timer)
167 struct atimer *timer;
168{
50b1039f
GM
169 int i;
170
e12489f9
GM
171 BLOCK_ATIMERS;
172
50b1039f 173 for (i = 0; i < 2; ++i)
94870977 174 {
50b1039f
GM
175 struct atimer *t, *prev;
176 struct atimer **list = i ? &stopped_atimers : &atimers;
177
94870977 178 /* See if TIMER is active or stopped. */
94870977
GM
179 for (t = *list, prev = 0; t && t != timer; t = t->next)
180 ;
181
50b1039f 182 /* If it is, take it off the its list, and put in on the
94870977
GM
183 free-list. We don't bother to arrange for setting a
184 different alarm time, since a too early one doesn't hurt. */
185 if (t)
186 {
187 if (prev)
188 prev->next = t->next;
189 else
190 *list = t->next;
191
192 t->next = free_atimers;
193 free_atimers = t;
194 }
e12489f9
GM
195 }
196
197 UNBLOCK_ATIMERS;
198}
199
200
50b1039f
GM
201/* Append two lists of atimers LIST1 and LIST2 and return the
202 result list. */
203
204static struct atimer *
205append_atimer_lists (list1, list2)
206 struct atimer *list1, *list2;
207{
208 if (list1 == NULL)
209 return list2;
210 else if (list2 == NULL)
211 return list1;
212 else
213 {
214 struct atimer *p;
215
216 for (p = list1; p->next; p = p->next)
217 ;
218 p->next = list2;
219 return list1;
220 }
221}
222
223
224/* Stop all timers except timer T. T null means stop all timers. */
9c2e3e8d
GM
225
226void
227stop_other_atimers (t)
228 struct atimer *t;
229{
230 BLOCK_ATIMERS;
231
9c2e3e8d
GM
232 if (t)
233 {
2503c8b1
GM
234 struct atimer *p, *prev;
235
236 /* See if T is active. */
237 for (p = atimers, prev = 0; p && p != t; p = p->next)
238 ;
239
240 if (p == t)
241 {
242 if (prev)
243 prev->next = t->next;
244 else
245 atimers = t->next;
246 t->next = NULL;
247 }
248 else
249 /* T is not active. Let's handle this like T == 0. */
250 t = NULL;
9c2e3e8d
GM
251 }
252
50b1039f 253 stopped_atimers = append_atimer_lists (atimers, stopped_atimers);
9c2e3e8d
GM
254 atimers = t;
255 UNBLOCK_ATIMERS;
256}
257
258
259/* Run all timers again, if some have been stopped with a call to
260 stop_other_atimers. */
261
262void
263run_all_atimers ()
264{
265 if (stopped_atimers)
266 {
267 struct atimer *t = atimers;
50b1039f
GM
268 struct atimer *next;
269
9c2e3e8d
GM
270 BLOCK_ATIMERS;
271 atimers = stopped_atimers;
272 stopped_atimers = NULL;
50b1039f
GM
273
274 while (t)
275 {
276 next = t->next;
277 schedule_atimer (t);
278 t = next;
279 }
280
9c2e3e8d
GM
281 UNBLOCK_ATIMERS;
282 }
283}
284
285
286/* A version of run_all_timers suitable for a record_unwind_protect. */
287
288Lisp_Object
289unwind_stop_other_atimers (dummy)
290 Lisp_Object dummy;
291{
292 run_all_atimers ();
293 return Qnil;
294}
295
296
e12489f9
GM
297/* Arrange for a SIGALRM to arrive when the next timer is ripe. */
298
299static void
300set_alarm ()
301{
e12489f9
GM
302#if defined (USG) && !defined (POSIX_SIGNALS)
303 /* USG systems forget handlers when they are used;
304 must reestablish each time. */
305 signal (SIGALRM, alarm_signal_handler);
306#endif /* USG */
307
308 if (atimers)
309 {
310 EMACS_TIME now, time;
311#ifdef HAVE_SETITIMER
312 struct itimerval it;
313#endif
314
315 /* Determine s/us till the next timer is ripe. */
316 EMACS_GET_TIME (now);
317 EMACS_SUB_TIME (time, atimers->expiration, now);
318
319#ifdef HAVE_SETITIMER
320 /* Don't set the interval to 0; this disables the timer. */
321 if (EMACS_TIME_LE (atimers->expiration, now))
322 {
323 EMACS_SET_SECS (time, 0);
324 EMACS_SET_USECS (time, 1000);
325 }
326
327 bzero (&it, sizeof it);
328 it.it_value = time;
329 setitimer (ITIMER_REAL, &it, 0);
330#else /* not HAVE_SETITIMER */
331 alarm (max (EMACS_SECS (time), 1));
332#endif /* not HAVE_SETITIMER */
333 }
334}
335
336
337/* Insert timer T into the list of active atimers `atimers', keeping
338 the list sorted by expiration time. T must not be in this list
339 already. */
340
341static void
342schedule_atimer (t)
343 struct atimer *t;
344{
345 struct atimer *a = atimers, *prev = NULL;
346
347 /* Look for the first atimer that is ripe after T. */
348 while (a && EMACS_TIME_GT (t->expiration, a->expiration))
349 prev = a, a = a->next;
350
351 /* Insert T in front of the atimer found, if any. */
352 if (prev)
353 prev->next = t;
354 else
355 atimers = t;
356
357 t->next = a;
358}
359
360
361/* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
362 SIGALRM. */
363
364SIGTYPE
365alarm_signal_handler (signo)
366 int signo;
367{
368 EMACS_TIME now;
369
370 EMACS_GET_TIME (now);
371 pending_atimers = 0;
372
373 while (atimers
374 && (pending_atimers = interrupt_input_blocked) == 0
375 && EMACS_TIME_LE (atimers->expiration, now))
376 {
377 struct atimer *t;
378
379 t = atimers;
380 atimers = atimers->next;
381 t->fn (t);
382
383 if (t->type == ATIMER_CONTINUOUS)
384 {
385 EMACS_ADD_TIME (t->expiration, now, t->interval);
386 schedule_atimer (t);
387 }
388 else
389 {
390 t->next = free_atimers;
391 free_atimers = t;
392 }
393
394 EMACS_GET_TIME (now);
395 }
396
397#if defined (USG) && !defined (POSIX_SIGNALS)
398 /* USG systems forget handlers when they are used;
399 must reestablish each time. */
400 signal (SIGALRM, alarm_signal_handler);
401#endif /* USG */
402
403 set_alarm ();
404}
405
406
407/* Call alarm_signal_handler for pending timers. */
408
409void
410do_pending_atimers ()
411{
412 if (pending_atimers)
413 {
414 BLOCK_ATIMERS;
415 alarm_signal_handler (SIGALRM);
416 UNBLOCK_ATIMERS;
417 }
418}
419
420
421/* Turn alarms on/off. This seems to be temporarily necessary on
422 some systems like HPUX (see process.c). */
423
424void
425turn_on_atimers (on)
426 int on;
427{
428 if (on)
429 {
430 signal (SIGALRM, alarm_signal_handler);
431 set_alarm ();
432 }
433 else
434 alarm (0);
435}
436
437
438void
439init_atimer ()
440{
441 free_atimers = atimers = NULL;
442 pending_atimers = 0;
443 signal (SIGALRM, alarm_signal_handler);
444}