1 /* Asynchronous timers.
2 Copyright (C) 2000-2012 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 /* Non-zero means alarm signal handler has found ripe timers but
44 interrupt_input_blocked was non-zero. In this case, timer
45 functions are not called until the next UNBLOCK_INPUT because timer
46 functions are expected to call X, and X cannot be assumed to be
51 /* Block/unblock SIGALRM. */
54 sigmask_atimers (int how
)
57 sigemptyset (&blocked
);
58 sigaddset (&blocked
, SIGALRM
);
59 pthread_sigmask (how
, &blocked
, 0);
64 sigmask_atimers (SIG_BLOCK
);
67 unblock_atimers (void)
69 sigmask_atimers (SIG_UNBLOCK
);
72 /* Function prototypes. */
74 static void set_alarm (void);
75 static void schedule_atimer (struct atimer
*);
76 static struct atimer
*append_atimer_lists (struct atimer
*,
79 /* Start a new atimer of type TYPE. TIME specifies when the timer is
80 ripe. FN is the function to call when the timer fires.
81 CLIENT_DATA is stored in the client_data member of the atimer
82 structure returned and so made available to FN when it is called.
84 If TYPE is ATIMER_ABSOLUTE, TIME is the absolute time at which the
87 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the
90 In both cases, the timer is automatically freed after it has fired.
92 If TYPE is ATIMER_CONTINUOUS, the timer fires every TIME s/us.
94 Value is a pointer to the atimer started. It can be used in calls
95 to cancel_atimer; don't free it yourself. */
98 start_atimer (enum atimer_type type
, EMACS_TIME timestamp
, atimer_callback fn
,
103 /* Round TIME up to the next full second if we don't have
105 #ifndef HAVE_SETITIMER
106 if (EMACS_NSECS (timestamp
) != 0
107 && EMACS_SECS (timestamp
) < TYPE_MAXIMUM (time_t))
108 timestamp
= make_emacs_time (EMACS_SECS (timestamp
) + 1, 0);
109 #endif /* not HAVE_SETITIMER */
111 /* Get an atimer structure from the free-list, or allocate
116 free_atimers
= t
->next
;
119 t
= xmalloc (sizeof *t
);
121 /* Fill the atimer structure. */
122 memset (t
, 0, sizeof *t
);
125 t
->client_data
= client_data
;
129 /* Compute the timer's expiration time. */
132 case ATIMER_ABSOLUTE
:
133 t
->expiration
= timestamp
;
136 case ATIMER_RELATIVE
:
137 t
->expiration
= add_emacs_time (current_emacs_time (), timestamp
);
140 case ATIMER_CONTINUOUS
:
141 t
->expiration
= add_emacs_time (current_emacs_time (), timestamp
);
142 t
->interval
= timestamp
;
146 /* Insert the timer in the list of active atimers. */
150 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
157 /* Cancel and free atimer TIMER. */
160 cancel_atimer (struct atimer
*timer
)
166 for (i
= 0; i
< 2; ++i
)
168 struct atimer
*t
, *prev
;
169 struct atimer
**list
= i
? &stopped_atimers
: &atimers
;
171 /* See if TIMER is active or stopped. */
172 for (t
= *list
, prev
= NULL
; t
&& t
!= timer
; prev
= t
, t
= t
->next
)
175 /* If it is, take it off its list, and put in on the free-list.
176 We don't bother to arrange for setting a different alarm time,
177 since a too early one doesn't hurt. */
181 prev
->next
= t
->next
;
185 t
->next
= free_atimers
;
195 /* Append two lists of atimers LIST_1 and LIST_2 and return the
198 static struct atimer
*
199 append_atimer_lists (struct atimer
*list_1
, struct atimer
*list_2
)
203 else if (list_2
== NULL
)
209 for (p
= list_1
; p
->next
; p
= p
->next
)
217 /* Stop all timers except timer T. T null means stop all timers. */
220 stop_other_atimers (struct atimer
*t
)
226 struct atimer
*p
, *prev
;
228 /* See if T is active. */
229 for (p
= atimers
, prev
= NULL
; p
&& p
!= t
; prev
= p
, p
= p
->next
)
235 prev
->next
= t
->next
;
241 /* T is not active. Let's handle this like T == 0. */
245 stopped_atimers
= append_atimer_lists (atimers
, stopped_atimers
);
251 /* Run all timers again, if some have been stopped with a call to
252 stop_other_atimers. */
255 run_all_atimers (void)
259 struct atimer
*t
= atimers
;
263 atimers
= stopped_atimers
;
264 stopped_atimers
= NULL
;
278 /* A version of run_all_atimers suitable for a record_unwind_protect. */
281 unwind_stop_other_atimers (Lisp_Object dummy
)
288 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
295 #ifdef HAVE_SETITIMER
299 /* Determine s/us till the next timer is ripe. */
300 EMACS_TIME now
= current_emacs_time ();
302 /* Don't set the interval to 0; this disables the timer. */
303 EMACS_TIME 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_GT (t
->expiration
, a
->expiration
))
330 prev
= a
, a
= a
->next
;
332 /* Insert T in front of the atimer found, if any. */
347 && (pending_atimers
= interrupt_input_blocked
) == 0
348 && (now
= current_emacs_time (),
349 EMACS_TIME_LE (atimers
->expiration
, now
)))
354 atimers
= atimers
->next
;
357 if (t
->type
== ATIMER_CONTINUOUS
)
359 t
->expiration
= add_emacs_time (now
, t
->interval
);
364 t
->next
= free_atimers
;
377 pending_signals
= interrupt_input_pending
;
381 if (! pending_atimers
)
387 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
391 handle_alarm_signal (int sig
)
402 deliver_alarm_signal (int sig
)
404 handle_on_main_thread (sig
, handle_alarm_signal
);
408 /* Call alarm signal handler for pending timers. */
411 do_pending_atimers (void)
422 /* Turn alarms on/off. This seems to be temporarily necessary on
423 some systems like HPUX (see process.c). */
426 turn_on_atimers (bool on
)
430 struct sigaction action
;
431 emacs_sigaction_init (&action
, deliver_alarm_signal
);
432 sigaction (SIGALRM
, &action
, 0);
443 struct sigaction action
;
444 free_atimers
= stopped_atimers
= atimers
= NULL
;
446 /* pending_signals is initialized in init_keyboard.*/
447 emacs_sigaction_init (&action
, deliver_alarm_signal
);
448 sigaction (SIGALRM
, &action
, 0);