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
, EMACS_TIME time
, atimer_callback fn
,
98 /* Round TIME up to the next full second if we don't have
100 #ifndef HAVE_SETITIMER
101 if (EMACS_USECS (time
) != 0)
103 EMACS_SET_USECS (time
, 0);
104 EMACS_SET_SECS (time
, EMACS_SECS (time
) + 1);
106 #endif /* not HAVE_SETITIMER */
108 /* Get an atimer structure from the free-list, or allocate
113 free_atimers
= t
->next
;
116 t
= (struct atimer
*) xmalloc (sizeof *t
);
118 /* Fill the atimer structure. */
119 memset (t
, 0, sizeof *t
);
122 t
->client_data
= client_data
;
126 /* Compute the timer's expiration time. */
129 case ATIMER_ABSOLUTE
:
130 t
->expiration
= time
;
133 case ATIMER_RELATIVE
:
134 EMACS_GET_TIME (t
->expiration
);
135 EMACS_ADD_TIME (t
->expiration
, t
->expiration
, time
);
138 case ATIMER_CONTINUOUS
:
139 EMACS_GET_TIME (t
->expiration
);
140 EMACS_ADD_TIME (t
->expiration
, t
->expiration
, time
);
145 /* Insert the timer in the list of active atimers. */
149 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
156 /* Cancel and free atimer TIMER. */
159 cancel_atimer (struct atimer
*timer
)
165 for (i
= 0; i
< 2; ++i
)
167 struct atimer
*t
, *prev
;
168 struct atimer
**list
= i
? &stopped_atimers
: &atimers
;
170 /* See if TIMER is active or stopped. */
171 for (t
= *list
, prev
= NULL
; t
&& t
!= timer
; prev
= t
, t
= t
->next
)
174 /* If it is, take it off its list, and put in on the free-list.
175 We don't bother to arrange for setting a different alarm time,
176 since a too early one doesn't hurt. */
180 prev
->next
= t
->next
;
184 t
->next
= free_atimers
;
194 /* Append two lists of atimers LIST1 and LIST2 and return the
197 static struct atimer
*
198 append_atimer_lists (struct atimer
*list1
, struct atimer
*list2
)
202 else if (list2
== NULL
)
208 for (p
= list1
; p
->next
; p
= p
->next
)
216 /* Stop all timers except timer T. T null means stop all timers. */
219 stop_other_atimers (struct atimer
*t
)
225 struct atimer
*p
, *prev
;
227 /* See if T is active. */
228 for (p
= atimers
, prev
= NULL
; p
&& p
!= t
; prev
= p
, p
= p
->next
)
234 prev
->next
= t
->next
;
240 /* T is not active. Let's handle this like T == 0. */
244 stopped_atimers
= append_atimer_lists (atimers
, stopped_atimers
);
250 /* Run all timers again, if some have been stopped with a call to
251 stop_other_atimers. */
254 run_all_atimers (void)
258 struct atimer
*t
= atimers
;
262 atimers
= stopped_atimers
;
263 stopped_atimers
= NULL
;
277 /* A version of run_all_timers suitable for a record_unwind_protect. */
280 unwind_stop_other_atimers (Lisp_Object dummy
)
287 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
294 EMACS_TIME now
, time
;
295 #ifdef HAVE_SETITIMER
299 /* Determine s/us till the next timer is ripe. */
300 EMACS_GET_TIME (now
);
301 EMACS_SUB_TIME (time
, atimers
->expiration
, now
);
303 #ifdef HAVE_SETITIMER
304 /* Don't set the interval to 0; this disables the timer. */
305 if (EMACS_TIME_LE (atimers
->expiration
, now
))
307 EMACS_SET_SECS (time
, 0);
308 EMACS_SET_USECS (time
, 1000);
311 memset (&it
, 0, sizeof it
);
313 setitimer (ITIMER_REAL
, &it
, 0);
314 #else /* not HAVE_SETITIMER */
315 alarm (max (EMACS_SECS (time
), 1));
316 #endif /* not HAVE_SETITIMER */
321 /* Insert timer T into the list of active atimers `atimers', keeping
322 the list sorted by expiration time. T must not be in this list
326 schedule_atimer (struct atimer
*t
)
328 struct atimer
*a
= atimers
, *prev
= NULL
;
330 /* Look for the first atimer that is ripe after T. */
331 while (a
&& EMACS_TIME_GT (t
->expiration
, a
->expiration
))
332 prev
= a
, a
= a
->next
;
334 /* Insert T in front of the atimer found, if any. */
348 EMACS_GET_TIME (now
);
351 && (pending_atimers
= interrupt_input_blocked
) == 0
352 && EMACS_TIME_LE (atimers
->expiration
, now
))
357 atimers
= atimers
->next
;
360 if (t
->type
== ATIMER_CONTINUOUS
)
362 EMACS_ADD_TIME (t
->expiration
, now
, t
->interval
);
367 t
->next
= free_atimers
;
371 EMACS_GET_TIME (now
);
382 pending_signals
= interrupt_input_pending
;
386 if (! pending_atimers
)
392 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
396 alarm_signal_handler (int signo
)
399 SIGNAL_THREAD_CHECK (signo
);
411 /* Call alarm_signal_handler for pending timers. */
414 do_pending_atimers (void)
425 /* Turn alarms on/off. This seems to be temporarily necessary on
426 some systems like HPUX (see process.c). */
429 turn_on_atimers (int on
)
433 signal (SIGALRM
, alarm_signal_handler
);
444 free_atimers
= stopped_atimers
= atimers
= NULL
;
446 /* pending_signals is initialized in init_keyboard.*/
447 signal (SIGALRM
, alarm_signal_handler
);
450 /* arch-tag: e6308261-eec6-404b-89fb-6e5909518d70
451 (do not change this comment) */