1 /* Asynchronous timers.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
25 #include "syssignal.h"
27 #include "blockinput.h"
31 #ifdef HAVE_SYS_TIME_H
35 /* Free-list of atimer structures. */
37 static struct atimer
*free_atimers
;
39 /* List of currently not running timers due to a call to
42 static struct atimer
*stopped_atimers
;
44 /* List of active atimers, sorted by expiration time. The timer that
45 will become ripe next is always at the front of this list. */
47 static struct atimer
*atimers
;
49 /* Non-zero means alarm_signal_handler has found ripe timers but
50 interrupt_input_blocked was non-zero. In this case, timer
51 functions are not called until the next UNBLOCK_INPUT because timer
52 functions are expected to call X, and X cannot be assumed to be
57 /* Block/unblock SIGALRM. */
59 #define BLOCK_ATIMERS sigblock (sigmask (SIGALRM))
60 #define UNBLOCK_ATIMERS sigunblock (sigmask (SIGALRM))
62 /* Function prototypes. */
64 static void set_alarm (void);
65 static void schedule_atimer (struct atimer
*);
66 static struct atimer
*append_atimer_lists (struct atimer
*,
68 SIGTYPE
alarm_signal_handler (int signo
);
71 /* Start a new atimer of type TYPE. TIME specifies when the timer is
72 ripe. FN is the function to call when the timer fires.
73 CLIENT_DATA is stored in the client_data member of the atimer
74 structure returned and so made available to FN when it is called.
76 If TYPE is ATIMER_ABSOLUTE, TIME is the absolute time at which the
79 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the
82 In both cases, the timer is automatically freed after it has fired.
84 If TYPE is ATIMER_CONTINUOUS, the timer fires every TIME s/us.
86 Value is a pointer to the atimer started. It can be used in calls
87 to cancel_atimer; don't free it yourself. */
90 start_atimer (enum atimer_type type
, EMACS_TIME time
, atimer_callback fn
,
95 /* Round TIME up to the next full second if we don't have
97 #ifndef HAVE_SETITIMER
98 if (EMACS_USECS (time
) != 0)
100 EMACS_SET_USECS (time
, 0);
101 EMACS_SET_SECS (time
, EMACS_SECS (time
) + 1);
103 #endif /* not HAVE_SETITIMER */
105 /* Get an atimer structure from the free-list, or allocate
110 free_atimers
= t
->next
;
113 t
= (struct atimer
*) xmalloc (sizeof *t
);
115 /* Fill the atimer structure. */
116 memset (t
, 0, sizeof *t
);
119 t
->client_data
= client_data
;
123 /* Compute the timer's expiration time. */
126 case ATIMER_ABSOLUTE
:
127 t
->expiration
= time
;
130 case ATIMER_RELATIVE
:
131 EMACS_GET_TIME (t
->expiration
);
132 EMACS_ADD_TIME (t
->expiration
, t
->expiration
, time
);
135 case ATIMER_CONTINUOUS
:
136 EMACS_GET_TIME (t
->expiration
);
137 EMACS_ADD_TIME (t
->expiration
, t
->expiration
, time
);
142 /* Insert the timer in the list of active atimers. */
146 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
153 /* Cancel and free atimer TIMER. */
156 cancel_atimer (struct atimer
*timer
)
162 for (i
= 0; i
< 2; ++i
)
164 struct atimer
*t
, *prev
;
165 struct atimer
**list
= i
? &stopped_atimers
: &atimers
;
167 /* See if TIMER is active or stopped. */
168 for (t
= *list
, prev
= NULL
; t
&& t
!= timer
; prev
= t
, t
= t
->next
)
171 /* If it is, take it off its list, and put in on the free-list.
172 We don't bother to arrange for setting a different alarm time,
173 since a too early one doesn't hurt. */
177 prev
->next
= t
->next
;
181 t
->next
= free_atimers
;
191 /* Append two lists of atimers LIST1 and LIST2 and return the
194 static struct atimer
*
195 append_atimer_lists (struct atimer
*list1
, struct atimer
*list2
)
199 else if (list2
== NULL
)
205 for (p
= list1
; p
->next
; p
= p
->next
)
213 /* Stop all timers except timer T. T null means stop all timers. */
216 stop_other_atimers (struct atimer
*t
)
222 struct atimer
*p
, *prev
;
224 /* See if T is active. */
225 for (p
= atimers
, prev
= NULL
; p
&& p
!= t
; prev
= p
, p
= p
->next
)
231 prev
->next
= t
->next
;
237 /* T is not active. Let's handle this like T == 0. */
241 stopped_atimers
= append_atimer_lists (atimers
, stopped_atimers
);
247 /* Run all timers again, if some have been stopped with a call to
248 stop_other_atimers. */
251 run_all_atimers (void)
255 struct atimer
*t
= atimers
;
259 atimers
= stopped_atimers
;
260 stopped_atimers
= NULL
;
274 /* A version of run_all_timers suitable for a record_unwind_protect. */
277 unwind_stop_other_atimers (Lisp_Object dummy
)
284 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
291 EMACS_TIME now
, time
;
292 #ifdef HAVE_SETITIMER
296 /* Determine s/us till the next timer is ripe. */
297 EMACS_GET_TIME (now
);
298 EMACS_SUB_TIME (time
, atimers
->expiration
, now
);
300 #ifdef HAVE_SETITIMER
301 /* Don't set the interval to 0; this disables the timer. */
302 if (EMACS_TIME_LE (atimers
->expiration
, now
))
304 EMACS_SET_SECS (time
, 0);
305 EMACS_SET_USECS (time
, 1000);
308 memset (&it
, 0, sizeof it
);
310 setitimer (ITIMER_REAL
, &it
, 0);
311 #else /* not HAVE_SETITIMER */
312 alarm (max (EMACS_SECS (time
), 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
&& EMACS_TIME_GT (t
->expiration
, a
->expiration
))
329 prev
= a
, a
= a
->next
;
331 /* Insert T in front of the atimer found, if any. */
345 EMACS_GET_TIME (now
);
348 && (pending_atimers
= interrupt_input_blocked
) == 0
349 && EMACS_TIME_LE (atimers
->expiration
, now
))
354 atimers
= atimers
->next
;
357 if (t
->type
== ATIMER_CONTINUOUS
)
359 EMACS_ADD_TIME (t
->expiration
, now
, t
->interval
);
364 t
->next
= free_atimers
;
368 EMACS_GET_TIME (now
);
379 pending_signals
= interrupt_input_pending
;
383 if (! pending_atimers
)
389 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
393 alarm_signal_handler (int signo
)
396 SIGNAL_THREAD_CHECK (signo
);
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 (int on
)
430 signal (SIGALRM
, alarm_signal_handler
);
441 free_atimers
= stopped_atimers
= atimers
= NULL
;
443 /* pending_signals is initialized in init_keyboard.*/
444 signal (SIGALRM
, alarm_signal_handler
);