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