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/>. */
24 #include "syssignal.h"
26 #include "blockinput.h"
30 /* Free-list of atimer structures. */
32 static struct atimer
*free_atimers
;
34 /* List of currently not running timers due to a call to
37 static struct atimer
*stopped_atimers
;
39 /* List of active atimers, sorted by expiration time. The timer that
40 will become ripe next is always at the front of this list. */
42 static struct atimer
*atimers
;
44 /* Non-zero means alarm_signal_handler has found ripe timers but
45 interrupt_input_blocked was non-zero. In this case, timer
46 functions are not called until the next UNBLOCK_INPUT because timer
47 functions are expected to call X, and X cannot be assumed to be
52 /* Block/unblock SIGALRM. */
54 #define BLOCK_ATIMERS sigblock (sigmask (SIGALRM))
55 #define UNBLOCK_ATIMERS sigunblock (sigmask (SIGALRM))
57 /* Function prototypes. */
59 static void set_alarm (void);
60 static void schedule_atimer (struct atimer
*);
61 static struct atimer
*append_atimer_lists (struct atimer
*,
63 static void alarm_signal_handler (int signo
);
66 /* Start a new atimer of type TYPE. TIME specifies when the timer is
67 ripe. FN is the function to call when the timer fires.
68 CLIENT_DATA is stored in the client_data member of the atimer
69 structure returned and so made available to FN when it is called.
71 If TYPE is ATIMER_ABSOLUTE, TIME is the absolute time at which the
74 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the
77 In both cases, the timer is automatically freed after it has fired.
79 If TYPE is ATIMER_CONTINUOUS, the timer fires every TIME s/us.
81 Value is a pointer to the atimer started. It can be used in calls
82 to cancel_atimer; don't free it yourself. */
85 start_atimer (enum atimer_type type
, EMACS_TIME timestamp
, atimer_callback fn
,
90 /* Round TIME up to the next full second if we don't have
92 #ifndef HAVE_SETITIMER
93 if (EMACS_NSECS (timestamp
) != 0
94 && EMACS_SECS (timestamp
) < TYPE_MAXIMUM (time_t))
95 timestamp
= make_emacs_time (EMACS_SECS (timestamp
) + 1, 0);
96 #endif /* not HAVE_SETITIMER */
98 /* Get an atimer structure from the free-list, or allocate
103 free_atimers
= t
->next
;
106 t
= xmalloc (sizeof *t
);
108 /* Fill the atimer structure. */
109 memset (t
, 0, sizeof *t
);
112 t
->client_data
= client_data
;
116 /* Compute the timer's expiration time. */
119 case ATIMER_ABSOLUTE
:
120 t
->expiration
= timestamp
;
123 case ATIMER_RELATIVE
:
124 t
->expiration
= add_emacs_time (current_emacs_time (), timestamp
);
127 case ATIMER_CONTINUOUS
:
128 t
->expiration
= add_emacs_time (current_emacs_time (), timestamp
);
129 t
->interval
= timestamp
;
133 /* Insert the timer in the list of active atimers. */
137 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
144 /* Cancel and free atimer TIMER. */
147 cancel_atimer (struct atimer
*timer
)
153 for (i
= 0; i
< 2; ++i
)
155 struct atimer
*t
, *prev
;
156 struct atimer
**list
= i
? &stopped_atimers
: &atimers
;
158 /* See if TIMER is active or stopped. */
159 for (t
= *list
, prev
= NULL
; t
&& t
!= timer
; prev
= t
, t
= t
->next
)
162 /* If it is, take it off its list, and put in on the free-list.
163 We don't bother to arrange for setting a different alarm time,
164 since a too early one doesn't hurt. */
168 prev
->next
= t
->next
;
172 t
->next
= free_atimers
;
182 /* Append two lists of atimers LIST_1 and LIST_2 and return the
185 static struct atimer
*
186 append_atimer_lists (struct atimer
*list_1
, struct atimer
*list_2
)
190 else if (list_2
== NULL
)
196 for (p
= list_1
; p
->next
; p
= p
->next
)
204 /* Stop all timers except timer T. T null means stop all timers. */
207 stop_other_atimers (struct atimer
*t
)
213 struct atimer
*p
, *prev
;
215 /* See if T is active. */
216 for (p
= atimers
, prev
= NULL
; p
&& p
!= t
; prev
= p
, p
= p
->next
)
222 prev
->next
= t
->next
;
228 /* T is not active. Let's handle this like T == 0. */
232 stopped_atimers
= append_atimer_lists (atimers
, stopped_atimers
);
238 /* Run all timers again, if some have been stopped with a call to
239 stop_other_atimers. */
242 run_all_atimers (void)
246 struct atimer
*t
= atimers
;
250 atimers
= stopped_atimers
;
251 stopped_atimers
= NULL
;
265 /* A version of run_all_atimers suitable for a record_unwind_protect. */
268 unwind_stop_other_atimers (Lisp_Object dummy
)
275 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
282 #ifdef HAVE_SETITIMER
286 /* Determine s/us till the next timer is ripe. */
287 EMACS_TIME now
= current_emacs_time ();
289 /* Don't set the interval to 0; this disables the timer. */
290 EMACS_TIME interval
= (EMACS_TIME_LE (atimers
->expiration
, now
)
291 ? make_emacs_time (0, 1000 * 1000)
292 : sub_emacs_time (atimers
->expiration
, now
));
294 #ifdef HAVE_SETITIMER
296 memset (&it
, 0, sizeof it
);
297 it
.it_value
= make_timeval (interval
);
298 setitimer (ITIMER_REAL
, &it
, 0);
299 #else /* not HAVE_SETITIMER */
300 alarm (max (EMACS_SECS (interval
), 1));
301 #endif /* not HAVE_SETITIMER */
306 /* Insert timer T into the list of active atimers `atimers', keeping
307 the list sorted by expiration time. T must not be in this list
311 schedule_atimer (struct atimer
*t
)
313 struct atimer
*a
= atimers
, *prev
= NULL
;
315 /* Look for the first atimer that is ripe after T. */
316 while (a
&& EMACS_TIME_GT (t
->expiration
, a
->expiration
))
317 prev
= a
, a
= a
->next
;
319 /* Insert T in front of the atimer found, if any. */
334 && (pending_atimers
= interrupt_input_blocked
) == 0
335 && (now
= current_emacs_time (),
336 EMACS_TIME_LE (atimers
->expiration
, now
)))
341 atimers
= atimers
->next
;
344 if (t
->type
== ATIMER_CONTINUOUS
)
346 t
->expiration
= add_emacs_time (now
, t
->interval
);
351 t
->next
= free_atimers
;
364 pending_signals
= interrupt_input_pending
;
368 if (! pending_atimers
)
374 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
378 alarm_signal_handler (int signo
)
381 SIGNAL_THREAD_CHECK (signo
);
393 /* Call alarm_signal_handler for pending timers. */
396 do_pending_atimers (void)
407 /* Turn alarms on/off. This seems to be temporarily necessary on
408 some systems like HPUX (see process.c). */
411 turn_on_atimers (int on
)
415 signal (SIGALRM
, alarm_signal_handler
);
426 free_atimers
= stopped_atimers
= atimers
= NULL
;
428 /* pending_signals is initialized in init_keyboard.*/
429 signal (SIGALRM
, alarm_signal_handler
);