Rearrange pointer logging
[emacs.git] / src / atimer.c
bloba5a2b0714e3b82570c2b2efc864beeb17542b67a
1 /* Asynchronous timers.
2 Copyright (C) 2000-2014 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 block_atimers (sigset_t *oldset)
55 sigset_t blocked;
56 sigemptyset (&blocked);
57 sigaddset (&blocked, SIGALRM);
58 pthread_sigmask (SIG_BLOCK, &blocked, oldset);
60 static void
61 unblock_atimers (sigset_t const *oldset)
63 pthread_sigmask (SIG_SETMASK, oldset, 0);
66 /* Function prototypes. */
68 static void set_alarm (void);
69 static void schedule_atimer (struct atimer *);
70 static struct atimer *append_atimer_lists (struct atimer *,
71 struct atimer *);
73 /* Start a new atimer of type TYPE. TIME specifies when the timer is
74 ripe. FN is the function to call when the timer fires.
75 CLIENT_DATA is stored in the client_data member of the atimer
76 structure returned and so made available to FN when it is called.
78 If TYPE is ATIMER_ABSOLUTE, TIME is the absolute time at which the
79 timer fires.
81 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the
82 future.
84 In both cases, the timer is automatically freed after it has fired.
86 If TYPE is ATIMER_CONTINUOUS, the timer fires every TIME s/us.
88 Value is a pointer to the atimer started. It can be used in calls
89 to cancel_atimer; don't free it yourself. */
91 struct atimer *
92 start_atimer (enum atimer_type type, struct timespec timestamp,
93 atimer_callback fn, void *client_data)
95 struct atimer *t;
96 sigset_t oldset;
98 /* Round TIME up to the next full second if we don't have
99 itimers. */
100 #ifndef HAVE_SETITIMER
101 if (timestamp.tv_nsec != 0 && timestamp.tv_sec < TYPE_MAXIMUM (time_t))
102 timestamp = make_timespec (timestamp.tv_sec + 1, 0);
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 = 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 (&oldset);
123 /* Compute the timer's expiration time. */
124 switch (type)
126 case ATIMER_ABSOLUTE:
127 t->expiration = timestamp;
128 break;
130 case ATIMER_RELATIVE:
131 t->expiration = timespec_add (current_timespec (), timestamp);
132 break;
134 case ATIMER_CONTINUOUS:
135 t->expiration = timespec_add (current_timespec (), timestamp);
136 t->interval = timestamp;
137 break;
140 /* Insert the timer in the list of active atimers. */
141 schedule_atimer (t);
142 unblock_atimers (&oldset);
144 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
145 set_alarm ();
147 return t;
151 /* Cancel and free atimer TIMER. */
153 void
154 cancel_atimer (struct atimer *timer)
156 int i;
157 sigset_t oldset;
159 block_atimers (&oldset);
161 for (i = 0; i < 2; ++i)
163 struct atimer *t, *prev;
164 struct atimer **list = i ? &stopped_atimers : &atimers;
166 /* See if TIMER is active or stopped. */
167 for (t = *list, prev = NULL; t && t != timer; prev = t, t = t->next)
170 /* If it is, take it off its list, and put in on the free-list.
171 We don't bother to arrange for setting a different alarm time,
172 since a too early one doesn't hurt. */
173 if (t)
175 if (prev)
176 prev->next = t->next;
177 else
178 *list = t->next;
180 t->next = free_atimers;
181 free_atimers = t;
182 break;
186 unblock_atimers (&oldset);
190 /* Append two lists of atimers LIST_1 and LIST_2 and return the
191 result list. */
193 static struct atimer *
194 append_atimer_lists (struct atimer *list_1, struct atimer *list_2)
196 if (list_1 == NULL)
197 return list_2;
198 else if (list_2 == NULL)
199 return list_1;
200 else
202 struct atimer *p;
204 for (p = list_1; p->next; p = p->next)
206 p->next = list_2;
207 return list_1;
212 /* Stop all timers except timer T. T null means stop all timers. */
214 void
215 stop_other_atimers (struct atimer *t)
217 sigset_t oldset;
218 block_atimers (&oldset);
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 (&oldset);
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;
257 sigset_t oldset;
259 block_atimers (&oldset);
260 atimers = stopped_atimers;
261 stopped_atimers = NULL;
263 while (t)
265 next = t->next;
266 schedule_atimer (t);
267 t = next;
270 unblock_atimers (&oldset);
275 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
277 static void
278 set_alarm (void)
280 if (atimers)
282 #ifdef HAVE_SETITIMER
283 struct itimerval it;
284 #endif
285 struct timespec now, interval;
287 #ifdef HAVE_ITIMERSPEC
288 if (alarm_timer_ok)
290 struct itimerspec ispec;
291 ispec.it_value = atimers->expiration;
292 ispec.it_interval.tv_sec = ispec.it_interval.tv_nsec = 0;
293 if (timer_settime (alarm_timer, 0, &ispec, 0) == 0)
294 return;
296 #endif
298 /* Determine interval till the next timer is ripe.
299 Don't set the interval to 0; this disables the timer. */
300 now = current_timespec ();
301 interval = (timespec_cmp (atimers->expiration, now) <= 0
302 ? make_timespec (0, 1000 * 1000)
303 : timespec_sub (atimers->expiration, now));
305 #ifdef HAVE_SETITIMER
307 memset (&it, 0, sizeof it);
308 it.it_value = make_timeval (interval);
309 setitimer (ITIMER_REAL, &it, 0);
310 #else /* not HAVE_SETITIMER */
311 alarm (max (interval.tv_sec, 1));
312 #endif /* not HAVE_SETITIMER */
317 /* Insert timer T into the list of active atimers `atimers', keeping
318 the list sorted by expiration time. T must not be in this list
319 already. */
321 static void
322 schedule_atimer (struct atimer *t)
324 struct atimer *a = atimers, *prev = NULL;
326 /* Look for the first atimer that is ripe after T. */
327 while (a && timespec_cmp (a->expiration, t->expiration) < 0)
328 prev = a, a = a->next;
330 /* Insert T in front of the atimer found, if any. */
331 if (prev)
332 prev->next = t;
333 else
334 atimers = t;
336 t->next = a;
339 static void
340 run_timers (void)
342 struct timespec now = current_timespec ();
344 while (atimers && timespec_cmp (atimers->expiration, now) <= 0)
346 struct atimer *t = atimers;
347 atimers = atimers->next;
348 t->fn (t);
350 if (t->type == ATIMER_CONTINUOUS)
352 t->expiration = timespec_add (now, t->interval);
353 schedule_atimer (t);
355 else
357 t->next = free_atimers;
358 free_atimers = t;
362 set_alarm ();
366 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
367 SIGALRM. */
369 static void
370 handle_alarm_signal (int sig)
372 pending_signals = 1;
376 /* Do pending timers. */
378 void
379 do_pending_atimers (void)
381 if (atimers)
383 sigset_t oldset;
384 block_atimers (&oldset);
385 run_timers ();
386 unblock_atimers (&oldset);
391 /* Turn alarms on/off. This seems to be temporarily necessary on
392 some systems like HPUX (see process.c). */
394 void
395 turn_on_atimers (bool on)
397 if (on)
398 set_alarm ();
399 else
400 alarm (0);
404 void
405 init_atimer (void)
407 struct sigaction action;
408 #ifdef HAVE_ITIMERSPEC
409 struct sigevent sigev;
410 sigev.sigev_notify = SIGEV_SIGNAL;
411 sigev.sigev_signo = SIGALRM;
412 sigev.sigev_value.sival_ptr = &alarm_timer;
413 alarm_timer_ok = timer_create (CLOCK_REALTIME, &sigev, &alarm_timer) == 0;
414 #endif
415 free_atimers = stopped_atimers = atimers = NULL;
416 /* pending_signals is initialized in init_keyboard.*/
417 emacs_sigaction_init (&action, handle_alarm_signal);
418 sigaction (SIGALRM, &action, 0);