Fix usage of @itemx in ELisp manual.
[emacs.git] / src / atimer.c
blob73c7aa5686bb90dfdf3aac9562909181a3daeb88
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 static 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 /* A version of run_all_atimers suitable for a record_unwind_protect. */
279 Lisp_Object
280 unwind_stop_other_atimers (Lisp_Object dummy)
282 run_all_atimers ();
283 return Qnil;
287 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
289 static void
290 set_alarm (void)
292 if (atimers)
294 #ifdef HAVE_SETITIMER
295 struct itimerval it;
296 #endif
297 EMACS_TIME now, interval;
299 #ifdef HAVE_ITIMERSPEC
300 if (alarm_timer_ok)
302 struct itimerspec ispec;
303 ispec.it_value = atimers->expiration;
304 ispec.it_interval.tv_sec = ispec.it_interval.tv_nsec = 0;
305 if (timer_settime (alarm_timer, 0, &ispec, 0) == 0)
306 return;
308 #endif
310 /* Determine interval till the next timer is ripe.
311 Don't set the interval to 0; this disables the timer. */
312 now = current_emacs_time ();
313 interval = (EMACS_TIME_LE (atimers->expiration, now)
314 ? make_emacs_time (0, 1000 * 1000)
315 : sub_emacs_time (atimers->expiration, now));
317 #ifdef HAVE_SETITIMER
319 memset (&it, 0, sizeof it);
320 it.it_value = make_timeval (interval);
321 setitimer (ITIMER_REAL, &it, 0);
322 #else /* not HAVE_SETITIMER */
323 alarm (max (EMACS_SECS (interval), 1));
324 #endif /* not HAVE_SETITIMER */
329 /* Insert timer T into the list of active atimers `atimers', keeping
330 the list sorted by expiration time. T must not be in this list
331 already. */
333 static void
334 schedule_atimer (struct atimer *t)
336 struct atimer *a = atimers, *prev = NULL;
338 /* Look for the first atimer that is ripe after T. */
339 while (a && EMACS_TIME_GT (t->expiration, a->expiration))
340 prev = a, a = a->next;
342 /* Insert T in front of the atimer found, if any. */
343 if (prev)
344 prev->next = t;
345 else
346 atimers = t;
348 t->next = a;
351 static void
352 run_timers (void)
354 EMACS_TIME now = current_emacs_time ();
356 while (atimers && EMACS_TIME_LE (atimers->expiration, now))
358 struct atimer *t = atimers;
359 atimers = atimers->next;
360 t->fn (t);
362 if (t->type == ATIMER_CONTINUOUS)
364 t->expiration = add_emacs_time (now, t->interval);
365 schedule_atimer (t);
367 else
369 t->next = free_atimers;
370 free_atimers = t;
374 set_alarm ();
378 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
379 SIGALRM. */
381 static void
382 handle_alarm_signal (int sig)
384 pending_signals = 1;
388 /* Do pending timers. */
390 void
391 do_pending_atimers (void)
393 if (atimers)
395 block_atimers ();
396 run_timers ();
397 unblock_atimers ();
402 /* Turn alarms on/off. This seems to be temporarily necessary on
403 some systems like HPUX (see process.c). */
405 void
406 turn_on_atimers (bool on)
408 if (on)
409 set_alarm ();
410 else
411 alarm (0);
415 void
416 init_atimer (void)
418 struct sigaction action;
419 #ifdef HAVE_ITIMERSPEC
420 struct sigevent sigev;
421 sigev.sigev_notify = SIGEV_SIGNAL;
422 sigev.sigev_signo = SIGALRM;
423 sigev.sigev_value.sival_ptr = &alarm_timer;
424 alarm_timer_ok = timer_create (CLOCK_REALTIME, &sigev, &alarm_timer) == 0;
425 #endif
426 free_atimers = stopped_atimers = atimers = NULL;
427 /* pending_signals is initialized in init_keyboard.*/
428 emacs_sigaction_init (&action, handle_alarm_signal);
429 sigaction (SIGALRM, &action, 0);