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