1 /* Asynchronous timers.
2 Copyright (C) 2000-2012 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 3 of the License, or
9 (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>. */
24 #include "syssignal.h"
26 #include "blockinput.h"
30 #ifdef HAVE_SYS_TIME_H
34 /* Free-list of atimer structures. */
36 static struct atimer
*free_atimers
;
38 /* List of currently not running timers due to a call to
41 static struct atimer
*stopped_atimers
;
43 /* List of active atimers, sorted by expiration time. The timer that
44 will become ripe next is always at the front of this list. */
46 static struct atimer
*atimers
;
48 /* Non-zero means alarm_signal_handler has found ripe timers but
49 interrupt_input_blocked was non-zero. In this case, timer
50 functions are not called until the next UNBLOCK_INPUT because timer
51 functions are expected to call X, and X cannot be assumed to be
56 /* Block/unblock SIGALRM. */
58 #define BLOCK_ATIMERS sigblock (sigmask (SIGALRM))
59 #define UNBLOCK_ATIMERS sigunblock (sigmask (SIGALRM))
61 /* Function prototypes. */
63 static void set_alarm (void);
64 static void schedule_atimer (struct atimer
*);
65 static struct atimer
*append_atimer_lists (struct atimer
*,
67 static void alarm_signal_handler (int signo
);
70 /* Start a new atimer of type TYPE. TIME specifies when the timer is
71 ripe. FN is the function to call when the timer fires.
72 CLIENT_DATA is stored in the client_data member of the atimer
73 structure returned and so made available to FN when it is called.
75 If TYPE is ATIMER_ABSOLUTE, TIME is the absolute time at which the
78 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the
81 In both cases, the timer is automatically freed after it has fired.
83 If TYPE is ATIMER_CONTINUOUS, the timer fires every TIME s/us.
85 Value is a pointer to the atimer started. It can be used in calls
86 to cancel_atimer; don't free it yourself. */
89 start_atimer (enum atimer_type type
, EMACS_TIME timestamp
, atimer_callback fn
,
94 /* Round TIME up to the next full second if we don't have
96 #ifndef HAVE_SETITIMER
97 if (EMACS_USECS (timestamp
) != 0)
99 EMACS_SET_USECS (timestamp
, 0);
100 EMACS_SET_SECS (timestamp
, EMACS_SECS (timestamp
) + 1);
102 #endif /* not HAVE_SETITIMER */
104 /* Get an atimer structure from the free-list, or allocate
109 free_atimers
= t
->next
;
112 t
= (struct atimer
*) xmalloc (sizeof *t
);
114 /* Fill the atimer structure. */
115 memset (t
, 0, sizeof *t
);
118 t
->client_data
= client_data
;
122 /* Compute the timer's expiration time. */
125 case ATIMER_ABSOLUTE
:
126 t
->expiration
= timestamp
;
129 case ATIMER_RELATIVE
:
130 EMACS_GET_TIME (t
->expiration
);
131 EMACS_ADD_TIME (t
->expiration
, t
->expiration
, timestamp
);
134 case ATIMER_CONTINUOUS
:
135 EMACS_GET_TIME (t
->expiration
);
136 EMACS_ADD_TIME (t
->expiration
, t
->expiration
, timestamp
);
137 t
->interval
= timestamp
;
141 /* Insert the timer in the list of active atimers. */
145 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
152 /* Cancel and free atimer TIMER. */
155 cancel_atimer (struct atimer
*timer
)
161 for (i
= 0; i
< 2; ++i
)
163 struct atimer
*t
, *prev
;
164 struct atimer
**list
= i
? &stopped_atimers
: &atimers
;
166 /* See if TIMER is active or stopped. */
167 for (t
= *list
, prev
= NULL
; t
&& t
!= timer
; prev
= t
, t
= t
->next
)
170 /* If it is, take it off its list, and put in on the free-list.
171 We don't bother to arrange for setting a different alarm time,
172 since a too early one doesn't hurt. */
176 prev
->next
= t
->next
;
180 t
->next
= free_atimers
;
190 /* Append two lists of atimers LIST_1 and LIST_2 and return the
193 static struct atimer
*
194 append_atimer_lists (struct atimer
*list_1
, struct atimer
*list_2
)
198 else if (list_2
== NULL
)
204 for (p
= list_1
; p
->next
; p
= p
->next
)
212 /* Stop all timers except timer T. T null means stop all timers. */
215 stop_other_atimers (struct atimer
*t
)
221 struct atimer
*p
, *prev
;
223 /* See if T is active. */
224 for (p
= atimers
, prev
= NULL
; p
&& p
!= t
; prev
= p
, p
= p
->next
)
230 prev
->next
= t
->next
;
236 /* T is not active. Let's handle this like T == 0. */
240 stopped_atimers
= append_atimer_lists (atimers
, stopped_atimers
);
246 /* Run all timers again, if some have been stopped with a call to
247 stop_other_atimers. */
250 run_all_atimers (void)
254 struct atimer
*t
= atimers
;
258 atimers
= stopped_atimers
;
259 stopped_atimers
= NULL
;
273 /* A version of run_all_atimers suitable for a record_unwind_protect. */
276 unwind_stop_other_atimers (Lisp_Object dummy
)
283 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
290 EMACS_TIME now
, timestamp
;
291 #ifdef HAVE_SETITIMER
295 /* Determine s/us till the next timer is ripe. */
296 EMACS_GET_TIME (now
);
297 EMACS_SUB_TIME (timestamp
, atimers
->expiration
, now
);
299 #ifdef HAVE_SETITIMER
300 /* Don't set the interval to 0; this disables the timer. */
301 if (EMACS_TIME_LE (atimers
->expiration
, now
))
303 EMACS_SET_SECS (timestamp
, 0);
304 EMACS_SET_USECS (timestamp
, 1000);
307 memset (&it
, 0, sizeof it
);
308 it
.it_value
= timestamp
;
309 setitimer (ITIMER_REAL
, &it
, 0);
310 #else /* not HAVE_SETITIMER */
311 alarm (max (EMACS_SECS (timestamp
), 1));
312 #endif /* not HAVE_SETITIMER */
317 /* Insert timer T into the list of active atimers `atimers', keeping
318 the list sorted by expiration time. T must not be in this list
322 schedule_atimer (struct atimer
*t
)
324 struct atimer
*a
= atimers
, *prev
= NULL
;
326 /* Look for the first atimer that is ripe after T. */
327 while (a
&& EMACS_TIME_GT (t
->expiration
, a
->expiration
))
328 prev
= a
, a
= a
->next
;
330 /* Insert T in front of the atimer found, if any. */
344 EMACS_GET_TIME (now
);
347 && (pending_atimers
= interrupt_input_blocked
) == 0
348 && EMACS_TIME_LE (atimers
->expiration
, now
))
353 atimers
= atimers
->next
;
356 if (t
->type
== ATIMER_CONTINUOUS
)
358 EMACS_ADD_TIME (t
->expiration
, now
, t
->interval
);
363 t
->next
= free_atimers
;
367 EMACS_GET_TIME (now
);
378 pending_signals
= interrupt_input_pending
;
382 if (! pending_atimers
)
388 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
392 alarm_signal_handler (int signo
)
395 SIGNAL_THREAD_CHECK (signo
);
407 /* Call alarm_signal_handler for pending timers. */
410 do_pending_atimers (void)
421 /* Turn alarms on/off. This seems to be temporarily necessary on
422 some systems like HPUX (see process.c). */
425 turn_on_atimers (int on
)
429 signal (SIGALRM
, alarm_signal_handler
);
440 free_atimers
= stopped_atimers
= atimers
= NULL
;
442 /* pending_signals is initialized in init_keyboard.*/
443 signal (SIGALRM
, alarm_signal_handler
);