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