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> | |
5890e9f7 | 20 | #include <stdio.h> |
d7306fe6 | 21 | #include <setjmp.h> |
898b4c5c DN |
22 | #include "lisp.h" |
23 | #include "syssignal.h" | |
24 | #include "systime.h" | |
25 | #include "blockinput.h" | |
26 | #include "atimer.h" | |
e12489f9 | 27 | #include <unistd.h> |
e12489f9 | 28 | |
e12489f9 GM |
29 | /* Free-list of atimer structures. */ |
30 | ||
31 | static struct atimer *free_atimers; | |
32 | ||
9c2e3e8d GM |
33 | /* List of currently not running timers due to a call to |
34 | lock_atimer. */ | |
35 | ||
36 | static struct atimer *stopped_atimers; | |
37 | ||
e12489f9 GM |
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 | ||
20ef56db | 43 | /* Non-zero means alarm signal handler has found ripe timers but |
e12489f9 GM |
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 | ||
5f351ff1 | 51 | /* Block/unblock SIGALRM. */ |
e12489f9 | 52 | |
2fe28299 PE |
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 | } | |
e12489f9 GM |
71 | |
72 | /* Function prototypes. */ | |
73 | ||
f57e2426 J |
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 *); | |
e12489f9 GM |
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 * | |
3e7d6594 | 98 | start_atimer (enum atimer_type type, EMACS_TIME timestamp, atimer_callback fn, |
e5447b22 | 99 | void *client_data) |
e12489f9 GM |
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 | |
d35af63c PE |
106 | if (EMACS_NSECS (timestamp) != 0 |
107 | && EMACS_SECS (timestamp) < TYPE_MAXIMUM (time_t)) | |
e9a9ae03 | 108 | timestamp = make_emacs_time (EMACS_SECS (timestamp) + 1, 0); |
e12489f9 GM |
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 | |
23f86fce | 119 | t = xmalloc (sizeof *t); |
e12489f9 GM |
120 | |
121 | /* Fill the atimer structure. */ | |
72af86bd | 122 | memset (t, 0, sizeof *t); |
e12489f9 GM |
123 | t->type = type; |
124 | t->fn = fn; | |
125 | t->client_data = client_data; | |
126 | ||
2fe28299 | 127 | block_atimers (); |
e12489f9 GM |
128 | |
129 | /* Compute the timer's expiration time. */ | |
130 | switch (type) | |
131 | { | |
132 | case ATIMER_ABSOLUTE: | |
3e7d6594 | 133 | t->expiration = timestamp; |
e12489f9 | 134 | break; |
177c0ea7 | 135 | |
e12489f9 | 136 | case ATIMER_RELATIVE: |
e9a9ae03 | 137 | t->expiration = add_emacs_time (current_emacs_time (), timestamp); |
e12489f9 | 138 | break; |
177c0ea7 | 139 | |
e12489f9 | 140 | case ATIMER_CONTINUOUS: |
e9a9ae03 | 141 | t->expiration = add_emacs_time (current_emacs_time (), timestamp); |
3e7d6594 | 142 | t->interval = timestamp; |
e12489f9 GM |
143 | break; |
144 | } | |
145 | ||
146 | /* Insert the timer in the list of active atimers. */ | |
147 | schedule_atimer (t); | |
2fe28299 | 148 | unblock_atimers (); |
e12489f9 GM |
149 | |
150 | /* Arrange for a SIGALRM at the time the next atimer is ripe. */ | |
151 | set_alarm (); | |
177c0ea7 | 152 | |
e12489f9 GM |
153 | return t; |
154 | } | |
155 | ||
156 | ||
157 | /* Cancel and free atimer TIMER. */ | |
158 | ||
159 | void | |
971de7fb | 160 | cancel_atimer (struct atimer *timer) |
e12489f9 | 161 | { |
50b1039f | 162 | int i; |
177c0ea7 | 163 | |
2fe28299 | 164 | block_atimers (); |
e12489f9 | 165 | |
50b1039f | 166 | for (i = 0; i < 2; ++i) |
94870977 | 167 | { |
50b1039f GM |
168 | struct atimer *t, *prev; |
169 | struct atimer **list = i ? &stopped_atimers : &atimers; | |
177c0ea7 | 170 | |
94870977 | 171 | /* See if TIMER is active or stopped. */ |
4ab1d4be | 172 | for (t = *list, prev = NULL; t && t != timer; prev = t, t = t->next) |
94870977 GM |
173 | ; |
174 | ||
88dbda51 JB |
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. */ | |
94870977 GM |
178 | if (t) |
179 | { | |
180 | if (prev) | |
181 | prev->next = t->next; | |
182 | else | |
183 | *list = t->next; | |
177c0ea7 | 184 | |
94870977 GM |
185 | t->next = free_atimers; |
186 | free_atimers = t; | |
4ab1d4be | 187 | break; |
94870977 | 188 | } |
e12489f9 GM |
189 | } |
190 | ||
2fe28299 | 191 | unblock_atimers (); |
e12489f9 GM |
192 | } |
193 | ||
194 | ||
3e7d6594 | 195 | /* Append two lists of atimers LIST_1 and LIST_2 and return the |
50b1039f GM |
196 | result list. */ |
197 | ||
198 | static struct atimer * | |
3e7d6594 | 199 | append_atimer_lists (struct atimer *list_1, struct atimer *list_2) |
50b1039f | 200 | { |
3e7d6594 PE |
201 | if (list_1 == NULL) |
202 | return list_2; | |
203 | else if (list_2 == NULL) | |
204 | return list_1; | |
50b1039f GM |
205 | else |
206 | { | |
207 | struct atimer *p; | |
177c0ea7 | 208 | |
3e7d6594 | 209 | for (p = list_1; p->next; p = p->next) |
50b1039f | 210 | ; |
3e7d6594 PE |
211 | p->next = list_2; |
212 | return list_1; | |
50b1039f GM |
213 | } |
214 | } | |
215 | ||
216 | ||
217 | /* Stop all timers except timer T. T null means stop all timers. */ | |
9c2e3e8d GM |
218 | |
219 | void | |
971de7fb | 220 | stop_other_atimers (struct atimer *t) |
9c2e3e8d | 221 | { |
2fe28299 | 222 | block_atimers (); |
177c0ea7 | 223 | |
9c2e3e8d GM |
224 | if (t) |
225 | { | |
2503c8b1 | 226 | struct atimer *p, *prev; |
177c0ea7 | 227 | |
2503c8b1 | 228 | /* See if T is active. */ |
96013ba9 | 229 | for (p = atimers, prev = NULL; p && p != t; prev = p, p = p->next) |
2503c8b1 GM |
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; | |
9c2e3e8d | 243 | } |
177c0ea7 | 244 | |
50b1039f | 245 | stopped_atimers = append_atimer_lists (atimers, stopped_atimers); |
9c2e3e8d | 246 | atimers = t; |
2fe28299 | 247 | unblock_atimers (); |
9c2e3e8d GM |
248 | } |
249 | ||
250 | ||
251 | /* Run all timers again, if some have been stopped with a call to | |
252 | stop_other_atimers. */ | |
253 | ||
e192d7d3 | 254 | static void |
971de7fb | 255 | run_all_atimers (void) |
9c2e3e8d GM |
256 | { |
257 | if (stopped_atimers) | |
258 | { | |
259 | struct atimer *t = atimers; | |
50b1039f | 260 | struct atimer *next; |
177c0ea7 | 261 | |
2fe28299 | 262 | block_atimers (); |
9c2e3e8d GM |
263 | atimers = stopped_atimers; |
264 | stopped_atimers = NULL; | |
177c0ea7 | 265 | |
50b1039f GM |
266 | while (t) |
267 | { | |
268 | next = t->next; | |
269 | schedule_atimer (t); | |
270 | t = next; | |
271 | } | |
177c0ea7 | 272 | |
2fe28299 | 273 | unblock_atimers (); |
9c2e3e8d GM |
274 | } |
275 | } | |
276 | ||
277 | ||
e192d7d3 | 278 | /* A version of run_all_atimers suitable for a record_unwind_protect. */ |
9c2e3e8d GM |
279 | |
280 | Lisp_Object | |
971de7fb | 281 | unwind_stop_other_atimers (Lisp_Object dummy) |
9c2e3e8d GM |
282 | { |
283 | run_all_atimers (); | |
284 | return Qnil; | |
285 | } | |
286 | ||
287 | ||
e12489f9 GM |
288 | /* Arrange for a SIGALRM to arrive when the next timer is ripe. */ |
289 | ||
290 | static void | |
971de7fb | 291 | set_alarm (void) |
e12489f9 | 292 | { |
e12489f9 GM |
293 | if (atimers) |
294 | { | |
e12489f9 GM |
295 | #ifdef HAVE_SETITIMER |
296 | struct itimerval it; | |
297 | #endif | |
298 | ||
299 | /* Determine s/us till the next timer is ripe. */ | |
e9a9ae03 | 300 | EMACS_TIME now = current_emacs_time (); |
e12489f9 | 301 | |
e12489f9 | 302 | /* Don't set the interval to 0; this disables the timer. */ |
e9a9ae03 PE |
303 | EMACS_TIME interval = (EMACS_TIME_LE (atimers->expiration, now) |
304 | ? make_emacs_time (0, 1000 * 1000) | |
305 | : sub_emacs_time (atimers->expiration, now)); | |
d35af63c PE |
306 | |
307 | #ifdef HAVE_SETITIMER | |
177c0ea7 | 308 | |
72af86bd | 309 | memset (&it, 0, sizeof it); |
e9a9ae03 | 310 | it.it_value = make_timeval (interval); |
e12489f9 GM |
311 | setitimer (ITIMER_REAL, &it, 0); |
312 | #else /* not HAVE_SETITIMER */ | |
e9a9ae03 | 313 | alarm (max (EMACS_SECS (interval), 1)); |
e12489f9 GM |
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 | |
971de7fb | 324 | schedule_atimer (struct atimer *t) |
e12489f9 GM |
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; | |
177c0ea7 | 337 | |
e12489f9 GM |
338 | t->next = a; |
339 | } | |
340 | ||
170c80be | 341 | static void |
971de7fb | 342 | run_timers (void) |
e12489f9 GM |
343 | { |
344 | EMACS_TIME now; | |
177c0ea7 | 345 | |
e12489f9 GM |
346 | while (atimers |
347 | && (pending_atimers = interrupt_input_blocked) == 0 | |
e9a9ae03 | 348 | && (now = current_emacs_time (), |
d35af63c | 349 | EMACS_TIME_LE (atimers->expiration, now))) |
e12489f9 GM |
350 | { |
351 | struct atimer *t; | |
177c0ea7 | 352 | |
e12489f9 GM |
353 | t = atimers; |
354 | atimers = atimers->next; | |
355 | t->fn (t); | |
177c0ea7 | 356 | |
e12489f9 GM |
357 | if (t->type == ATIMER_CONTINUOUS) |
358 | { | |
e9a9ae03 | 359 | t->expiration = add_emacs_time (now, t->interval); |
e12489f9 GM |
360 | schedule_atimer (t); |
361 | } | |
362 | else | |
363 | { | |
364 | t->next = free_atimers; | |
365 | free_atimers = t; | |
366 | } | |
e12489f9 | 367 | } |
177c0ea7 | 368 | |
6bbbda87 CY |
369 | if (! atimers) |
370 | pending_atimers = 0; | |
371 | ||
07a1e794 | 372 | #ifdef SYNC_INPUT |
325530de CY |
373 | if (pending_atimers) |
374 | pending_signals = 1; | |
375 | else | |
376 | { | |
377 | pending_signals = interrupt_input_pending; | |
378 | set_alarm (); | |
379 | } | |
07a1e794 JB |
380 | #else |
381 | if (! pending_atimers) | |
382 | set_alarm (); | |
383 | #endif | |
e12489f9 GM |
384 | } |
385 | ||
386 | ||
170c80be JD |
387 | /* Signal handler for SIGALRM. SIGNO is the signal number, i.e. |
388 | SIGALRM. */ | |
389 | ||
20ef56db PE |
390 | static void |
391 | handle_alarm_signal (int sig) | |
170c80be JD |
392 | { |
393 | pending_atimers = 1; | |
07a1e794 | 394 | #ifdef SYNC_INPUT |
325530de | 395 | pending_signals = 1; |
07a1e794 | 396 | #else |
170c80be JD |
397 | run_timers (); |
398 | #endif | |
399 | } | |
400 | ||
20ef56db PE |
401 | static void |
402 | deliver_alarm_signal (int sig) | |
403 | { | |
404 | handle_on_main_thread (sig, handle_alarm_signal); | |
405 | } | |
406 | ||
170c80be | 407 | |
20ef56db | 408 | /* Call alarm signal handler for pending timers. */ |
e12489f9 GM |
409 | |
410 | void | |
971de7fb | 411 | do_pending_atimers (void) |
e12489f9 GM |
412 | { |
413 | if (pending_atimers) | |
414 | { | |
2fe28299 | 415 | block_atimers (); |
170c80be | 416 | run_timers (); |
2fe28299 | 417 | unblock_atimers (); |
e12489f9 GM |
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 | |
b1bb8011 | 426 | turn_on_atimers (bool on) |
e12489f9 GM |
427 | { |
428 | if (on) | |
429 | { | |
2fe28299 PE |
430 | struct sigaction action; |
431 | emacs_sigaction_init (&action, deliver_alarm_signal); | |
432 | sigaction (SIGALRM, &action, 0); | |
e12489f9 GM |
433 | set_alarm (); |
434 | } | |
435 | else | |
436 | alarm (0); | |
437 | } | |
438 | ||
439 | ||
440 | void | |
971de7fb | 441 | init_atimer (void) |
e12489f9 | 442 | { |
2fe28299 | 443 | struct sigaction action; |
bcdf96ba | 444 | free_atimers = stopped_atimers = atimers = NULL; |
e12489f9 | 445 | pending_atimers = 0; |
325530de | 446 | /* pending_signals is initialized in init_keyboard.*/ |
2fe28299 PE |
447 | emacs_sigaction_init (&action, deliver_alarm_signal); |
448 | sigaction (SIGALRM, &action, 0); | |
e12489f9 | 449 | } |