* lisp/gnus/smime.el (smime-mode): Use define-derived-mode.
[emacs.git] / src / atimer.c
blob6258908e0b22bc997882b7b09edfdb23b76ff92c
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/>. */
20 #include <config.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <setjmp.h>
24 #include "lisp.h"
25 #include "syssignal.h"
26 #include "systime.h"
27 #include "blockinput.h"
28 #include "atimer.h"
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
34 #ifdef HAVE_SYS_TIME_H
35 #include <sys/time.h>
36 #endif
38 /* Free-list of atimer structures. */
40 static struct atimer *free_atimers;
42 /* List of currently not running timers due to a call to
43 lock_atimer. */
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
56 reentrant. */
58 int pending_atimers;
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 *,
70 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
80 timer fires.
82 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the
83 future.
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. */
92 struct atimer *
93 start_atimer (enum atimer_type type, EMACS_TIME time, atimer_callback fn,
94 void *client_data)
96 struct atimer *t;
98 /* Round TIME up to the next full second if we don't have
99 itimers. */
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
109 a new one. */
110 if (free_atimers)
112 t = free_atimers;
113 free_atimers = t->next;
115 else
116 t = (struct atimer *) xmalloc (sizeof *t);
118 /* Fill the atimer structure. */
119 memset (t, 0, sizeof *t);
120 t->type = type;
121 t->fn = fn;
122 t->client_data = client_data;
124 BLOCK_ATIMERS;
126 /* Compute the timer's expiration time. */
127 switch (type)
129 case ATIMER_ABSOLUTE:
130 t->expiration = time;
131 break;
133 case ATIMER_RELATIVE:
134 EMACS_GET_TIME (t->expiration);
135 EMACS_ADD_TIME (t->expiration, t->expiration, time);
136 break;
138 case ATIMER_CONTINUOUS:
139 EMACS_GET_TIME (t->expiration);
140 EMACS_ADD_TIME (t->expiration, t->expiration, time);
141 t->interval = time;
142 break;
145 /* Insert the timer in the list of active atimers. */
146 schedule_atimer (t);
147 UNBLOCK_ATIMERS;
149 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
150 set_alarm ();
152 return t;
156 /* Cancel and free atimer TIMER. */
158 void
159 cancel_atimer (struct atimer *timer)
161 int i;
163 BLOCK_ATIMERS;
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. */
177 if (t)
179 if (prev)
180 prev->next = t->next;
181 else
182 *list = t->next;
184 t->next = free_atimers;
185 free_atimers = t;
186 break;
190 UNBLOCK_ATIMERS;
194 /* Append two lists of atimers LIST1 and LIST2 and return the
195 result list. */
197 static struct atimer *
198 append_atimer_lists (struct atimer *list1, struct atimer *list2)
200 if (list1 == NULL)
201 return list2;
202 else if (list2 == NULL)
203 return list1;
204 else
206 struct atimer *p;
208 for (p = list1; p->next; p = p->next)
210 p->next = list2;
211 return list1;
216 /* Stop all timers except timer T. T null means stop all timers. */
218 void
219 stop_other_atimers (struct atimer *t)
221 BLOCK_ATIMERS;
223 if (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)
231 if (p == t)
233 if (prev)
234 prev->next = t->next;
235 else
236 atimers = t->next;
237 t->next = NULL;
239 else
240 /* T is not active. Let's handle this like T == 0. */
241 t = NULL;
244 stopped_atimers = append_atimer_lists (atimers, stopped_atimers);
245 atimers = t;
246 UNBLOCK_ATIMERS;
250 /* Run all timers again, if some have been stopped with a call to
251 stop_other_atimers. */
253 void
254 run_all_atimers (void)
256 if (stopped_atimers)
258 struct atimer *t = atimers;
259 struct atimer *next;
261 BLOCK_ATIMERS;
262 atimers = stopped_atimers;
263 stopped_atimers = NULL;
265 while (t)
267 next = t->next;
268 schedule_atimer (t);
269 t = next;
272 UNBLOCK_ATIMERS;
277 /* A version of run_all_timers suitable for a record_unwind_protect. */
279 Lisp_Object
280 unwind_stop_other_atimers (Lisp_Object dummy)
282 run_all_atimers ();
283 return Qnil;
287 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
289 static void
290 set_alarm (void)
292 if (atimers)
294 EMACS_TIME now, time;
295 #ifdef HAVE_SETITIMER
296 struct itimerval it;
297 #endif
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);
312 it.it_value = time;
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
323 already. */
325 static void
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. */
335 if (prev)
336 prev->next = t;
337 else
338 atimers = t;
340 t->next = a;
343 static void
344 run_timers (void)
346 EMACS_TIME now;
348 EMACS_GET_TIME (now);
350 while (atimers
351 && (pending_atimers = interrupt_input_blocked) == 0
352 && EMACS_TIME_LE (atimers->expiration, now))
354 struct atimer *t;
356 t = atimers;
357 atimers = atimers->next;
358 t->fn (t);
360 if (t->type == ATIMER_CONTINUOUS)
362 EMACS_ADD_TIME (t->expiration, now, t->interval);
363 schedule_atimer (t);
365 else
367 t->next = free_atimers;
368 free_atimers = t;
371 EMACS_GET_TIME (now);
374 if (! atimers)
375 pending_atimers = 0;
377 #ifdef SYNC_INPUT
378 if (pending_atimers)
379 pending_signals = 1;
380 else
382 pending_signals = interrupt_input_pending;
383 set_alarm ();
385 #else
386 if (! pending_atimers)
387 set_alarm ();
388 #endif
392 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
393 SIGALRM. */
395 SIGTYPE
396 alarm_signal_handler (int signo)
398 #ifndef SYNC_INPUT
399 SIGNAL_THREAD_CHECK (signo);
400 #endif
402 pending_atimers = 1;
403 #ifdef SYNC_INPUT
404 pending_signals = 1;
405 #else
406 run_timers ();
407 #endif
411 /* Call alarm_signal_handler for pending timers. */
413 void
414 do_pending_atimers (void)
416 if (pending_atimers)
418 BLOCK_ATIMERS;
419 run_timers ();
420 UNBLOCK_ATIMERS;
425 /* Turn alarms on/off. This seems to be temporarily necessary on
426 some systems like HPUX (see process.c). */
428 void
429 turn_on_atimers (int on)
431 if (on)
433 signal (SIGALRM, alarm_signal_handler);
434 set_alarm ();
436 else
437 alarm (0);
441 void
442 init_atimer (void)
444 free_atimers = stopped_atimers = atimers = NULL;
445 pending_atimers = 0;
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) */