1 /* Asynchronous timers.
2 Copyright (C) 2000 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. */
24 #include <syssignal.h>
26 #include <blockinput.h>
34 #ifdef HAVE_SYS_TIME_H
38 /* The ubiquitous min/max macros. */
40 #define max(X, Y) ((X) > (Y) ? (X) : (Y))
41 #define min(X, Y) ((X) < (Y) ? (X) : (Y))
43 /* Free-list of atimer structures. */
45 static struct atimer
*free_atimers
;
47 /* List of currently not running timers due to a call to
50 static struct atimer
*stopped_atimers
;
52 /* List of active atimers, sorted by expiration time. The timer that
53 will become ripe next is always at the front of this list. */
55 static struct atimer
*atimers
;
57 /* Non-zero means alarm_signal_handler has found ripe timers but
58 interrupt_input_blocked was non-zero. In this case, timer
59 functions are not called until the next UNBLOCK_INPUT because timer
60 functions are expected to call X, and X cannot be assumed to be
65 /* Block/unblock SIGALRM.. */
67 #define BLOCK_ATIMERS sigblock (sigmask (SIGALRM))
68 #define UNBLOCK_ATIMERS sigunblock (sigmask (SIGALRM))
70 /* Function prototypes. */
72 static void set_alarm
P_ ((void));
73 static void schedule_atimer
P_ ((struct atimer
*));
74 static struct atimer
*append_atimer_lists
P_ ((struct atimer
*,
76 SIGTYPE
alarm_signal_handler ();
79 /* Start a new atimer of type TYPE. TIME specifies when the timer is
80 ripe. FN is the function to call when the timer fires.
81 CLIENT_DATA is stored in the client_data member of the atimer
82 structure returned and so made available to FN when it is called.
84 If TYPE is ATIMER_ABSOLUTE, TIME is the absolute time at which the
87 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the
90 In both cases, the timer is automatically freed after it has fired.
92 If TYPE is ATIMER_CONTINUOUS, the timer fires every TIME s/us.
94 Value is a pointer to the atimer started. It can be used in calls
95 to cancel_atimer; don't free it yourself. */
98 start_atimer (type
, time
, fn
, client_data
)
99 enum atimer_type type
;
106 /* Round TIME up to the next full second if we don't have
108 #ifndef HAVE_SETITIMER
109 if (EMACS_USECS (time
) != 0)
111 EMACS_SET_USECS (time
, 0);
112 EMACS_SET_SECS (time
, EMACS_SECS (time
) + 1);
114 #endif /* not HAVE_SETITIMER */
116 /* Get an atimer structure from the free-list, or allocate
121 free_atimers
= t
->next
;
124 t
= (struct atimer
*) xmalloc (sizeof *t
);
126 /* Fill the atimer structure. */
127 bzero (t
, sizeof *t
);
130 t
->client_data
= client_data
;
134 /* Compute the timer's expiration time. */
137 case ATIMER_ABSOLUTE
:
138 t
->expiration
= time
;
141 case ATIMER_RELATIVE
:
142 EMACS_GET_TIME (t
->expiration
);
143 EMACS_ADD_TIME (t
->expiration
, t
->expiration
, time
);
146 case ATIMER_CONTINUOUS
:
147 EMACS_GET_TIME (t
->expiration
);
148 EMACS_ADD_TIME (t
->expiration
, t
->expiration
, time
);
153 /* Insert the timer in the list of active atimers. */
157 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
164 /* Cancel and free atimer TIMER. */
167 cancel_atimer (timer
)
168 struct atimer
*timer
;
174 for (i
= 0; i
< 2; ++i
)
176 struct atimer
*t
, *prev
;
177 struct atimer
**list
= i
? &stopped_atimers
: &atimers
;
179 /* See if TIMER is active or stopped. */
180 for (t
= *list
, prev
= NULL
; t
&& t
!= timer
; prev
= t
, t
= t
->next
)
183 /* If it is, take it off the its list, and put in on the
184 free-list. We don't bother to arrange for setting a
185 different alarm time, since a too early one doesn't hurt. */
189 prev
->next
= t
->next
;
193 t
->next
= free_atimers
;
203 /* Append two lists of atimers LIST1 and LIST2 and return the
206 static struct atimer
*
207 append_atimer_lists (list1
, list2
)
208 struct atimer
*list1
, *list2
;
212 else if (list2
== NULL
)
218 for (p
= list1
; p
->next
; p
= p
->next
)
226 /* Stop all timers except timer T. T null means stop all timers. */
229 stop_other_atimers (t
)
236 struct atimer
*p
, *prev
;
238 /* See if T is active. */
239 for (p
= atimers
, prev
= 0; p
&& p
!= t
; p
= p
->next
)
245 prev
->next
= t
->next
;
251 /* T is not active. Let's handle this like T == 0. */
255 stopped_atimers
= append_atimer_lists (atimers
, stopped_atimers
);
261 /* Run all timers again, if some have been stopped with a call to
262 stop_other_atimers. */
269 struct atimer
*t
= atimers
;
273 atimers
= stopped_atimers
;
274 stopped_atimers
= NULL
;
288 /* A version of run_all_timers suitable for a record_unwind_protect. */
291 unwind_stop_other_atimers (dummy
)
299 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
304 #if defined (USG) && !defined (POSIX_SIGNALS)
305 /* USG systems forget handlers when they are used;
306 must reestablish each time. */
307 signal (SIGALRM
, alarm_signal_handler
);
312 EMACS_TIME now
, time
;
313 #ifdef HAVE_SETITIMER
317 /* Determine s/us till the next timer is ripe. */
318 EMACS_GET_TIME (now
);
319 EMACS_SUB_TIME (time
, atimers
->expiration
, now
);
321 #ifdef HAVE_SETITIMER
322 /* Don't set the interval to 0; this disables the timer. */
323 if (EMACS_TIME_LE (atimers
->expiration
, now
))
325 EMACS_SET_SECS (time
, 0);
326 EMACS_SET_USECS (time
, 1000);
329 bzero (&it
, sizeof it
);
331 setitimer (ITIMER_REAL
, &it
, 0);
332 #else /* not HAVE_SETITIMER */
333 alarm (max (EMACS_SECS (time
), 1));
334 #endif /* not HAVE_SETITIMER */
339 /* Insert timer T into the list of active atimers `atimers', keeping
340 the list sorted by expiration time. T must not be in this list
347 struct atimer
*a
= atimers
, *prev
= NULL
;
349 /* Look for the first atimer that is ripe after T. */
350 while (a
&& EMACS_TIME_GT (t
->expiration
, a
->expiration
))
351 prev
= a
, a
= a
->next
;
353 /* Insert T in front of the atimer found, if any. */
363 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
367 alarm_signal_handler (signo
)
372 EMACS_GET_TIME (now
);
376 && (pending_atimers
= interrupt_input_blocked
) == 0
377 && EMACS_TIME_LE (atimers
->expiration
, now
))
382 atimers
= atimers
->next
;
385 if (t
->type
== ATIMER_CONTINUOUS
)
387 EMACS_ADD_TIME (t
->expiration
, now
, t
->interval
);
392 t
->next
= free_atimers
;
396 EMACS_GET_TIME (now
);
399 #if defined (USG) && !defined (POSIX_SIGNALS)
400 /* USG systems forget handlers when they are used;
401 must reestablish each time. */
402 signal (SIGALRM
, alarm_signal_handler
);
409 /* Call alarm_signal_handler for pending timers. */
412 do_pending_atimers ()
417 alarm_signal_handler (SIGALRM
);
423 /* Turn alarms on/off. This seems to be temporarily necessary on
424 some systems like HPUX (see process.c). */
432 signal (SIGALRM
, alarm_signal_handler
);
443 free_atimers
= atimers
= NULL
;
445 signal (SIGALRM
, alarm_signal_handler
);