Make pcomplete less eager to add an extra space.
[emacs.git] / src / atimer.c
blob5dbd807872ae8f56887daf42f939aa721f6a6da7
1 /* Asynchronous timers.
2 Copyright (C) 2000-2012 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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 3 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19 #include <config.h>
20 #include <stdio.h>
22 #include "lisp.h"
23 #include "syssignal.h"
24 #include "systime.h"
25 #include "blockinput.h"
26 #include "atimer.h"
27 #include <unistd.h>
29 /* Free-list of atimer structures. */
31 static struct atimer *free_atimers;
33 /* List of currently not running timers due to a call to
34 lock_atimer. */
36 static struct atimer *stopped_atimers;
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. */
41 static struct atimer *atimers;
43 /* Non-zero means alarm signal handler has found ripe timers but
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. */
49 int pending_atimers;
51 /* Block/unblock SIGALRM. */
53 static void
54 sigmask_atimers (int how)
56 sigset_t blocked;
57 sigemptyset (&blocked);
58 sigaddset (&blocked, SIGALRM);
59 pthread_sigmask (how, &blocked, 0);
61 static void
62 block_atimers (void)
64 sigmask_atimers (SIG_BLOCK);
66 static void
67 unblock_atimers (void)
69 sigmask_atimers (SIG_UNBLOCK);
72 /* Function prototypes. */
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 *);
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.
84 If TYPE is ATIMER_ABSOLUTE, TIME is the absolute time at which the
85 timer fires.
87 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the
88 future.
90 In both cases, the timer is automatically freed after it has fired.
92 If TYPE is ATIMER_CONTINUOUS, the timer fires every TIME s/us.
94 Value is a pointer to the atimer started. It can be used in calls
95 to cancel_atimer; don't free it yourself. */
97 struct atimer *
98 start_atimer (enum atimer_type type, EMACS_TIME timestamp, atimer_callback fn,
99 void *client_data)
101 struct atimer *t;
103 /* Round TIME up to the next full second if we don't have
104 itimers. */
105 #ifndef HAVE_SETITIMER
106 if (EMACS_NSECS (timestamp) != 0
107 && EMACS_SECS (timestamp) < TYPE_MAXIMUM (time_t))
108 timestamp = make_emacs_time (EMACS_SECS (timestamp) + 1, 0);
109 #endif /* not HAVE_SETITIMER */
111 /* Get an atimer structure from the free-list, or allocate
112 a new one. */
113 if (free_atimers)
115 t = free_atimers;
116 free_atimers = t->next;
118 else
119 t = xmalloc (sizeof *t);
121 /* Fill the atimer structure. */
122 memset (t, 0, sizeof *t);
123 t->type = type;
124 t->fn = fn;
125 t->client_data = client_data;
127 block_atimers ();
129 /* Compute the timer's expiration time. */
130 switch (type)
132 case ATIMER_ABSOLUTE:
133 t->expiration = timestamp;
134 break;
136 case ATIMER_RELATIVE:
137 t->expiration = add_emacs_time (current_emacs_time (), timestamp);
138 break;
140 case ATIMER_CONTINUOUS:
141 t->expiration = add_emacs_time (current_emacs_time (), timestamp);
142 t->interval = timestamp;
143 break;
146 /* Insert the timer in the list of active atimers. */
147 schedule_atimer (t);
148 unblock_atimers ();
150 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
151 set_alarm ();
153 return t;
157 /* Cancel and free atimer TIMER. */
159 void
160 cancel_atimer (struct atimer *timer)
162 int i;
164 block_atimers ();
166 for (i = 0; i < 2; ++i)
168 struct atimer *t, *prev;
169 struct atimer **list = i ? &stopped_atimers : &atimers;
171 /* See if TIMER is active or stopped. */
172 for (t = *list, prev = NULL; t && t != timer; prev = t, t = t->next)
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. */
178 if (t)
180 if (prev)
181 prev->next = t->next;
182 else
183 *list = t->next;
185 t->next = free_atimers;
186 free_atimers = t;
187 break;
191 unblock_atimers ();
195 /* Append two lists of atimers LIST_1 and LIST_2 and return the
196 result list. */
198 static struct atimer *
199 append_atimer_lists (struct atimer *list_1, struct atimer *list_2)
201 if (list_1 == NULL)
202 return list_2;
203 else if (list_2 == NULL)
204 return list_1;
205 else
207 struct atimer *p;
209 for (p = list_1; p->next; p = p->next)
211 p->next = list_2;
212 return list_1;
217 /* Stop all timers except timer T. T null means stop all timers. */
219 void
220 stop_other_atimers (struct atimer *t)
222 block_atimers ();
224 if (t)
226 struct atimer *p, *prev;
228 /* See if T is active. */
229 for (p = atimers, prev = NULL; p && p != t; prev = p, p = p->next)
232 if (p == t)
234 if (prev)
235 prev->next = t->next;
236 else
237 atimers = t->next;
238 t->next = NULL;
240 else
241 /* T is not active. Let's handle this like T == 0. */
242 t = NULL;
245 stopped_atimers = append_atimer_lists (atimers, stopped_atimers);
246 atimers = t;
247 unblock_atimers ();
251 /* Run all timers again, if some have been stopped with a call to
252 stop_other_atimers. */
254 static void
255 run_all_atimers (void)
257 if (stopped_atimers)
259 struct atimer *t = atimers;
260 struct atimer *next;
262 block_atimers ();
263 atimers = stopped_atimers;
264 stopped_atimers = NULL;
266 while (t)
268 next = t->next;
269 schedule_atimer (t);
270 t = next;
273 unblock_atimers ();
278 /* A version of run_all_atimers suitable for a record_unwind_protect. */
280 Lisp_Object
281 unwind_stop_other_atimers (Lisp_Object dummy)
283 run_all_atimers ();
284 return Qnil;
288 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
290 static void
291 set_alarm (void)
293 if (atimers)
295 #ifdef HAVE_SETITIMER
296 struct itimerval it;
297 #endif
299 /* Determine s/us till the next timer is ripe. */
300 EMACS_TIME now = current_emacs_time ();
302 /* Don't set the interval to 0; this disables the timer. */
303 EMACS_TIME interval = (EMACS_TIME_LE (atimers->expiration, now)
304 ? make_emacs_time (0, 1000 * 1000)
305 : sub_emacs_time (atimers->expiration, now));
307 #ifdef HAVE_SETITIMER
309 memset (&it, 0, sizeof it);
310 it.it_value = make_timeval (interval);
311 setitimer (ITIMER_REAL, &it, 0);
312 #else /* not HAVE_SETITIMER */
313 alarm (max (EMACS_SECS (interval), 1));
314 #endif /* not HAVE_SETITIMER */
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. */
323 static void
324 schedule_atimer (struct atimer *t)
326 struct atimer *a = atimers, *prev = NULL;
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;
332 /* Insert T in front of the atimer found, if any. */
333 if (prev)
334 prev->next = t;
335 else
336 atimers = t;
338 t->next = a;
341 static void
342 run_timers (void)
344 EMACS_TIME now;
346 while (atimers
347 && (pending_atimers = interrupt_input_blocked) == 0
348 && (now = current_emacs_time (),
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 t->expiration = add_emacs_time (now, t->interval);
360 schedule_atimer (t);
362 else
364 t->next = free_atimers;
365 free_atimers = t;
369 if (! atimers)
370 pending_atimers = 0;
372 if (pending_atimers)
373 pending_signals = 1;
374 else
376 pending_signals = interrupt_input_pending;
377 set_alarm ();
382 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
383 SIGALRM. */
385 static void
386 handle_alarm_signal (int sig)
388 pending_atimers = 1;
389 pending_signals = 1;
392 static void
393 deliver_alarm_signal (int sig)
395 handle_on_main_thread (sig, handle_alarm_signal);
399 /* Call alarm signal handler for pending timers. */
401 void
402 do_pending_atimers (void)
404 if (pending_atimers)
406 block_atimers ();
407 run_timers ();
408 unblock_atimers ();
413 /* Turn alarms on/off. This seems to be temporarily necessary on
414 some systems like HPUX (see process.c). */
416 void
417 turn_on_atimers (bool on)
419 if (on)
421 struct sigaction action;
422 emacs_sigaction_init (&action, deliver_alarm_signal);
423 sigaction (SIGALRM, &action, 0);
424 set_alarm ();
426 else
427 alarm (0);
431 void
432 init_atimer (void)
434 struct sigaction action;
435 free_atimers = stopped_atimers = atimers = NULL;
436 pending_atimers = 0;
437 /* pending_signals is initialized in init_keyboard.*/
438 emacs_sigaction_init (&action, deliver_alarm_signal);
439 sigaction (SIGALRM, &action, 0);