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