1 /* Asynchronous timers.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009 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/>. */
24 #include <syssignal.h>
26 #include <blockinput.h>
33 #ifdef HAVE_SYS_TIME_H
37 /* Free-list of atimer structures. */
39 static struct atimer
*free_atimers
;
41 /* List of currently not running timers due to a call to
44 static struct atimer
*stopped_atimers
;
46 /* List of active atimers, sorted by expiration time. The timer that
47 will become ripe next is always at the front of this list. */
49 static struct atimer
*atimers
;
51 /* Non-zero means alarm_signal_handler has found ripe timers but
52 interrupt_input_blocked was non-zero. In this case, timer
53 functions are not called until the next UNBLOCK_INPUT because timer
54 functions are expected to call X, and X cannot be assumed to be
59 /* Block/unblock SIGALRM. */
61 #define BLOCK_ATIMERS sigblock (sigmask (SIGALRM))
62 #define UNBLOCK_ATIMERS sigunblock (sigmask (SIGALRM))
64 /* Function prototypes. */
66 static void set_alarm
P_ ((void));
67 static void schedule_atimer
P_ ((struct atimer
*));
68 static struct atimer
*append_atimer_lists
P_ ((struct atimer
*,
70 SIGTYPE
alarm_signal_handler ();
73 /* Start a new atimer of type TYPE. TIME specifies when the timer is
74 ripe. FN is the function to call when the timer fires.
75 CLIENT_DATA is stored in the client_data member of the atimer
76 structure returned and so made available to FN when it is called.
78 If TYPE is ATIMER_ABSOLUTE, TIME is the absolute time at which the
81 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the
84 In both cases, the timer is automatically freed after it has fired.
86 If TYPE is ATIMER_CONTINUOUS, the timer fires every TIME s/us.
88 Value is a pointer to the atimer started. It can be used in calls
89 to cancel_atimer; don't free it yourself. */
92 start_atimer (type
, time
, fn
, client_data
)
93 enum atimer_type type
;
100 /* Round TIME up to the next full second if we don't have
102 #ifndef HAVE_SETITIMER
103 if (EMACS_USECS (time
) != 0)
105 EMACS_SET_USECS (time
, 0);
106 EMACS_SET_SECS (time
, EMACS_SECS (time
) + 1);
108 #endif /* not HAVE_SETITIMER */
110 /* Get an atimer structure from the free-list, or allocate
115 free_atimers
= t
->next
;
118 t
= (struct atimer
*) xmalloc (sizeof *t
);
120 /* Fill the atimer structure. */
121 bzero (t
, sizeof *t
);
124 t
->client_data
= client_data
;
128 /* Compute the timer's expiration time. */
131 case ATIMER_ABSOLUTE
:
132 t
->expiration
= time
;
135 case ATIMER_RELATIVE
:
136 EMACS_GET_TIME (t
->expiration
);
137 EMACS_ADD_TIME (t
->expiration
, t
->expiration
, time
);
140 case ATIMER_CONTINUOUS
:
141 EMACS_GET_TIME (t
->expiration
);
142 EMACS_ADD_TIME (t
->expiration
, t
->expiration
, time
);
147 /* Insert the timer in the list of active atimers. */
151 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
158 /* Cancel and free atimer TIMER. */
161 cancel_atimer (timer
)
162 struct atimer
*timer
;
168 for (i
= 0; i
< 2; ++i
)
170 struct atimer
*t
, *prev
;
171 struct atimer
**list
= i
? &stopped_atimers
: &atimers
;
173 /* See if TIMER is active or stopped. */
174 for (t
= *list
, prev
= NULL
; t
&& t
!= timer
; prev
= t
, t
= t
->next
)
177 /* If it is, take it off the its list, and put in on the
178 free-list. We don't bother to arrange for setting a
179 different alarm time, since a too early one doesn't hurt. */
183 prev
->next
= t
->next
;
187 t
->next
= free_atimers
;
197 /* Append two lists of atimers LIST1 and LIST2 and return the
200 static struct atimer
*
201 append_atimer_lists (list1
, list2
)
202 struct atimer
*list1
, *list2
;
206 else if (list2
== NULL
)
212 for (p
= list1
; p
->next
; p
= p
->next
)
220 /* Stop all timers except timer T. T null means stop all timers. */
223 stop_other_atimers (t
)
230 struct atimer
*p
, *prev
;
232 /* See if T is active. */
233 for (p
= atimers
, prev
= NULL
; p
&& p
!= t
; prev
= p
, p
= p
->next
)
239 prev
->next
= t
->next
;
245 /* T is not active. Let's handle this like T == 0. */
249 stopped_atimers
= append_atimer_lists (atimers
, stopped_atimers
);
255 /* Run all timers again, if some have been stopped with a call to
256 stop_other_atimers. */
263 struct atimer
*t
= atimers
;
267 atimers
= stopped_atimers
;
268 stopped_atimers
= NULL
;
282 /* A version of run_all_timers suitable for a record_unwind_protect. */
285 unwind_stop_other_atimers (dummy
)
293 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
298 #if defined (USG) && !defined (POSIX_SIGNALS)
299 /* USG systems forget handlers when they are used;
300 must reestablish each time. */
301 signal (SIGALRM
, alarm_signal_handler
);
306 EMACS_TIME now
, time
;
307 #ifdef HAVE_SETITIMER
311 /* Determine s/us till the next timer is ripe. */
312 EMACS_GET_TIME (now
);
313 EMACS_SUB_TIME (time
, atimers
->expiration
, now
);
315 #ifdef HAVE_SETITIMER
316 /* Don't set the interval to 0; this disables the timer. */
317 if (EMACS_TIME_LE (atimers
->expiration
, now
))
319 EMACS_SET_SECS (time
, 0);
320 EMACS_SET_USECS (time
, 1000);
323 bzero (&it
, sizeof it
);
325 setitimer (ITIMER_REAL
, &it
, 0);
326 #else /* not HAVE_SETITIMER */
327 alarm (max (EMACS_SECS (time
), 1));
328 #endif /* not HAVE_SETITIMER */
333 /* Insert timer T into the list of active atimers `atimers', keeping
334 the list sorted by expiration time. T must not be in this list
341 struct atimer
*a
= atimers
, *prev
= NULL
;
343 /* Look for the first atimer that is ripe after T. */
344 while (a
&& EMACS_TIME_GT (t
->expiration
, a
->expiration
))
345 prev
= a
, a
= a
->next
;
347 /* Insert T in front of the atimer found, if any. */
361 EMACS_GET_TIME (now
);
364 && (pending_atimers
= interrupt_input_blocked
) == 0
365 && EMACS_TIME_LE (atimers
->expiration
, now
))
370 atimers
= atimers
->next
;
373 if (t
->type
== ATIMER_CONTINUOUS
)
375 EMACS_ADD_TIME (t
->expiration
, now
, t
->interval
);
380 t
->next
= free_atimers
;
384 EMACS_GET_TIME (now
);
387 if (! pending_atimers
)
392 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
396 alarm_signal_handler (signo
)
406 /* Call alarm_signal_handler for pending timers. */
409 do_pending_atimers ()
420 /* Turn alarms on/off. This seems to be temporarily necessary on
421 some systems like HPUX (see process.c). */
429 signal (SIGALRM
, alarm_signal_handler
);
440 free_atimers
= atimers
= NULL
;
442 signal (SIGALRM
, alarm_signal_handler
);
445 /* arch-tag: e6308261-eec6-404b-89fb-6e5909518d70
446 (do not change this comment) */