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/>. */
23 #include "syssignal.h"
25 #include "blockinput.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
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
;
50 /* Block/unblock SIGALRM. */
53 sigmask_atimers (int how
)
56 sigemptyset (&blocked
);
57 sigaddset (&blocked
, SIGALRM
);
58 pthread_sigmask (how
, &blocked
, 0);
63 sigmask_atimers (SIG_BLOCK
);
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
*,
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
86 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the
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. */
97 start_atimer (enum atimer_type type
, struct timespec timestamp
,
98 atimer_callback fn
, void *client_data
)
102 /* Round TIME up to the next full second if we don't have
104 #ifndef HAVE_SETITIMER
105 if (timestamp
.tv_nsec
!= 0 && timestamp
.tv_sec
< TYPE_MAXIMUM (time_t))
106 timestamp
= make_timespec (timestamp
.tv_sec
+ 1, 0);
107 #endif /* not HAVE_SETITIMER */
109 /* Get an atimer structure from the free-list, or allocate
114 free_atimers
= t
->next
;
117 t
= xmalloc (sizeof *t
);
119 /* Fill the atimer structure. */
120 memset (t
, 0, sizeof *t
);
123 t
->client_data
= client_data
;
127 /* Compute the timer's expiration time. */
130 case ATIMER_ABSOLUTE
:
131 t
->expiration
= timestamp
;
134 case ATIMER_RELATIVE
:
135 t
->expiration
= timespec_add (current_timespec (), timestamp
);
138 case ATIMER_CONTINUOUS
:
139 t
->expiration
= timespec_add (current_timespec (), timestamp
);
140 t
->interval
= timestamp
;
144 /* Insert the timer in the list of active atimers. */
148 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
155 /* Cancel and free atimer TIMER. */
158 cancel_atimer (struct atimer
*timer
)
164 for (i
= 0; i
< 2; ++i
)
166 struct atimer
*t
, *prev
;
167 struct atimer
**list
= i
? &stopped_atimers
: &atimers
;
169 /* See if TIMER is active or stopped. */
170 for (t
= *list
, prev
= NULL
; t
&& t
!= timer
; prev
= t
, t
= t
->next
)
173 /* If it is, take it off its list, and put in on the free-list.
174 We don't bother to arrange for setting a different alarm time,
175 since a too early one doesn't hurt. */
179 prev
->next
= t
->next
;
183 t
->next
= free_atimers
;
193 /* Append two lists of atimers LIST_1 and LIST_2 and return the
196 static struct atimer
*
197 append_atimer_lists (struct atimer
*list_1
, struct atimer
*list_2
)
201 else if (list_2
== NULL
)
207 for (p
= list_1
; p
->next
; p
= p
->next
)
215 /* Stop all timers except timer T. T null means stop all timers. */
218 stop_other_atimers (struct atimer
*t
)
224 struct atimer
*p
, *prev
;
226 /* See if T is active. */
227 for (p
= atimers
, prev
= NULL
; p
&& p
!= t
; prev
= p
, p
= p
->next
)
233 prev
->next
= t
->next
;
239 /* T is not active. Let's handle this like T == 0. */
243 stopped_atimers
= append_atimer_lists (atimers
, stopped_atimers
);
249 /* Run all timers again, if some have been stopped with a call to
250 stop_other_atimers. */
253 run_all_atimers (void)
257 struct atimer
*t
= atimers
;
261 atimers
= stopped_atimers
;
262 stopped_atimers
= NULL
;
276 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
283 #ifdef HAVE_SETITIMER
286 struct timespec now
, interval
;
288 #ifdef HAVE_ITIMERSPEC
291 struct itimerspec ispec
;
292 ispec
.it_value
= atimers
->expiration
;
293 ispec
.it_interval
.tv_sec
= ispec
.it_interval
.tv_nsec
= 0;
294 if (timer_settime (alarm_timer
, 0, &ispec
, 0) == 0)
299 /* Determine interval till the next timer is ripe.
300 Don't set the interval to 0; this disables the timer. */
301 now
= current_timespec ();
302 interval
= (timespec_cmp (atimers
->expiration
, now
) <= 0
303 ? make_timespec (0, 1000 * 1000)
304 : timespec_sub (atimers
->expiration
, now
));
306 #ifdef HAVE_SETITIMER
308 memset (&it
, 0, sizeof it
);
309 it
.it_value
= make_timeval (interval
);
310 setitimer (ITIMER_REAL
, &it
, 0);
311 #else /* not HAVE_SETITIMER */
312 alarm (max (interval
.tv_sec
, 1));
313 #endif /* not HAVE_SETITIMER */
318 /* Insert timer T into the list of active atimers `atimers', keeping
319 the list sorted by expiration time. T must not be in this list
323 schedule_atimer (struct atimer
*t
)
325 struct atimer
*a
= atimers
, *prev
= NULL
;
327 /* Look for the first atimer that is ripe after T. */
328 while (a
&& timespec_cmp (a
->expiration
, t
->expiration
) < 0)
329 prev
= a
, a
= a
->next
;
331 /* Insert T in front of the atimer found, if any. */
343 struct timespec now
= current_timespec ();
345 while (atimers
&& timespec_cmp (atimers
->expiration
, now
) <= 0)
347 struct atimer
*t
= atimers
;
348 atimers
= atimers
->next
;
351 if (t
->type
== ATIMER_CONTINUOUS
)
353 t
->expiration
= timespec_add (now
, t
->interval
);
358 t
->next
= free_atimers
;
367 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
371 handle_alarm_signal (int sig
)
377 /* Do pending timers. */
380 do_pending_atimers (void)
391 /* Turn alarms on/off. This seems to be temporarily necessary on
392 some systems like HPUX (see process.c). */
395 turn_on_atimers (bool on
)
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;
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);