1 /* Asynchronous timers.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010 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>
34 #ifdef HAVE_SYS_TIME_H
38 /* Free-list of atimer structures. */
40 static struct atimer
*free_atimers
;
42 /* List of currently not running timers due to a call to
45 static struct atimer
*stopped_atimers
;
47 /* List of active atimers, sorted by expiration time. The timer that
48 will become ripe next is always at the front of this list. */
50 static struct atimer
*atimers
;
52 /* Non-zero means alarm_signal_handler has found ripe timers but
53 interrupt_input_blocked was non-zero. In this case, timer
54 functions are not called until the next UNBLOCK_INPUT because timer
55 functions are expected to call X, and X cannot be assumed to be
60 /* Block/unblock SIGALRM. */
62 #define BLOCK_ATIMERS sigblock (sigmask (SIGALRM))
63 #define UNBLOCK_ATIMERS sigunblock (sigmask (SIGALRM))
65 /* Function prototypes. */
67 static void set_alarm (void);
68 static void schedule_atimer (struct atimer
*);
69 static struct atimer
*append_atimer_lists (struct atimer
*,
71 SIGTYPE
alarm_signal_handler (int signo
);
74 /* Start a new atimer of type TYPE. TIME specifies when the timer is
75 ripe. FN is the function to call when the timer fires.
76 CLIENT_DATA is stored in the client_data member of the atimer
77 structure returned and so made available to FN when it is called.
79 If TYPE is ATIMER_ABSOLUTE, TIME is the absolute time at which the
82 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the
85 In both cases, the timer is automatically freed after it has fired.
87 If TYPE is ATIMER_CONTINUOUS, the timer fires every TIME s/us.
89 Value is a pointer to the atimer started. It can be used in calls
90 to cancel_atimer; don't free it yourself. */
93 start_atimer (enum atimer_type type
, struct timeval time
, atimer_callback fn
, void *client_data
)
97 /* Round TIME up to the next full second if we don't have
99 #ifndef HAVE_SETITIMER
100 if (EMACS_USECS (time
) != 0)
102 EMACS_SET_USECS (time
, 0);
103 EMACS_SET_SECS (time
, EMACS_SECS (time
) + 1);
105 #endif /* not HAVE_SETITIMER */
107 /* Get an atimer structure from the free-list, or allocate
112 free_atimers
= t
->next
;
115 t
= (struct atimer
*) xmalloc (sizeof *t
);
117 /* Fill the atimer structure. */
118 bzero (t
, sizeof *t
);
121 t
->client_data
= client_data
;
125 /* Compute the timer's expiration time. */
128 case ATIMER_ABSOLUTE
:
129 t
->expiration
= time
;
132 case ATIMER_RELATIVE
:
133 EMACS_GET_TIME (t
->expiration
);
134 EMACS_ADD_TIME (t
->expiration
, t
->expiration
, time
);
137 case ATIMER_CONTINUOUS
:
138 EMACS_GET_TIME (t
->expiration
);
139 EMACS_ADD_TIME (t
->expiration
, t
->expiration
, time
);
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 the its list, and put in on the
174 free-list. We don't bother to arrange for setting a
175 different alarm time, since a too early one doesn't hurt. */
179 prev
->next
= t
->next
;
183 t
->next
= free_atimers
;
193 /* Append two lists of atimers LIST1 and LIST2 and return the
196 static struct atimer
*
197 append_atimer_lists (struct atimer
*list1
, struct atimer
*list2
)
201 else if (list2
== NULL
)
207 for (p
= list1
; 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 /* A version of run_all_timers suitable for a record_unwind_protect. */
279 unwind_stop_other_atimers (Lisp_Object dummy
)
286 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
293 EMACS_TIME now
, time
;
294 #ifdef HAVE_SETITIMER
298 /* Determine s/us till the next timer is ripe. */
299 EMACS_GET_TIME (now
);
300 EMACS_SUB_TIME (time
, atimers
->expiration
, now
);
302 #ifdef HAVE_SETITIMER
303 /* Don't set the interval to 0; this disables the timer. */
304 if (EMACS_TIME_LE (atimers
->expiration
, now
))
306 EMACS_SET_SECS (time
, 0);
307 EMACS_SET_USECS (time
, 1000);
310 bzero (&it
, sizeof it
);
312 setitimer (ITIMER_REAL
, &it
, 0);
313 #else /* not HAVE_SETITIMER */
314 alarm (max (EMACS_SECS (time
), 1));
315 #endif /* not HAVE_SETITIMER */
320 /* Insert timer T into the list of active atimers `atimers', keeping
321 the list sorted by expiration time. T must not be in this list
325 schedule_atimer (struct atimer
*t
)
327 struct atimer
*a
= atimers
, *prev
= NULL
;
329 /* Look for the first atimer that is ripe after T. */
330 while (a
&& EMACS_TIME_GT (t
->expiration
, a
->expiration
))
331 prev
= a
, a
= a
->next
;
333 /* Insert T in front of the atimer found, if any. */
347 EMACS_GET_TIME (now
);
350 && (pending_atimers
= interrupt_input_blocked
) == 0
351 && EMACS_TIME_LE (atimers
->expiration
, now
))
356 atimers
= atimers
->next
;
359 if (t
->type
== ATIMER_CONTINUOUS
)
361 EMACS_ADD_TIME (t
->expiration
, now
, t
->interval
);
366 t
->next
= free_atimers
;
370 EMACS_GET_TIME (now
);
381 pending_signals
= interrupt_input_pending
;
385 if (! pending_atimers
)
391 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
395 alarm_signal_handler (int signo
)
398 SIGNAL_THREAD_CHECK (signo
);
410 /* Call alarm_signal_handler for pending timers. */
413 do_pending_atimers (void)
424 /* Turn alarms on/off. This seems to be temporarily necessary on
425 some systems like HPUX (see process.c). */
428 turn_on_atimers (int on
)
432 signal (SIGALRM
, alarm_signal_handler
);
443 free_atimers
= stopped_atimers
= atimers
= NULL
;
445 /* pending_signals is initialized in init_keyboard.*/
446 signal (SIGALRM
, alarm_signal_handler
);
449 /* arch-tag: e6308261-eec6-404b-89fb-6e5909518d70
450 (do not change this comment) */