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