Add 2011 to FSF/AIST copyright years.
[bpt/emacs.git] / src / atimer.c
CommitLineData
e12489f9 1/* Asynchronous timers.
429ab54e 2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005,
5df4f04c 3 2006, 2007, 2008, 2009, 2010, 2011 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 22#include <stdio.h>
d7306fe6 23#include <setjmp.h>
5890e9f7 24#include <lisp.h>
e12489f9
GM
25#include <syssignal.h>
26#include <systime.h>
27#include <blockinput.h>
28#include <atimer.h>
e12489f9
GM
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
e12489f9
GM
38/* Free-list of atimer structures. */
39
40static struct atimer *free_atimers;
41
9c2e3e8d
GM
42/* List of currently not running timers due to a call to
43 lock_atimer. */
44
45static struct atimer *stopped_atimers;
46
e12489f9
GM
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
50static 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
58int pending_atimers;
59
5f351ff1 60/* Block/unblock SIGALRM. */
e12489f9
GM
61
62#define BLOCK_ATIMERS sigblock (sigmask (SIGALRM))
63#define UNBLOCK_ATIMERS sigunblock (sigmask (SIGALRM))
64
65/* Function prototypes. */
66
67static void set_alarm P_ ((void));
68static void schedule_atimer P_ ((struct atimer *));
50b1039f
GM
69static struct atimer *append_atimer_lists P_ ((struct atimer *,
70 struct atimer *));
a3aca278 71SIGTYPE alarm_signal_handler ();
e12489f9
GM
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
92struct atimer *
93start_atimer (type, time, fn, client_data)
94 enum atimer_type type;
95 EMACS_TIME time;
96 atimer_callback fn;
97 void *client_data;
98{
99 struct atimer *t;
100
101 /* Round TIME up to the next full second if we don't have
102 itimers. */
103#ifndef HAVE_SETITIMER
104 if (EMACS_USECS (time) != 0)
105 {
4ce94f99
EZ
106 EMACS_SET_USECS (time, 0);
107 EMACS_SET_SECS (time, EMACS_SECS (time) + 1);
e12489f9
GM
108 }
109#endif /* not HAVE_SETITIMER */
110
111 /* Get an atimer structure from the free-list, or allocate
112 a new one. */
113 if (free_atimers)
114 {
115 t = free_atimers;
116 free_atimers = t->next;
117 }
118 else
119 t = (struct atimer *) xmalloc (sizeof *t);
120
121 /* Fill the atimer structure. */
122 bzero (t, sizeof *t);
123 t->type = type;
124 t->fn = fn;
125 t->client_data = client_data;
126
127 BLOCK_ATIMERS;
128
129 /* Compute the timer's expiration time. */
130 switch (type)
131 {
132 case ATIMER_ABSOLUTE:
133 t->expiration = time;
134 break;
177c0ea7 135
e12489f9
GM
136 case ATIMER_RELATIVE:
137 EMACS_GET_TIME (t->expiration);
138 EMACS_ADD_TIME (t->expiration, t->expiration, time);
139 break;
177c0ea7 140
e12489f9
GM
141 case ATIMER_CONTINUOUS:
142 EMACS_GET_TIME (t->expiration);
143 EMACS_ADD_TIME (t->expiration, t->expiration, time);
144 t->interval = time;
145 break;
146 }
147
148 /* Insert the timer in the list of active atimers. */
149 schedule_atimer (t);
150 UNBLOCK_ATIMERS;
151
152 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
153 set_alarm ();
177c0ea7 154
e12489f9
GM
155 return t;
156}
157
158
159/* Cancel and free atimer TIMER. */
160
161void
162cancel_atimer (timer)
163 struct atimer *timer;
164{
50b1039f 165 int i;
177c0ea7 166
e12489f9
GM
167 BLOCK_ATIMERS;
168
50b1039f 169 for (i = 0; i < 2; ++i)
94870977 170 {
50b1039f
GM
171 struct atimer *t, *prev;
172 struct atimer **list = i ? &stopped_atimers : &atimers;
177c0ea7 173
94870977 174 /* See if TIMER is active or stopped. */
4ab1d4be 175 for (t = *list, prev = NULL; t && t != timer; prev = t, t = t->next)
94870977
GM
176 ;
177
88dbda51
JB
178 /* If it is, take it off its list, and put in on the free-list.
179 We don't bother to arrange for setting a different alarm time,
180 since a too early one doesn't hurt. */
94870977
GM
181 if (t)
182 {
183 if (prev)
184 prev->next = t->next;
185 else
186 *list = t->next;
177c0ea7 187
94870977
GM
188 t->next = free_atimers;
189 free_atimers = t;
4ab1d4be 190 break;
94870977 191 }
e12489f9
GM
192 }
193
194 UNBLOCK_ATIMERS;
195}
196
197
50b1039f
GM
198/* Append two lists of atimers LIST1 and LIST2 and return the
199 result list. */
200
201static struct atimer *
202append_atimer_lists (list1, list2)
203 struct atimer *list1, *list2;
204{
205 if (list1 == NULL)
206 return list2;
207 else if (list2 == NULL)
208 return list1;
209 else
210 {
211 struct atimer *p;
177c0ea7 212
50b1039f
GM
213 for (p = list1; p->next; p = p->next)
214 ;
215 p->next = list2;
216 return list1;
217 }
218}
219
220
221/* Stop all timers except timer T. T null means stop all timers. */
9c2e3e8d
GM
222
223void
224stop_other_atimers (t)
225 struct atimer *t;
226{
227 BLOCK_ATIMERS;
177c0ea7 228
9c2e3e8d
GM
229 if (t)
230 {
2503c8b1 231 struct atimer *p, *prev;
177c0ea7 232
2503c8b1 233 /* See if T is active. */
96013ba9 234 for (p = atimers, prev = NULL; p && p != t; prev = p, p = p->next)
2503c8b1
GM
235 ;
236
237 if (p == t)
238 {
239 if (prev)
240 prev->next = t->next;
241 else
242 atimers = t->next;
243 t->next = NULL;
244 }
245 else
246 /* T is not active. Let's handle this like T == 0. */
247 t = NULL;
9c2e3e8d 248 }
177c0ea7 249
50b1039f 250 stopped_atimers = append_atimer_lists (atimers, stopped_atimers);
9c2e3e8d
GM
251 atimers = t;
252 UNBLOCK_ATIMERS;
253}
254
255
256/* Run all timers again, if some have been stopped with a call to
257 stop_other_atimers. */
258
259void
260run_all_atimers ()
261{
262 if (stopped_atimers)
263 {
264 struct atimer *t = atimers;
50b1039f 265 struct atimer *next;
177c0ea7 266
9c2e3e8d
GM
267 BLOCK_ATIMERS;
268 atimers = stopped_atimers;
269 stopped_atimers = NULL;
177c0ea7 270
50b1039f
GM
271 while (t)
272 {
273 next = t->next;
274 schedule_atimer (t);
275 t = next;
276 }
177c0ea7 277
9c2e3e8d
GM
278 UNBLOCK_ATIMERS;
279 }
280}
281
282
283/* A version of run_all_timers suitable for a record_unwind_protect. */
284
285Lisp_Object
286unwind_stop_other_atimers (dummy)
287 Lisp_Object dummy;
288{
289 run_all_atimers ();
290 return Qnil;
291}
292
293
e12489f9
GM
294/* Arrange for a SIGALRM to arrive when the next timer is ripe. */
295
296static void
297set_alarm ()
298{
e12489f9
GM
299#if defined (USG) && !defined (POSIX_SIGNALS)
300 /* USG systems forget handlers when they are used;
301 must reestablish each time. */
302 signal (SIGALRM, alarm_signal_handler);
303#endif /* USG */
177c0ea7 304
e12489f9
GM
305 if (atimers)
306 {
307 EMACS_TIME now, time;
308#ifdef HAVE_SETITIMER
309 struct itimerval it;
310#endif
311
312 /* Determine s/us till the next timer is ripe. */
313 EMACS_GET_TIME (now);
314 EMACS_SUB_TIME (time, atimers->expiration, now);
315
316#ifdef HAVE_SETITIMER
317 /* Don't set the interval to 0; this disables the timer. */
318 if (EMACS_TIME_LE (atimers->expiration, now))
319 {
320 EMACS_SET_SECS (time, 0);
321 EMACS_SET_USECS (time, 1000);
322 }
177c0ea7 323
e12489f9
GM
324 bzero (&it, sizeof it);
325 it.it_value = time;
326 setitimer (ITIMER_REAL, &it, 0);
327#else /* not HAVE_SETITIMER */
328 alarm (max (EMACS_SECS (time), 1));
329#endif /* not HAVE_SETITIMER */
330 }
331}
332
333
334/* Insert timer T into the list of active atimers `atimers', keeping
335 the list sorted by expiration time. T must not be in this list
336 already. */
337
338static void
339schedule_atimer (t)
340 struct atimer *t;
341{
342 struct atimer *a = atimers, *prev = NULL;
343
344 /* Look for the first atimer that is ripe after T. */
345 while (a && EMACS_TIME_GT (t->expiration, a->expiration))
346 prev = a, a = a->next;
347
348 /* Insert T in front of the atimer found, if any. */
349 if (prev)
350 prev->next = t;
351 else
352 atimers = t;
177c0ea7 353
e12489f9
GM
354 t->next = a;
355}
356
170c80be
JD
357static void
358run_timers ()
e12489f9
GM
359{
360 EMACS_TIME now;
177c0ea7 361
e12489f9 362 EMACS_GET_TIME (now);
177c0ea7 363
e12489f9
GM
364 while (atimers
365 && (pending_atimers = interrupt_input_blocked) == 0
366 && EMACS_TIME_LE (atimers->expiration, now))
367 {
368 struct atimer *t;
177c0ea7 369
e12489f9
GM
370 t = atimers;
371 atimers = atimers->next;
372 t->fn (t);
177c0ea7 373
e12489f9
GM
374 if (t->type == ATIMER_CONTINUOUS)
375 {
376 EMACS_ADD_TIME (t->expiration, now, t->interval);
377 schedule_atimer (t);
378 }
379 else
380 {
381 t->next = free_atimers;
382 free_atimers = t;
383 }
177c0ea7 384
e12489f9
GM
385 EMACS_GET_TIME (now);
386 }
177c0ea7 387
6bbbda87
CY
388 if (! atimers)
389 pending_atimers = 0;
390
07a1e794 391#ifdef SYNC_INPUT
325530de
CY
392 if (pending_atimers)
393 pending_signals = 1;
394 else
395 {
396 pending_signals = interrupt_input_pending;
397 set_alarm ();
398 }
07a1e794
JB
399#else
400 if (! pending_atimers)
401 set_alarm ();
402#endif
e12489f9
GM
403}
404
405
170c80be
JD
406/* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
407 SIGALRM. */
408
409SIGTYPE
410alarm_signal_handler (signo)
411 int signo;
412{
c2623ee7
YM
413#ifndef SYNC_INPUT
414 SIGNAL_THREAD_CHECK (signo);
415#endif
416
170c80be 417 pending_atimers = 1;
07a1e794 418#ifdef SYNC_INPUT
325530de 419 pending_signals = 1;
07a1e794 420#else
170c80be
JD
421 run_timers ();
422#endif
423}
424
425
e12489f9
GM
426/* Call alarm_signal_handler for pending timers. */
427
428void
429do_pending_atimers ()
430{
431 if (pending_atimers)
432 {
433 BLOCK_ATIMERS;
170c80be 434 run_timers ();
e12489f9
GM
435 UNBLOCK_ATIMERS;
436 }
437}
438
439
440/* Turn alarms on/off. This seems to be temporarily necessary on
441 some systems like HPUX (see process.c). */
442
443void
444turn_on_atimers (on)
445 int on;
446{
447 if (on)
448 {
449 signal (SIGALRM, alarm_signal_handler);
450 set_alarm ();
451 }
452 else
453 alarm (0);
454}
455
456
457void
458init_atimer ()
459{
bcdf96ba 460 free_atimers = stopped_atimers = atimers = NULL;
e12489f9 461 pending_atimers = 0;
325530de 462 /* pending_signals is initialized in init_keyboard.*/
e12489f9
GM
463 signal (SIGALRM, alarm_signal_handler);
464}
ab5796a9
MB
465
466/* arch-tag: e6308261-eec6-404b-89fb-6e5909518d70
467 (do not change this comment) */