lisp/dired-x.el: Fix bug#14927.
[emacs.git] / src / atimer.c
blob219b3502acc3d1f4dafc5b07b90ec385758859f1
1 /* Asynchronous timers.
2 Copyright (C) 2000-2013 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 /* The alarm timer and whether it was properly initialized, if
44 POSIX timers are available. */
45 #ifdef HAVE_ITIMERSPEC
46 static timer_t alarm_timer;
47 static bool alarm_timer_ok;
48 #endif
50 /* Block/unblock SIGALRM. */
52 static void
53 sigmask_atimers (int how)
55 sigset_t blocked;
56 sigemptyset (&blocked);
57 sigaddset (&blocked, SIGALRM);
58 pthread_sigmask (how, &blocked, 0);
60 static void
61 block_atimers (void)
63 sigmask_atimers (SIG_BLOCK);
65 static void
66 unblock_atimers (void)
68 sigmask_atimers (SIG_UNBLOCK);
71 /* Function prototypes. */
73 static void set_alarm (void);
74 static void schedule_atimer (struct atimer *);
75 static struct atimer *append_atimer_lists (struct atimer *,
76 struct atimer *);
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.
83 If TYPE is ATIMER_ABSOLUTE, TIME is the absolute time at which the
84 timer fires.
86 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the
87 future.
89 In both cases, the timer is automatically freed after it has fired.
91 If TYPE is ATIMER_CONTINUOUS, the timer fires every TIME s/us.
93 Value is a pointer to the atimer started. It can be used in calls
94 to cancel_atimer; don't free it yourself. */
96 struct atimer *
97 start_atimer (enum atimer_type type, EMACS_TIME timestamp, atimer_callback fn,
98 void *client_data)
100 struct atimer *t;
102 /* Round TIME up to the next full second if we don't have
103 itimers. */
104 #ifndef HAVE_SETITIMER
105 if (EMACS_NSECS (timestamp) != 0
106 && EMACS_SECS (timestamp) < TYPE_MAXIMUM (time_t))
107 timestamp = make_emacs_time (EMACS_SECS (timestamp) + 1, 0);
108 #endif /* not HAVE_SETITIMER */
110 /* Get an atimer structure from the free-list, or allocate
111 a new one. */
112 if (free_atimers)
114 t = free_atimers;
115 free_atimers = t->next;
117 else
118 t = xmalloc (sizeof *t);
120 /* Fill the atimer structure. */
121 memset (t, 0, sizeof *t);
122 t->type = type;
123 t->fn = fn;
124 t->client_data = client_data;
126 block_atimers ();
128 /* Compute the timer's expiration time. */
129 switch (type)
131 case ATIMER_ABSOLUTE:
132 t->expiration = timestamp;
133 break;
135 case ATIMER_RELATIVE:
136 t->expiration = add_emacs_time (current_emacs_time (), timestamp);
137 break;
139 case ATIMER_CONTINUOUS:
140 t->expiration = add_emacs_time (current_emacs_time (), timestamp);
141 t->interval = timestamp;
142 break;
145 /* Insert the timer in the list of active atimers. */
146 schedule_atimer (t);
147 unblock_atimers ();
149 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
150 set_alarm ();
152 return t;
156 /* Cancel and free atimer TIMER. */
158 void
159 cancel_atimer (struct atimer *timer)
161 int i;
163 block_atimers ();
165 for (i = 0; i < 2; ++i)
167 struct atimer *t, *prev;
168 struct atimer **list = i ? &stopped_atimers : &atimers;
170 /* See if TIMER is active or stopped. */
171 for (t = *list, prev = NULL; t && t != timer; prev = t, t = t->next)
174 /* If it is, take it off its list, and put in on the free-list.
175 We don't bother to arrange for setting a different alarm time,
176 since a too early one doesn't hurt. */
177 if (t)
179 if (prev)
180 prev->next = t->next;
181 else
182 *list = t->next;
184 t->next = free_atimers;
185 free_atimers = t;
186 break;
190 unblock_atimers ();
194 /* Append two lists of atimers LIST_1 and LIST_2 and return the
195 result list. */
197 static struct atimer *
198 append_atimer_lists (struct atimer *list_1, struct atimer *list_2)
200 if (list_1 == NULL)
201 return list_2;
202 else if (list_2 == NULL)
203 return list_1;
204 else
206 struct atimer *p;
208 for (p = list_1; p->next; p = p->next)
210 p->next = list_2;
211 return list_1;
216 /* Stop all timers except timer T. T null means stop all timers. */
218 void
219 stop_other_atimers (struct atimer *t)
221 block_atimers ();
223 if (t)
225 struct atimer *p, *prev;
227 /* See if T is active. */
228 for (p = atimers, prev = NULL; p && p != t; prev = p, p = p->next)
231 if (p == t)
233 if (prev)
234 prev->next = t->next;
235 else
236 atimers = t->next;
237 t->next = NULL;
239 else
240 /* T is not active. Let's handle this like T == 0. */
241 t = NULL;
244 stopped_atimers = append_atimer_lists (atimers, stopped_atimers);
245 atimers = t;
246 unblock_atimers ();
250 /* Run all timers again, if some have been stopped with a call to
251 stop_other_atimers. */
253 void
254 run_all_atimers (void)
256 if (stopped_atimers)
258 struct atimer *t = atimers;
259 struct atimer *next;
261 block_atimers ();
262 atimers = stopped_atimers;
263 stopped_atimers = NULL;
265 while (t)
267 next = t->next;
268 schedule_atimer (t);
269 t = next;
272 unblock_atimers ();
277 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
279 static void
280 set_alarm (void)
282 if (atimers)
284 #ifdef HAVE_SETITIMER
285 struct itimerval it;
286 #endif
287 EMACS_TIME now, interval;
289 #ifdef HAVE_ITIMERSPEC
290 if (alarm_timer_ok)
292 struct itimerspec ispec;
293 ispec.it_value = atimers->expiration;
294 ispec.it_interval.tv_sec = ispec.it_interval.tv_nsec = 0;
295 if (timer_settime (alarm_timer, 0, &ispec, 0) == 0)
296 return;
298 #endif
300 /* Determine interval till the next timer is ripe.
301 Don't set the interval to 0; this disables the timer. */
302 now = current_emacs_time ();
303 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_LT (a->expiration, t->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 = current_emacs_time ();
346 while (atimers && EMACS_TIME_LE (atimers->expiration, now))
348 struct atimer *t = atimers;
349 atimers = atimers->next;
350 t->fn (t);
352 if (t->type == ATIMER_CONTINUOUS)
354 t->expiration = add_emacs_time (now, t->interval);
355 schedule_atimer (t);
357 else
359 t->next = free_atimers;
360 free_atimers = t;
364 set_alarm ();
368 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
369 SIGALRM. */
371 static void
372 handle_alarm_signal (int sig)
374 pending_signals = 1;
378 /* Do pending timers. */
380 void
381 do_pending_atimers (void)
383 if (atimers)
385 block_atimers ();
386 run_timers ();
387 unblock_atimers ();
392 /* Turn alarms on/off. This seems to be temporarily necessary on
393 some systems like HPUX (see process.c). */
395 void
396 turn_on_atimers (bool on)
398 if (on)
399 set_alarm ();
400 else
401 alarm (0);
405 void
406 init_atimer (void)
408 struct sigaction action;
409 #ifdef HAVE_ITIMERSPEC
410 struct sigevent sigev;
411 sigev.sigev_notify = SIGEV_SIGNAL;
412 sigev.sigev_signo = SIGALRM;
413 sigev.sigev_value.sival_ptr = &alarm_timer;
414 alarm_timer_ok = timer_create (CLOCK_REALTIME, &sigev, &alarm_timer) == 0;
415 #endif
416 free_atimers = stopped_atimers = atimers = NULL;
417 /* pending_signals is initialized in init_keyboard.*/
418 emacs_sigaction_init (&action, handle_alarm_signal);
419 sigaction (SIGALRM, &action, 0);