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