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
, EMACS_TIME timestamp
, atimer_callback fn
,
102 /* Round TIME up to the next full second if we don't have
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
115 free_atimers
= t
->next
;
118 t
= xmalloc (sizeof *t
);
120 /* Fill the atimer structure. */
121 memset (t
, 0, sizeof *t
);
124 t
->client_data
= client_data
;
128 /* Compute the timer's expiration time. */
131 case ATIMER_ABSOLUTE
:
132 t
->expiration
= timestamp
;
135 case ATIMER_RELATIVE
:
136 t
->expiration
= add_emacs_time (current_emacs_time (), timestamp
);
139 case ATIMER_CONTINUOUS
:
140 t
->expiration
= add_emacs_time (current_emacs_time (), timestamp
);
141 t
->interval
= timestamp
;
145 /* Insert the timer in the list of active atimers. */
149 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
156 /* Cancel and free atimer TIMER. */
159 cancel_atimer (struct atimer
*timer
)
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. */
180 prev
->next
= t
->next
;
184 t
->next
= free_atimers
;
194 /* Append two lists of atimers LIST_1 and LIST_2 and return the
197 static struct atimer
*
198 append_atimer_lists (struct atimer
*list_1
, struct atimer
*list_2
)
202 else if (list_2
== NULL
)
208 for (p
= list_1
; p
->next
; p
= p
->next
)
216 /* Stop all timers except timer T. T null means stop all timers. */
219 stop_other_atimers (struct atimer
*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
)
234 prev
->next
= t
->next
;
240 /* T is not active. Let's handle this like T == 0. */
244 stopped_atimers
= append_atimer_lists (atimers
, stopped_atimers
);
250 /* Run all timers again, if some have been stopped with a call to
251 stop_other_atimers. */
254 run_all_atimers (void)
258 struct atimer
*t
= atimers
;
262 atimers
= stopped_atimers
;
263 stopped_atimers
= NULL
;
277 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
284 #ifdef HAVE_SETITIMER
287 EMACS_TIME now
, interval
;
289 #ifdef HAVE_ITIMERSPEC
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)
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
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. */
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
;
352 if (t
->type
== ATIMER_CONTINUOUS
)
354 t
->expiration
= add_emacs_time (now
, t
->interval
);
359 t
->next
= free_atimers
;
368 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
372 handle_alarm_signal (int sig
)
378 /* Do pending timers. */
381 do_pending_atimers (void)
392 /* Turn alarms on/off. This seems to be temporarily necessary on
393 some systems like HPUX (see process.c). */
396 turn_on_atimers (bool on
)
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;
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);