Commit | Line | Data |
---|---|---|
e12489f9 | 1 | /* Asynchronous timers. |
429ab54e | 2 | Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, |
114f9c96 | 3 | 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
e12489f9 GM |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
e12489f9 | 8 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
e12489f9 GM |
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 | |
9ec0b715 | 18 | along 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> |
898b4c5c DN |
24 | #include "lisp.h" |
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 | ||
40 | static struct atimer *free_atimers; | |
41 | ||
9c2e3e8d GM |
42 | /* List of currently not running timers due to a call to |
43 | lock_atimer. */ | |
44 | ||
45 | static 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 | ||
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 | ||
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 | ||
f57e2426 J |
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 *); | |
971de7fb | 71 | SIGTYPE alarm_signal_handler (int signo); |
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 | ||
92 | struct atimer * | |
e5447b22 JB |
93 | start_atimer (enum atimer_type type, EMACS_TIME time, atimer_callback fn, |
94 | void *client_data) | |
e12489f9 GM |
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 | { | |
4ce94f99 EZ |
103 | EMACS_SET_USECS (time, 0); |
104 | EMACS_SET_SECS (time, EMACS_SECS (time) + 1); | |
e12489f9 GM |
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. */ | |
72af86bd | 119 | memset (t, 0, sizeof *t); |
e12489f9 GM |
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; | |
177c0ea7 | 132 | |
e12489f9 GM |
133 | case ATIMER_RELATIVE: |
134 | EMACS_GET_TIME (t->expiration); | |
135 | EMACS_ADD_TIME (t->expiration, t->expiration, time); | |
136 | break; | |
177c0ea7 | 137 | |
e12489f9 GM |
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 (); | |
177c0ea7 | 151 | |
e12489f9 GM |
152 | return t; |
153 | } | |
154 | ||
155 | ||
156 | /* Cancel and free atimer TIMER. */ | |
157 | ||
158 | void | |
971de7fb | 159 | cancel_atimer (struct atimer *timer) |
e12489f9 | 160 | { |
50b1039f | 161 | int i; |
177c0ea7 | 162 | |
e12489f9 GM |
163 | BLOCK_ATIMERS; |
164 | ||
50b1039f | 165 | for (i = 0; i < 2; ++i) |
94870977 | 166 | { |
50b1039f GM |
167 | struct atimer *t, *prev; |
168 | struct atimer **list = i ? &stopped_atimers : &atimers; | |
177c0ea7 | 169 | |
94870977 | 170 | /* See if TIMER is active or stopped. */ |
4ab1d4be | 171 | for (t = *list, prev = NULL; t && t != timer; prev = t, t = t->next) |
94870977 GM |
172 | ; |
173 | ||
50b1039f | 174 | /* If it is, take it off the its list, and put in on the |
94870977 GM |
175 | free-list. We don't bother to arrange for setting a |
176 | different alarm time, 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; | |
177c0ea7 | 183 | |
94870977 GM |
184 | t->next = free_atimers; |
185 | free_atimers = t; | |
4ab1d4be | 186 | break; |
94870977 | 187 | } |
e12489f9 GM |
188 | } |
189 | ||
190 | UNBLOCK_ATIMERS; | |
191 | } | |
192 | ||
193 | ||
50b1039f GM |
194 | /* Append two lists of atimers LIST1 and LIST2 and return the |
195 | result list. */ | |
196 | ||
197 | static struct atimer * | |
971de7fb | 198 | append_atimer_lists (struct atimer *list1, struct atimer *list2) |
50b1039f GM |
199 | { |
200 | if (list1 == NULL) | |
201 | return list2; | |
202 | else if (list2 == NULL) | |
203 | return list1; | |
204 | else | |
205 | { | |
206 | struct atimer *p; | |
177c0ea7 | 207 | |
50b1039f GM |
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. */ | |
9c2e3e8d GM |
217 | |
218 | void | |
971de7fb | 219 | stop_other_atimers (struct atimer *t) |
9c2e3e8d GM |
220 | { |
221 | BLOCK_ATIMERS; | |
177c0ea7 | 222 | |
9c2e3e8d GM |
223 | if (t) |
224 | { | |
2503c8b1 | 225 | struct atimer *p, *prev; |
177c0ea7 | 226 | |
2503c8b1 | 227 | /* See if T is active. */ |
96013ba9 | 228 | for (p = atimers, prev = NULL; p && p != t; prev = p, p = p->next) |
2503c8b1 GM |
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; | |
9c2e3e8d | 242 | } |
177c0ea7 | 243 | |
50b1039f | 244 | stopped_atimers = append_atimer_lists (atimers, stopped_atimers); |
9c2e3e8d GM |
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 | |
971de7fb | 254 | run_all_atimers (void) |
9c2e3e8d GM |
255 | { |
256 | if (stopped_atimers) | |
257 | { | |
258 | struct atimer *t = atimers; | |
50b1039f | 259 | struct atimer *next; |
177c0ea7 | 260 | |
9c2e3e8d GM |
261 | BLOCK_ATIMERS; |
262 | atimers = stopped_atimers; | |
263 | stopped_atimers = NULL; | |
177c0ea7 | 264 | |
50b1039f GM |
265 | while (t) |
266 | { | |
267 | next = t->next; | |
268 | schedule_atimer (t); | |
269 | t = next; | |
270 | } | |
177c0ea7 | 271 | |
9c2e3e8d GM |
272 | UNBLOCK_ATIMERS; |
273 | } | |
274 | } | |
275 | ||
276 | ||
277 | /* A version of run_all_timers suitable for a record_unwind_protect. */ | |
278 | ||
279 | Lisp_Object | |
971de7fb | 280 | unwind_stop_other_atimers (Lisp_Object dummy) |
9c2e3e8d GM |
281 | { |
282 | run_all_atimers (); | |
283 | return Qnil; | |
284 | } | |
285 | ||
286 | ||
e12489f9 GM |
287 | /* Arrange for a SIGALRM to arrive when the next timer is ripe. */ |
288 | ||
289 | static void | |
971de7fb | 290 | set_alarm (void) |
e12489f9 | 291 | { |
e12489f9 GM |
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 | } | |
177c0ea7 | 310 | |
72af86bd | 311 | memset (&it, 0, sizeof it); |
e12489f9 GM |
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 | |
971de7fb | 326 | schedule_atimer (struct atimer *t) |
e12489f9 GM |
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; | |
177c0ea7 | 339 | |
e12489f9 GM |
340 | t->next = a; |
341 | } | |
342 | ||
170c80be | 343 | static void |
971de7fb | 344 | run_timers (void) |
e12489f9 GM |
345 | { |
346 | EMACS_TIME now; | |
177c0ea7 | 347 | |
e12489f9 | 348 | EMACS_GET_TIME (now); |
177c0ea7 | 349 | |
e12489f9 GM |
350 | while (atimers |
351 | && (pending_atimers = interrupt_input_blocked) == 0 | |
352 | && EMACS_TIME_LE (atimers->expiration, now)) | |
353 | { | |
354 | struct atimer *t; | |
177c0ea7 | 355 | |
e12489f9 GM |
356 | t = atimers; |
357 | atimers = atimers->next; | |
358 | t->fn (t); | |
177c0ea7 | 359 | |
e12489f9 GM |
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 | } | |
177c0ea7 | 370 | |
e12489f9 GM |
371 | EMACS_GET_TIME (now); |
372 | } | |
177c0ea7 | 373 | |
6bbbda87 CY |
374 | if (! atimers) |
375 | pending_atimers = 0; | |
376 | ||
07a1e794 | 377 | #ifdef SYNC_INPUT |
325530de CY |
378 | if (pending_atimers) |
379 | pending_signals = 1; | |
380 | else | |
381 | { | |
382 | pending_signals = interrupt_input_pending; | |
383 | set_alarm (); | |
384 | } | |
07a1e794 JB |
385 | #else |
386 | if (! pending_atimers) | |
387 | set_alarm (); | |
388 | #endif | |
e12489f9 GM |
389 | } |
390 | ||
391 | ||
170c80be JD |
392 | /* Signal handler for SIGALRM. SIGNO is the signal number, i.e. |
393 | SIGALRM. */ | |
394 | ||
395 | SIGTYPE | |
971de7fb | 396 | alarm_signal_handler (int signo) |
170c80be | 397 | { |
c2623ee7 YM |
398 | #ifndef SYNC_INPUT |
399 | SIGNAL_THREAD_CHECK (signo); | |
400 | #endif | |
401 | ||
170c80be | 402 | pending_atimers = 1; |
07a1e794 | 403 | #ifdef SYNC_INPUT |
325530de | 404 | pending_signals = 1; |
07a1e794 | 405 | #else |
170c80be JD |
406 | run_timers (); |
407 | #endif | |
408 | } | |
409 | ||
410 | ||
e12489f9 GM |
411 | /* Call alarm_signal_handler for pending timers. */ |
412 | ||
413 | void | |
971de7fb | 414 | do_pending_atimers (void) |
e12489f9 GM |
415 | { |
416 | if (pending_atimers) | |
417 | { | |
418 | BLOCK_ATIMERS; | |
170c80be | 419 | run_timers (); |
e12489f9 GM |
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 | |
971de7fb | 429 | turn_on_atimers (int on) |
e12489f9 GM |
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 | |
971de7fb | 442 | init_atimer (void) |
e12489f9 | 443 | { |
bcdf96ba | 444 | free_atimers = stopped_atimers = atimers = NULL; |
e12489f9 | 445 | pending_atimers = 0; |
325530de | 446 | /* pending_signals is initialized in init_keyboard.*/ |
e12489f9 GM |
447 | signal (SIGALRM, alarm_signal_handler); |
448 | } | |
ab5796a9 MB |
449 | |
450 | /* arch-tag: e6308261-eec6-404b-89fb-6e5909518d70 | |
451 | (do not change this comment) */ |