Add pcomplete support for hosts defined in .ssh/config.
[emacs.git] / src / atimer.c
blobaff506a9f70e927d9d69d2e224f2d30e17a2750d
1 /* Asynchronous timers.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include <config.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <setjmp.h>
24 #include "lisp.h"
25 #include "syssignal.h"
26 #include "systime.h"
27 #include "blockinput.h"
28 #include "atimer.h"
29 #include <unistd.h>
31 #ifdef HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #endif
35 /* Free-list of atimer structures. */
37 static struct atimer *free_atimers;
39 /* List of currently not running timers due to a call to
40 lock_atimer. */
42 static struct atimer *stopped_atimers;
44 /* List of active atimers, sorted by expiration time. The timer that
45 will become ripe next is always at the front of this list. */
47 static struct atimer *atimers;
49 /* Non-zero means alarm_signal_handler has found ripe timers but
50 interrupt_input_blocked was non-zero. In this case, timer
51 functions are not called until the next UNBLOCK_INPUT because timer
52 functions are expected to call X, and X cannot be assumed to be
53 reentrant. */
55 int pending_atimers;
57 /* Block/unblock SIGALRM. */
59 #define BLOCK_ATIMERS sigblock (sigmask (SIGALRM))
60 #define UNBLOCK_ATIMERS sigunblock (sigmask (SIGALRM))
62 /* Function prototypes. */
64 static void set_alarm (void);
65 static void schedule_atimer (struct atimer *);
66 static struct atimer *append_atimer_lists (struct atimer *,
67 struct atimer *);
68 SIGTYPE alarm_signal_handler (int signo);
71 /* Start a new atimer of type TYPE. TIME specifies when the timer is
72 ripe. FN is the function to call when the timer fires.
73 CLIENT_DATA is stored in the client_data member of the atimer
74 structure returned and so made available to FN when it is called.
76 If TYPE is ATIMER_ABSOLUTE, TIME is the absolute time at which the
77 timer fires.
79 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the
80 future.
82 In both cases, the timer is automatically freed after it has fired.
84 If TYPE is ATIMER_CONTINUOUS, the timer fires every TIME s/us.
86 Value is a pointer to the atimer started. It can be used in calls
87 to cancel_atimer; don't free it yourself. */
89 struct atimer *
90 start_atimer (enum atimer_type type, EMACS_TIME time, atimer_callback fn,
91 void *client_data)
93 struct atimer *t;
95 /* Round TIME up to the next full second if we don't have
96 itimers. */
97 #ifndef HAVE_SETITIMER
98 if (EMACS_USECS (time) != 0)
100 EMACS_SET_USECS (time, 0);
101 EMACS_SET_SECS (time, EMACS_SECS (time) + 1);
103 #endif /* not HAVE_SETITIMER */
105 /* Get an atimer structure from the free-list, or allocate
106 a new one. */
107 if (free_atimers)
109 t = free_atimers;
110 free_atimers = t->next;
112 else
113 t = (struct atimer *) xmalloc (sizeof *t);
115 /* Fill the atimer structure. */
116 memset (t, 0, sizeof *t);
117 t->type = type;
118 t->fn = fn;
119 t->client_data = client_data;
121 BLOCK_ATIMERS;
123 /* Compute the timer's expiration time. */
124 switch (type)
126 case ATIMER_ABSOLUTE:
127 t->expiration = time;
128 break;
130 case ATIMER_RELATIVE:
131 EMACS_GET_TIME (t->expiration);
132 EMACS_ADD_TIME (t->expiration, t->expiration, time);
133 break;
135 case ATIMER_CONTINUOUS:
136 EMACS_GET_TIME (t->expiration);
137 EMACS_ADD_TIME (t->expiration, t->expiration, time);
138 t->interval = time;
139 break;
142 /* Insert the timer in the list of active atimers. */
143 schedule_atimer (t);
144 UNBLOCK_ATIMERS;
146 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
147 set_alarm ();
149 return t;
153 /* Cancel and free atimer TIMER. */
155 void
156 cancel_atimer (struct atimer *timer)
158 int i;
160 BLOCK_ATIMERS;
162 for (i = 0; i < 2; ++i)
164 struct atimer *t, *prev;
165 struct atimer **list = i ? &stopped_atimers : &atimers;
167 /* See if TIMER is active or stopped. */
168 for (t = *list, prev = NULL; t && t != timer; prev = t, t = t->next)
171 /* If it is, take it off its list, and put in on the free-list.
172 We don't bother to arrange for setting a different alarm time,
173 since a too early one doesn't hurt. */
174 if (t)
176 if (prev)
177 prev->next = t->next;
178 else
179 *list = t->next;
181 t->next = free_atimers;
182 free_atimers = t;
183 break;
187 UNBLOCK_ATIMERS;
191 /* Append two lists of atimers LIST1 and LIST2 and return the
192 result list. */
194 static struct atimer *
195 append_atimer_lists (struct atimer *list1, struct atimer *list2)
197 if (list1 == NULL)
198 return list2;
199 else if (list2 == NULL)
200 return list1;
201 else
203 struct atimer *p;
205 for (p = list1; p->next; p = p->next)
207 p->next = list2;
208 return list1;
213 /* Stop all timers except timer T. T null means stop all timers. */
215 void
216 stop_other_atimers (struct atimer *t)
218 BLOCK_ATIMERS;
220 if (t)
222 struct atimer *p, *prev;
224 /* See if T is active. */
225 for (p = atimers, prev = NULL; p && p != t; prev = p, p = p->next)
228 if (p == t)
230 if (prev)
231 prev->next = t->next;
232 else
233 atimers = t->next;
234 t->next = NULL;
236 else
237 /* T is not active. Let's handle this like T == 0. */
238 t = NULL;
241 stopped_atimers = append_atimer_lists (atimers, stopped_atimers);
242 atimers = t;
243 UNBLOCK_ATIMERS;
247 /* Run all timers again, if some have been stopped with a call to
248 stop_other_atimers. */
250 void
251 run_all_atimers (void)
253 if (stopped_atimers)
255 struct atimer *t = atimers;
256 struct atimer *next;
258 BLOCK_ATIMERS;
259 atimers = stopped_atimers;
260 stopped_atimers = NULL;
262 while (t)
264 next = t->next;
265 schedule_atimer (t);
266 t = next;
269 UNBLOCK_ATIMERS;
274 /* A version of run_all_timers suitable for a record_unwind_protect. */
276 Lisp_Object
277 unwind_stop_other_atimers (Lisp_Object dummy)
279 run_all_atimers ();
280 return Qnil;
284 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
286 static void
287 set_alarm (void)
289 if (atimers)
291 EMACS_TIME now, time;
292 #ifdef HAVE_SETITIMER
293 struct itimerval it;
294 #endif
296 /* Determine s/us till the next timer is ripe. */
297 EMACS_GET_TIME (now);
298 EMACS_SUB_TIME (time, atimers->expiration, now);
300 #ifdef HAVE_SETITIMER
301 /* Don't set the interval to 0; this disables the timer. */
302 if (EMACS_TIME_LE (atimers->expiration, now))
304 EMACS_SET_SECS (time, 0);
305 EMACS_SET_USECS (time, 1000);
308 memset (&it, 0, sizeof it);
309 it.it_value = time;
310 setitimer (ITIMER_REAL, &it, 0);
311 #else /* not HAVE_SETITIMER */
312 alarm (max (EMACS_SECS (time), 1));
313 #endif /* not HAVE_SETITIMER */
318 /* Insert timer T into the list of active atimers `atimers', keeping
319 the list sorted by expiration time. T must not be in this list
320 already. */
322 static void
323 schedule_atimer (struct atimer *t)
325 struct atimer *a = atimers, *prev = NULL;
327 /* Look for the first atimer that is ripe after T. */
328 while (a && EMACS_TIME_GT (t->expiration, a->expiration))
329 prev = a, a = a->next;
331 /* Insert T in front of the atimer found, if any. */
332 if (prev)
333 prev->next = t;
334 else
335 atimers = t;
337 t->next = a;
340 static void
341 run_timers (void)
343 EMACS_TIME now;
345 EMACS_GET_TIME (now);
347 while (atimers
348 && (pending_atimers = interrupt_input_blocked) == 0
349 && EMACS_TIME_LE (atimers->expiration, now))
351 struct atimer *t;
353 t = atimers;
354 atimers = atimers->next;
355 t->fn (t);
357 if (t->type == ATIMER_CONTINUOUS)
359 EMACS_ADD_TIME (t->expiration, now, t->interval);
360 schedule_atimer (t);
362 else
364 t->next = free_atimers;
365 free_atimers = t;
368 EMACS_GET_TIME (now);
371 if (! atimers)
372 pending_atimers = 0;
374 #ifdef SYNC_INPUT
375 if (pending_atimers)
376 pending_signals = 1;
377 else
379 pending_signals = interrupt_input_pending;
380 set_alarm ();
382 #else
383 if (! pending_atimers)
384 set_alarm ();
385 #endif
389 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
390 SIGALRM. */
392 SIGTYPE
393 alarm_signal_handler (int signo)
395 #ifndef SYNC_INPUT
396 SIGNAL_THREAD_CHECK (signo);
397 #endif
399 pending_atimers = 1;
400 #ifdef SYNC_INPUT
401 pending_signals = 1;
402 #else
403 run_timers ();
404 #endif
408 /* Call alarm_signal_handler for pending timers. */
410 void
411 do_pending_atimers (void)
413 if (pending_atimers)
415 BLOCK_ATIMERS;
416 run_timers ();
417 UNBLOCK_ATIMERS;
422 /* Turn alarms on/off. This seems to be temporarily necessary on
423 some systems like HPUX (see process.c). */
425 void
426 turn_on_atimers (int on)
428 if (on)
430 signal (SIGALRM, alarm_signal_handler);
431 set_alarm ();
433 else
434 alarm (0);
438 void
439 init_atimer (void)
441 free_atimers = stopped_atimers = atimers = NULL;
442 pending_atimers = 0;
443 /* pending_signals is initialized in init_keyboard.*/
444 signal (SIGALRM, alarm_signal_handler);