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> |
5890e9f7 | 24 | #include <lisp.h> |
e12489f9 GM |
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 * | |
971de7fb | 93 | start_atimer (enum atimer_type type, struct timeval time, atimer_callback fn, void *client_data) |
e12489f9 GM |
94 | { |
95 | struct atimer *t; | |
96 | ||
97 | /* Round TIME up to the next full second if we don't have | |
98 | itimers. */ | |
99 | #ifndef HAVE_SETITIMER | |
100 | if (EMACS_USECS (time) != 0) | |
101 | { | |
4ce94f99 EZ |
102 | EMACS_SET_USECS (time, 0); |
103 | EMACS_SET_SECS (time, EMACS_SECS (time) + 1); | |
e12489f9 GM |
104 | } |
105 | #endif /* not HAVE_SETITIMER */ | |
106 | ||
107 | /* Get an atimer structure from the free-list, or allocate | |
108 | a new one. */ | |
109 | if (free_atimers) | |
110 | { | |
111 | t = free_atimers; | |
112 | free_atimers = t->next; | |
113 | } | |
114 | else | |
115 | t = (struct atimer *) xmalloc (sizeof *t); | |
116 | ||
117 | /* Fill the atimer structure. */ | |
118 | bzero (t, sizeof *t); | |
119 | t->type = type; | |
120 | t->fn = fn; | |
121 | t->client_data = client_data; | |
122 | ||
123 | BLOCK_ATIMERS; | |
124 | ||
125 | /* Compute the timer's expiration time. */ | |
126 | switch (type) | |
127 | { | |
128 | case ATIMER_ABSOLUTE: | |
129 | t->expiration = time; | |
130 | break; | |
177c0ea7 | 131 | |
e12489f9 GM |
132 | case ATIMER_RELATIVE: |
133 | EMACS_GET_TIME (t->expiration); | |
134 | EMACS_ADD_TIME (t->expiration, t->expiration, time); | |
135 | break; | |
177c0ea7 | 136 | |
e12489f9 GM |
137 | case ATIMER_CONTINUOUS: |
138 | EMACS_GET_TIME (t->expiration); | |
139 | EMACS_ADD_TIME (t->expiration, t->expiration, time); | |
140 | t->interval = time; | |
141 | break; | |
142 | } | |
143 | ||
144 | /* Insert the timer in the list of active atimers. */ | |
145 | schedule_atimer (t); | |
146 | UNBLOCK_ATIMERS; | |
147 | ||
148 | /* Arrange for a SIGALRM at the time the next atimer is ripe. */ | |
149 | set_alarm (); | |
177c0ea7 | 150 | |
e12489f9 GM |
151 | return t; |
152 | } | |
153 | ||
154 | ||
155 | /* Cancel and free atimer TIMER. */ | |
156 | ||
157 | void | |
971de7fb | 158 | cancel_atimer (struct atimer *timer) |
e12489f9 | 159 | { |
50b1039f | 160 | int i; |
177c0ea7 | 161 | |
e12489f9 GM |
162 | BLOCK_ATIMERS; |
163 | ||
50b1039f | 164 | for (i = 0; i < 2; ++i) |
94870977 | 165 | { |
50b1039f GM |
166 | struct atimer *t, *prev; |
167 | struct atimer **list = i ? &stopped_atimers : &atimers; | |
177c0ea7 | 168 | |
94870977 | 169 | /* See if TIMER is active or stopped. */ |
4ab1d4be | 170 | for (t = *list, prev = NULL; t && t != timer; prev = t, t = t->next) |
94870977 GM |
171 | ; |
172 | ||
50b1039f | 173 | /* If it is, take it off the its list, and put in on the |
94870977 GM |
174 | free-list. We don't bother to arrange for setting a |
175 | different alarm time, since a too early one doesn't hurt. */ | |
176 | if (t) | |
177 | { | |
178 | if (prev) | |
179 | prev->next = t->next; | |
180 | else | |
181 | *list = t->next; | |
177c0ea7 | 182 | |
94870977 GM |
183 | t->next = free_atimers; |
184 | free_atimers = t; | |
4ab1d4be | 185 | break; |
94870977 | 186 | } |
e12489f9 GM |
187 | } |
188 | ||
189 | UNBLOCK_ATIMERS; | |
190 | } | |
191 | ||
192 | ||
50b1039f GM |
193 | /* Append two lists of atimers LIST1 and LIST2 and return the |
194 | result list. */ | |
195 | ||
196 | static struct atimer * | |
971de7fb | 197 | append_atimer_lists (struct atimer *list1, struct atimer *list2) |
50b1039f GM |
198 | { |
199 | if (list1 == NULL) | |
200 | return list2; | |
201 | else if (list2 == NULL) | |
202 | return list1; | |
203 | else | |
204 | { | |
205 | struct atimer *p; | |
177c0ea7 | 206 | |
50b1039f GM |
207 | for (p = list1; p->next; p = p->next) |
208 | ; | |
209 | p->next = list2; | |
210 | return list1; | |
211 | } | |
212 | } | |
213 | ||
214 | ||
215 | /* Stop all timers except timer T. T null means stop all timers. */ | |
9c2e3e8d GM |
216 | |
217 | void | |
971de7fb | 218 | stop_other_atimers (struct atimer *t) |
9c2e3e8d GM |
219 | { |
220 | BLOCK_ATIMERS; | |
177c0ea7 | 221 | |
9c2e3e8d GM |
222 | if (t) |
223 | { | |
2503c8b1 | 224 | struct atimer *p, *prev; |
177c0ea7 | 225 | |
2503c8b1 | 226 | /* See if T is active. */ |
96013ba9 | 227 | for (p = atimers, prev = NULL; p && p != t; prev = p, p = p->next) |
2503c8b1 GM |
228 | ; |
229 | ||
230 | if (p == t) | |
231 | { | |
232 | if (prev) | |
233 | prev->next = t->next; | |
234 | else | |
235 | atimers = t->next; | |
236 | t->next = NULL; | |
237 | } | |
238 | else | |
239 | /* T is not active. Let's handle this like T == 0. */ | |
240 | t = NULL; | |
9c2e3e8d | 241 | } |
177c0ea7 | 242 | |
50b1039f | 243 | stopped_atimers = append_atimer_lists (atimers, stopped_atimers); |
9c2e3e8d GM |
244 | atimers = t; |
245 | UNBLOCK_ATIMERS; | |
246 | } | |
247 | ||
248 | ||
249 | /* Run all timers again, if some have been stopped with a call to | |
250 | stop_other_atimers. */ | |
251 | ||
252 | void | |
971de7fb | 253 | run_all_atimers (void) |
9c2e3e8d GM |
254 | { |
255 | if (stopped_atimers) | |
256 | { | |
257 | struct atimer *t = atimers; | |
50b1039f | 258 | struct atimer *next; |
177c0ea7 | 259 | |
9c2e3e8d GM |
260 | BLOCK_ATIMERS; |
261 | atimers = stopped_atimers; | |
262 | stopped_atimers = NULL; | |
177c0ea7 | 263 | |
50b1039f GM |
264 | while (t) |
265 | { | |
266 | next = t->next; | |
267 | schedule_atimer (t); | |
268 | t = next; | |
269 | } | |
177c0ea7 | 270 | |
9c2e3e8d GM |
271 | UNBLOCK_ATIMERS; |
272 | } | |
273 | } | |
274 | ||
275 | ||
276 | /* A version of run_all_timers suitable for a record_unwind_protect. */ | |
277 | ||
278 | Lisp_Object | |
971de7fb | 279 | unwind_stop_other_atimers (Lisp_Object dummy) |
9c2e3e8d GM |
280 | { |
281 | run_all_atimers (); | |
282 | return Qnil; | |
283 | } | |
284 | ||
285 | ||
e12489f9 GM |
286 | /* Arrange for a SIGALRM to arrive when the next timer is ripe. */ |
287 | ||
288 | static void | |
971de7fb | 289 | set_alarm (void) |
e12489f9 | 290 | { |
e12489f9 GM |
291 | if (atimers) |
292 | { | |
293 | EMACS_TIME now, time; | |
294 | #ifdef HAVE_SETITIMER | |
295 | struct itimerval it; | |
296 | #endif | |
297 | ||
298 | /* Determine s/us till the next timer is ripe. */ | |
299 | EMACS_GET_TIME (now); | |
300 | EMACS_SUB_TIME (time, atimers->expiration, now); | |
301 | ||
302 | #ifdef HAVE_SETITIMER | |
303 | /* Don't set the interval to 0; this disables the timer. */ | |
304 | if (EMACS_TIME_LE (atimers->expiration, now)) | |
305 | { | |
306 | EMACS_SET_SECS (time, 0); | |
307 | EMACS_SET_USECS (time, 1000); | |
308 | } | |
177c0ea7 | 309 | |
e12489f9 GM |
310 | bzero (&it, sizeof it); |
311 | it.it_value = time; | |
312 | setitimer (ITIMER_REAL, &it, 0); | |
313 | #else /* not HAVE_SETITIMER */ | |
314 | alarm (max (EMACS_SECS (time), 1)); | |
315 | #endif /* not HAVE_SETITIMER */ | |
316 | } | |
317 | } | |
318 | ||
319 | ||
320 | /* Insert timer T into the list of active atimers `atimers', keeping | |
321 | the list sorted by expiration time. T must not be in this list | |
322 | already. */ | |
323 | ||
324 | static void | |
971de7fb | 325 | schedule_atimer (struct atimer *t) |
e12489f9 GM |
326 | { |
327 | struct atimer *a = atimers, *prev = NULL; | |
328 | ||
329 | /* Look for the first atimer that is ripe after T. */ | |
330 | while (a && EMACS_TIME_GT (t->expiration, a->expiration)) | |
331 | prev = a, a = a->next; | |
332 | ||
333 | /* Insert T in front of the atimer found, if any. */ | |
334 | if (prev) | |
335 | prev->next = t; | |
336 | else | |
337 | atimers = t; | |
177c0ea7 | 338 | |
e12489f9 GM |
339 | t->next = a; |
340 | } | |
341 | ||
170c80be | 342 | static void |
971de7fb | 343 | run_timers (void) |
e12489f9 GM |
344 | { |
345 | EMACS_TIME now; | |
177c0ea7 | 346 | |
e12489f9 | 347 | EMACS_GET_TIME (now); |
177c0ea7 | 348 | |
e12489f9 GM |
349 | while (atimers |
350 | && (pending_atimers = interrupt_input_blocked) == 0 | |
351 | && EMACS_TIME_LE (atimers->expiration, now)) | |
352 | { | |
353 | struct atimer *t; | |
177c0ea7 | 354 | |
e12489f9 GM |
355 | t = atimers; |
356 | atimers = atimers->next; | |
357 | t->fn (t); | |
177c0ea7 | 358 | |
e12489f9 GM |
359 | if (t->type == ATIMER_CONTINUOUS) |
360 | { | |
361 | EMACS_ADD_TIME (t->expiration, now, t->interval); | |
362 | schedule_atimer (t); | |
363 | } | |
364 | else | |
365 | { | |
366 | t->next = free_atimers; | |
367 | free_atimers = t; | |
368 | } | |
177c0ea7 | 369 | |
e12489f9 GM |
370 | EMACS_GET_TIME (now); |
371 | } | |
177c0ea7 | 372 | |
6bbbda87 CY |
373 | if (! atimers) |
374 | pending_atimers = 0; | |
375 | ||
07a1e794 | 376 | #ifdef SYNC_INPUT |
325530de CY |
377 | if (pending_atimers) |
378 | pending_signals = 1; | |
379 | else | |
380 | { | |
381 | pending_signals = interrupt_input_pending; | |
382 | set_alarm (); | |
383 | } | |
07a1e794 JB |
384 | #else |
385 | if (! pending_atimers) | |
386 | set_alarm (); | |
387 | #endif | |
e12489f9 GM |
388 | } |
389 | ||
390 | ||
170c80be JD |
391 | /* Signal handler for SIGALRM. SIGNO is the signal number, i.e. |
392 | SIGALRM. */ | |
393 | ||
394 | SIGTYPE | |
971de7fb | 395 | alarm_signal_handler (int signo) |
170c80be | 396 | { |
c2623ee7 YM |
397 | #ifndef SYNC_INPUT |
398 | SIGNAL_THREAD_CHECK (signo); | |
399 | #endif | |
400 | ||
170c80be | 401 | pending_atimers = 1; |
07a1e794 | 402 | #ifdef SYNC_INPUT |
325530de | 403 | pending_signals = 1; |
07a1e794 | 404 | #else |
170c80be JD |
405 | run_timers (); |
406 | #endif | |
407 | } | |
408 | ||
409 | ||
e12489f9 GM |
410 | /* Call alarm_signal_handler for pending timers. */ |
411 | ||
412 | void | |
971de7fb | 413 | do_pending_atimers (void) |
e12489f9 GM |
414 | { |
415 | if (pending_atimers) | |
416 | { | |
417 | BLOCK_ATIMERS; | |
170c80be | 418 | run_timers (); |
e12489f9 GM |
419 | UNBLOCK_ATIMERS; |
420 | } | |
421 | } | |
422 | ||
423 | ||
424 | /* Turn alarms on/off. This seems to be temporarily necessary on | |
425 | some systems like HPUX (see process.c). */ | |
426 | ||
427 | void | |
971de7fb | 428 | turn_on_atimers (int on) |
e12489f9 GM |
429 | { |
430 | if (on) | |
431 | { | |
432 | signal (SIGALRM, alarm_signal_handler); | |
433 | set_alarm (); | |
434 | } | |
435 | else | |
436 | alarm (0); | |
437 | } | |
438 | ||
439 | ||
440 | void | |
971de7fb | 441 | init_atimer (void) |
e12489f9 | 442 | { |
bcdf96ba | 443 | free_atimers = stopped_atimers = atimers = NULL; |
e12489f9 | 444 | pending_atimers = 0; |
325530de | 445 | /* pending_signals is initialized in init_keyboard.*/ |
e12489f9 GM |
446 | signal (SIGALRM, alarm_signal_handler); |
447 | } | |
ab5796a9 MB |
448 | |
449 | /* arch-tag: e6308261-eec6-404b-89fb-6e5909518d70 | |
450 | (do not change this comment) */ |