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