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