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