Avoid leaving garbage on screen when using 'raise' display property
[emacs.git] / src / atimer.c
blob5feb1f6777d990e9836bb69b6536ac7e5024d307
1 /* Asynchronous timers.
2 Copyright (C) 2000-2017 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 (at
9 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 "keyboard.h"
24 #include "syssignal.h"
25 #include "systime.h"
26 #include "atimer.h"
27 #include <unistd.h>
29 #ifdef HAVE_TIMERFD
30 #include <errno.h>
31 # include <sys/timerfd.h>
32 #endif
34 #ifdef MSDOS
35 #include "msdos.h"
36 #endif
38 /* Free-list of atimer structures. */
40 static struct atimer *free_atimers;
42 /* List of currently not running timers due to a call to
43 lock_atimer. */
45 static struct atimer *stopped_atimers;
47 /* List of active atimers, sorted by expiration time. The timer that
48 will become ripe next is always at the front of this list. */
50 static struct atimer *atimers;
52 #ifdef HAVE_ITIMERSPEC
53 /* The alarm timer and whether it was properly initialized, if
54 POSIX timers are available. */
55 static timer_t alarm_timer;
56 static bool alarm_timer_ok;
58 # ifdef HAVE_TIMERFD
59 /* File descriptor for timer, or -1 if it could not be created. */
60 static int timerfd;
61 # else
62 enum { timerfd = -1 };
63 # endif
64 #endif
66 /* Block/unblock SIGALRM. */
68 static void
69 block_atimers (sigset_t *oldset)
71 sigset_t blocked;
72 sigemptyset (&blocked);
73 sigaddset (&blocked, SIGALRM);
74 sigaddset (&blocked, SIGINT);
75 pthread_sigmask (SIG_BLOCK, &blocked, oldset);
77 static void
78 unblock_atimers (sigset_t const *oldset)
80 pthread_sigmask (SIG_SETMASK, oldset, 0);
83 /* Function prototypes. */
85 static void set_alarm (void);
86 static void schedule_atimer (struct atimer *);
87 static struct atimer *append_atimer_lists (struct atimer *,
88 struct atimer *);
90 /* Start a new atimer of type TYPE. TIMESTAMP specifies when the timer is
91 ripe. FN is the function to call when the timer fires.
92 CLIENT_DATA is stored in the client_data member of the atimer
93 structure returned and so made available to FN when it is called.
95 If TYPE is ATIMER_ABSOLUTE, TIMESTAMP is the absolute time at which the
96 timer fires.
98 If TYPE is ATIMER_RELATIVE, the timer is ripe TIMESTAMP seconds in the
99 future.
101 In both cases, the timer is automatically freed after it has fired.
103 If TYPE is ATIMER_CONTINUOUS, the timer fires every TIMESTAMP seconds.
105 Value is a pointer to the atimer started. It can be used in calls
106 to cancel_atimer; don't free it yourself. */
108 struct atimer *
109 start_atimer (enum atimer_type type, struct timespec timestamp,
110 atimer_callback fn, void *client_data)
112 struct atimer *t;
113 sigset_t oldset;
115 /* Round TIMESTAMP up to the next full second if we don't have itimers. */
116 #ifndef HAVE_SETITIMER
117 if (timestamp.tv_nsec != 0 && timestamp.tv_sec < TYPE_MAXIMUM (time_t))
118 timestamp = make_timespec (timestamp.tv_sec + 1, 0);
119 #endif /* not HAVE_SETITIMER */
121 /* Get an atimer structure from the free-list, or allocate
122 a new one. */
123 if (free_atimers)
125 t = free_atimers;
126 free_atimers = t->next;
128 else
129 t = xmalloc (sizeof *t);
131 /* Fill the atimer structure. */
132 memset (t, 0, sizeof *t);
133 t->type = type;
134 t->fn = fn;
135 t->client_data = client_data;
137 block_atimers (&oldset);
139 /* Compute the timer's expiration time. */
140 switch (type)
142 case ATIMER_ABSOLUTE:
143 t->expiration = timestamp;
144 break;
146 case ATIMER_RELATIVE:
147 t->expiration = timespec_add (current_timespec (), timestamp);
148 break;
150 case ATIMER_CONTINUOUS:
151 t->expiration = timespec_add (current_timespec (), timestamp);
152 t->interval = timestamp;
153 break;
156 /* Insert the timer in the list of active atimers. */
157 schedule_atimer (t);
158 unblock_atimers (&oldset);
160 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
161 set_alarm ();
163 return t;
167 /* Cancel and free atimer TIMER. */
169 void
170 cancel_atimer (struct atimer *timer)
172 int i;
173 sigset_t oldset;
175 block_atimers (&oldset);
177 for (i = 0; i < 2; ++i)
179 struct atimer *t, *prev;
180 struct atimer **list = i ? &stopped_atimers : &atimers;
182 /* See if TIMER is active or stopped. */
183 for (t = *list, prev = NULL; t && t != timer; prev = t, t = t->next)
186 /* If it is, take it off its list, and put in on the free-list.
187 We don't bother to arrange for setting a different alarm time,
188 since a too early one doesn't hurt. */
189 if (t)
191 if (prev)
192 prev->next = t->next;
193 else
194 *list = t->next;
196 t->next = free_atimers;
197 free_atimers = t;
198 break;
202 unblock_atimers (&oldset);
206 /* Append two lists of atimers LIST_1 and LIST_2 and return the
207 result list. */
209 static struct atimer *
210 append_atimer_lists (struct atimer *list_1, struct atimer *list_2)
212 if (list_1 == NULL)
213 return list_2;
214 else if (list_2 == NULL)
215 return list_1;
216 else
218 struct atimer *p;
220 for (p = list_1; p->next; p = p->next)
222 p->next = list_2;
223 return list_1;
228 /* Stop all timers except timer T. T null means stop all timers. */
230 void
231 stop_other_atimers (struct atimer *t)
233 sigset_t oldset;
234 block_atimers (&oldset);
236 if (t)
238 struct atimer *p, *prev;
240 /* See if T is active. */
241 for (p = atimers, prev = NULL; p && p != t; prev = p, p = p->next)
244 if (p == t)
246 if (prev)
247 prev->next = t->next;
248 else
249 atimers = t->next;
250 t->next = NULL;
252 else
253 /* T is not active. Let's handle this like T == 0. */
254 t = NULL;
257 stopped_atimers = append_atimer_lists (atimers, stopped_atimers);
258 atimers = t;
259 unblock_atimers (&oldset);
263 /* Run all timers again, if some have been stopped with a call to
264 stop_other_atimers. */
266 void
267 run_all_atimers (void)
269 if (stopped_atimers)
271 struct atimer *t = atimers;
272 struct atimer *next;
273 sigset_t oldset;
275 block_atimers (&oldset);
276 atimers = stopped_atimers;
277 stopped_atimers = NULL;
279 while (t)
281 next = t->next;
282 schedule_atimer (t);
283 t = next;
286 unblock_atimers (&oldset);
291 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
293 static void
294 set_alarm (void)
296 if (atimers)
298 #ifdef HAVE_SETITIMER
299 struct itimerval it;
300 #endif
301 struct timespec now, interval;
303 #ifdef HAVE_ITIMERSPEC
304 if (0 <= timerfd || alarm_timer_ok)
306 struct itimerspec ispec;
307 ispec.it_value = atimers->expiration;
308 ispec.it_interval.tv_sec = ispec.it_interval.tv_nsec = 0;
309 # ifdef HAVE_TIMERFD
310 if (timerfd_settime (timerfd, TFD_TIMER_ABSTIME, &ispec, 0) == 0)
312 add_timer_wait_descriptor (timerfd);
313 return;
315 # endif
316 if (alarm_timer_ok
317 && timer_settime (alarm_timer, TIMER_ABSTIME, &ispec, 0) == 0)
318 return;
320 #endif
322 /* Determine interval till the next timer is ripe.
323 Don't set the interval to 0; this disables the timer. */
324 now = current_timespec ();
325 interval = (timespec_cmp (atimers->expiration, now) <= 0
326 ? make_timespec (0, 1000 * 1000)
327 : timespec_sub (atimers->expiration, now));
329 #ifdef HAVE_SETITIMER
331 memset (&it, 0, sizeof it);
332 it.it_value = make_timeval (interval);
333 setitimer (ITIMER_REAL, &it, 0);
334 #else /* not HAVE_SETITIMER */
335 alarm (max (interval.tv_sec, 1));
336 #endif /* not HAVE_SETITIMER */
341 /* Insert timer T into the list of active atimers `atimers', keeping
342 the list sorted by expiration time. T must not be in this list
343 already. */
345 static void
346 schedule_atimer (struct atimer *t)
348 struct atimer *a = atimers, *prev = NULL;
350 /* Look for the first atimer that is ripe after T. */
351 while (a && timespec_cmp (a->expiration, t->expiration) < 0)
352 prev = a, a = a->next;
354 /* Insert T in front of the atimer found, if any. */
355 if (prev)
356 prev->next = t;
357 else
358 atimers = t;
360 t->next = a;
363 static void
364 run_timers (void)
366 struct timespec now = current_timespec ();
368 while (atimers && timespec_cmp (atimers->expiration, now) <= 0)
370 struct atimer *t = atimers;
371 atimers = atimers->next;
372 t->fn (t);
374 if (t->type == ATIMER_CONTINUOUS)
376 t->expiration = timespec_add (now, t->interval);
377 schedule_atimer (t);
379 else
381 t->next = free_atimers;
382 free_atimers = t;
386 set_alarm ();
390 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
391 SIGALRM. */
393 static void
394 handle_alarm_signal (int sig)
396 pending_signals = 1;
399 #ifdef HAVE_TIMERFD
401 /* Called from wait_reading_process_output when FD, which
402 should be equal to TIMERFD, is available for reading. */
404 void
405 timerfd_callback (int fd, void *arg)
407 ptrdiff_t nbytes;
408 uint64_t expirations;
410 eassert (fd == timerfd);
411 nbytes = emacs_read (fd, &expirations, sizeof (expirations));
413 if (nbytes == sizeof (expirations))
415 /* Timer should expire just once. */
416 eassert (expirations == 1);
417 do_pending_atimers ();
419 else if (nbytes < 0)
420 /* For some not yet known reason, we may get weird event and no
421 data on timer descriptor. This can break Gnus at least, see:
422 http://lists.gnu.org/archive/html/emacs-devel/2014-07/msg00503.html. */
423 eassert (errno == EAGAIN);
424 else
425 /* I don't know what else can happen with this descriptor. */
426 emacs_abort ();
429 #endif /* HAVE_TIMERFD */
431 /* Do pending timers. */
433 void
434 do_pending_atimers (void)
436 if (atimers)
438 sigset_t oldset;
439 block_atimers (&oldset);
440 run_timers ();
441 unblock_atimers (&oldset);
446 /* Turn alarms on/off. This seems to be temporarily necessary on
447 some systems like HPUX (see process.c). */
449 void
450 turn_on_atimers (bool on)
452 if (on)
453 set_alarm ();
454 else
456 #ifdef HAVE_ITIMERSPEC
457 struct itimerspec ispec;
458 memset (&ispec, 0, sizeof ispec);
459 if (alarm_timer_ok)
460 timer_settime (alarm_timer, TIMER_ABSTIME, &ispec, 0);
461 # ifdef HAVE_TIMERFD
462 timerfd_settime (timerfd, TFD_TIMER_ABSTIME, &ispec, 0);
463 # endif
464 #endif
465 alarm (0);
469 /* This is intended to use from automated tests. */
471 #ifdef ENABLE_CHECKING
473 #define MAXTIMERS 10
475 struct atimer_result
477 /* Time when we expect this timer to trigger. */
478 struct timespec expected;
480 /* Timer status: -1 if not triggered, 0 if triggered
481 too early or too late, 1 if triggered timely. */
482 int intime;
485 static void
486 debug_timer_callback (struct atimer *t)
488 struct timespec now = current_timespec ();
489 struct atimer_result *r = (struct atimer_result *) t->client_data;
490 int result = timespec_cmp (now, r->expected);
492 if (result < 0)
493 /* Too early. */
494 r->intime = 0;
495 else if (result >= 0)
497 #ifdef HAVE_SETITIMER
498 struct timespec delta = timespec_sub (now, r->expected);
499 /* Too late if later than expected + 0.02s. FIXME:
500 this should depend from system clock resolution. */
501 if (timespec_cmp (delta, make_timespec (0, 20000000)) > 0)
502 r->intime = 0;
503 else
504 #endif /* HAVE_SETITIMER */
505 r->intime = 1;
509 DEFUN ("debug-timer-check", Fdebug_timer_check, Sdebug_timer_check, 0, 0, 0,
510 doc: /* Run internal self-tests to check timers subsystem.
511 Return t if all self-tests are passed, nil otherwise. */)
512 (void)
514 int i, ok;
515 struct atimer *timer;
516 struct atimer_result *results[MAXTIMERS];
517 struct timespec t = make_timespec (0, 0);
519 /* Arm MAXTIMERS relative timers to trigger with 0.1s intervals. */
520 for (i = 0; i < MAXTIMERS; i++)
522 results[i] = xmalloc (sizeof (struct atimer_result));
523 t = timespec_add (t, make_timespec (0, 100000000));
524 results[i]->expected = timespec_add (current_timespec (), t);
525 results[i]->intime = -1;
526 timer = start_atimer (ATIMER_RELATIVE, t,
527 debug_timer_callback, results[i]);
530 #ifdef HAVE_TIMERFD
531 /* Wait for 1s but process timers. */
532 wait_reading_process_output (1, 0, 0, false, Qnil, NULL, 0);
533 #else
534 /* If timerfd is not supported, wait_reading_process_output won't
535 pay attention to timers that expired, and the callbacks won't be
536 called. So we need to run the expired timers' callbacks by
537 hand. */
538 /* Wait 1.2 sec for the timers to expire. */
539 struct timespec tend =
540 timespec_add (current_timespec (), make_timespec (1, 200000000));
542 while (timespec_cmp (current_timespec (), tend) < 0)
544 /* Wait for 5 msec between iterations. */
545 wait_reading_process_output (0, 5000000, 0, false, Qnil, NULL, 0);
546 if (pending_signals)
547 do_pending_atimers ();
549 #endif
550 /* Shut up the compiler by "using" this variable. */
551 (void) timer;
553 for (i = 0, ok = 0; i < MAXTIMERS; i++)
554 ok += results[i]->intime, xfree (results[i]);
556 return ok == MAXTIMERS ? Qt : Qnil;
559 #endif /* ENABLE_CHECKING */
561 void
562 init_atimer (void)
564 #ifdef HAVE_ITIMERSPEC
565 # ifdef HAVE_TIMERFD
566 /* Until this feature is considered stable, you can ask to not use it. */
567 timerfd = (egetenv ("EMACS_IGNORE_TIMERFD") ? -1 :
568 timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC));
569 # endif
570 if (timerfd < 0)
572 struct sigevent sigev;
573 sigev.sigev_notify = SIGEV_SIGNAL;
574 sigev.sigev_signo = SIGALRM;
575 sigev.sigev_value.sival_ptr = &alarm_timer;
576 alarm_timer_ok
577 = timer_create (CLOCK_REALTIME, &sigev, &alarm_timer) == 0;
579 #endif
580 free_atimers = stopped_atimers = atimers = NULL;
582 /* pending_signals is initialized in init_keyboard. */
583 struct sigaction action;
584 emacs_sigaction_init (&action, handle_alarm_signal);
585 sigaction (SIGALRM, &action, 0);
587 #ifdef ENABLE_CHECKING
588 defsubr (&Sdebug_timer_check);
589 #endif