1 /* Asynchronous timers.
2 Copyright (C) 2000, 2004 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 2, or (at your option)
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; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
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
P_ ((void));
68 static void schedule_atimer
P_ ((struct atimer
*));
69 static struct atimer
*append_atimer_lists
P_ ((struct atimer
*,
71 SIGTYPE
alarm_signal_handler ();
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 (type
, time
, fn
, client_data
)
94 enum atimer_type type
;
101 /* Round TIME up to the next full second if we don't have
103 #ifndef HAVE_SETITIMER
104 if (EMACS_USECS (time
) != 0)
106 EMACS_SET_USECS (time
, 0);
107 EMACS_SET_SECS (time
, EMACS_SECS (time
) + 1);
109 #endif /* not HAVE_SETITIMER */
111 /* Get an atimer structure from the free-list, or allocate
116 free_atimers
= t
->next
;
119 t
= (struct atimer
*) xmalloc (sizeof *t
);
121 /* Fill the atimer structure. */
122 bzero (t
, sizeof *t
);
125 t
->client_data
= client_data
;
129 /* Compute the timer's expiration time. */
132 case ATIMER_ABSOLUTE
:
133 t
->expiration
= time
;
136 case ATIMER_RELATIVE
:
137 EMACS_GET_TIME (t
->expiration
);
138 EMACS_ADD_TIME (t
->expiration
, t
->expiration
, time
);
141 case ATIMER_CONTINUOUS
:
142 EMACS_GET_TIME (t
->expiration
);
143 EMACS_ADD_TIME (t
->expiration
, t
->expiration
, time
);
148 /* Insert the timer in the list of active atimers. */
152 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
159 /* Cancel and free atimer TIMER. */
162 cancel_atimer (timer
)
163 struct atimer
*timer
;
169 for (i
= 0; i
< 2; ++i
)
171 struct atimer
*t
, *prev
;
172 struct atimer
**list
= i
? &stopped_atimers
: &atimers
;
174 /* See if TIMER is active or stopped. */
175 for (t
= *list
, prev
= NULL
; t
&& t
!= timer
; prev
= t
, t
= t
->next
)
178 /* If it is, take it off the its list, and put in on the
179 free-list. We don't bother to arrange for setting a
180 different alarm time, since a too early one doesn't hurt. */
184 prev
->next
= t
->next
;
188 t
->next
= free_atimers
;
198 /* Append two lists of atimers LIST1 and LIST2 and return the
201 static struct atimer
*
202 append_atimer_lists (list1
, list2
)
203 struct atimer
*list1
, *list2
;
207 else if (list2
== NULL
)
213 for (p
= list1
; p
->next
; p
= p
->next
)
221 /* Stop all timers except timer T. T null means stop all timers. */
224 stop_other_atimers (t
)
231 struct atimer
*p
, *prev
;
233 /* See if T is active. */
234 for (p
= atimers
, prev
= 0; p
&& p
!= t
; p
= p
->next
)
240 prev
->next
= t
->next
;
246 /* T is not active. Let's handle this like T == 0. */
250 stopped_atimers
= append_atimer_lists (atimers
, stopped_atimers
);
256 /* Run all timers again, if some have been stopped with a call to
257 stop_other_atimers. */
264 struct atimer
*t
= atimers
;
268 atimers
= stopped_atimers
;
269 stopped_atimers
= NULL
;
283 /* A version of run_all_timers suitable for a record_unwind_protect. */
286 unwind_stop_other_atimers (dummy
)
294 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
299 #if defined (USG) && !defined (POSIX_SIGNALS)
300 /* USG systems forget handlers when they are used;
301 must reestablish each time. */
302 signal (SIGALRM
, alarm_signal_handler
);
307 EMACS_TIME now
, time
;
308 #ifdef HAVE_SETITIMER
312 /* Determine s/us till the next timer is ripe. */
313 EMACS_GET_TIME (now
);
314 EMACS_SUB_TIME (time
, atimers
->expiration
, now
);
316 #ifdef HAVE_SETITIMER
317 /* Don't set the interval to 0; this disables the timer. */
318 if (EMACS_TIME_LE (atimers
->expiration
, now
))
320 EMACS_SET_SECS (time
, 0);
321 EMACS_SET_USECS (time
, 1000);
324 bzero (&it
, sizeof it
);
326 setitimer (ITIMER_REAL
, &it
, 0);
327 #else /* not HAVE_SETITIMER */
328 alarm (max (EMACS_SECS (time
), 1));
329 #endif /* not HAVE_SETITIMER */
334 /* Insert timer T into the list of active atimers `atimers', keeping
335 the list sorted by expiration time. T must not be in this list
342 struct atimer
*a
= atimers
, *prev
= NULL
;
344 /* Look for the first atimer that is ripe after T. */
345 while (a
&& EMACS_TIME_GT (t
->expiration
, a
->expiration
))
346 prev
= a
, a
= a
->next
;
348 /* Insert T in front of the atimer found, if any. */
358 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
362 alarm_signal_handler (signo
)
367 EMACS_GET_TIME (now
);
371 && (pending_atimers
= interrupt_input_blocked
) == 0
372 && EMACS_TIME_LE (atimers
->expiration
, now
))
377 atimers
= atimers
->next
;
382 if (t
->type
== ATIMER_CONTINUOUS
)
384 EMACS_ADD_TIME (t
->expiration
, now
, t
->interval
);
389 t
->next
= free_atimers
;
393 /* Fix for Ctrl-G. Perhaps this should apply to all platforms. */
397 EMACS_GET_TIME (now
);
400 if (! pending_atimers
)
405 /* Call alarm_signal_handler for pending timers. */
408 do_pending_atimers ()
413 alarm_signal_handler (SIGALRM
);
419 /* Turn alarms on/off. This seems to be temporarily necessary on
420 some systems like HPUX (see process.c). */
428 signal (SIGALRM
, alarm_signal_handler
);
439 free_atimers
= atimers
= NULL
;
441 signal (SIGALRM
, alarm_signal_handler
);
444 /* arch-tag: e6308261-eec6-404b-89fb-6e5909518d70
445 (do not change this comment) */