]> code.delx.au - gnu-emacs/blob - src/atimer.c
Port documentation to Texinfo 5.0.
[gnu-emacs] / src / atimer.c
1 /* Asynchronous timers.
2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
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.
10
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.
15
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/>. */
18
19 #include <config.h>
20 #include <stdio.h>
21
22 #include "lisp.h"
23 #include "syssignal.h"
24 #include "systime.h"
25 #include "blockinput.h"
26 #include "atimer.h"
27 #include <unistd.h>
28
29 /* Free-list of atimer structures. */
30
31 static struct atimer *free_atimers;
32
33 /* List of currently not running timers due to a call to
34 lock_atimer. */
35
36 static struct atimer *stopped_atimers;
37
38 /* List of active atimers, sorted by expiration time. The timer that
39 will become ripe next is always at the front of this list. */
40
41 static struct atimer *atimers;
42
43 /* The alarm timer and whether it was properly initialized, if
44 POSIX timers are available. */
45 #ifdef HAVE_ITIMERSPEC
46 static timer_t alarm_timer;
47 static bool alarm_timer_ok;
48 #endif
49
50 /* Block/unblock SIGALRM. */
51
52 static void
53 sigmask_atimers (int how)
54 {
55 sigset_t blocked;
56 sigemptyset (&blocked);
57 sigaddset (&blocked, SIGALRM);
58 pthread_sigmask (how, &blocked, 0);
59 }
60 static void
61 block_atimers (void)
62 {
63 sigmask_atimers (SIG_BLOCK);
64 }
65 static void
66 unblock_atimers (void)
67 {
68 sigmask_atimers (SIG_UNBLOCK);
69 }
70
71 /* Function prototypes. */
72
73 static void set_alarm (void);
74 static void schedule_atimer (struct atimer *);
75 static struct atimer *append_atimer_lists (struct atimer *,
76 struct atimer *);
77
78 /* Start a new atimer of type TYPE. TIME specifies when the timer is
79 ripe. FN is the function to call when the timer fires.
80 CLIENT_DATA is stored in the client_data member of the atimer
81 structure returned and so made available to FN when it is called.
82
83 If TYPE is ATIMER_ABSOLUTE, TIME is the absolute time at which the
84 timer fires.
85
86 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the
87 future.
88
89 In both cases, the timer is automatically freed after it has fired.
90
91 If TYPE is ATIMER_CONTINUOUS, the timer fires every TIME s/us.
92
93 Value is a pointer to the atimer started. It can be used in calls
94 to cancel_atimer; don't free it yourself. */
95
96 struct atimer *
97 start_atimer (enum atimer_type type, EMACS_TIME timestamp, atimer_callback fn,
98 void *client_data)
99 {
100 struct atimer *t;
101
102 /* Round TIME up to the next full second if we don't have
103 itimers. */
104 #ifndef HAVE_SETITIMER
105 if (EMACS_NSECS (timestamp) != 0
106 && EMACS_SECS (timestamp) < TYPE_MAXIMUM (time_t))
107 timestamp = make_emacs_time (EMACS_SECS (timestamp) + 1, 0);
108 #endif /* not HAVE_SETITIMER */
109
110 /* Get an atimer structure from the free-list, or allocate
111 a new one. */
112 if (free_atimers)
113 {
114 t = free_atimers;
115 free_atimers = t->next;
116 }
117 else
118 t = xmalloc (sizeof *t);
119
120 /* Fill the atimer structure. */
121 memset (t, 0, sizeof *t);
122 t->type = type;
123 t->fn = fn;
124 t->client_data = client_data;
125
126 block_atimers ();
127
128 /* Compute the timer's expiration time. */
129 switch (type)
130 {
131 case ATIMER_ABSOLUTE:
132 t->expiration = timestamp;
133 break;
134
135 case ATIMER_RELATIVE:
136 t->expiration = add_emacs_time (current_emacs_time (), timestamp);
137 break;
138
139 case ATIMER_CONTINUOUS:
140 t->expiration = add_emacs_time (current_emacs_time (), timestamp);
141 t->interval = timestamp;
142 break;
143 }
144
145 /* Insert the timer in the list of active atimers. */
146 schedule_atimer (t);
147 unblock_atimers ();
148
149 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
150 set_alarm ();
151
152 return t;
153 }
154
155
156 /* Cancel and free atimer TIMER. */
157
158 void
159 cancel_atimer (struct atimer *timer)
160 {
161 int i;
162
163 block_atimers ();
164
165 for (i = 0; i < 2; ++i)
166 {
167 struct atimer *t, *prev;
168 struct atimer **list = i ? &stopped_atimers : &atimers;
169
170 /* See if TIMER is active or stopped. */
171 for (t = *list, prev = NULL; t && t != timer; prev = t, t = t->next)
172 ;
173
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)
178 {
179 if (prev)
180 prev->next = t->next;
181 else
182 *list = t->next;
183
184 t->next = free_atimers;
185 free_atimers = t;
186 break;
187 }
188 }
189
190 unblock_atimers ();
191 }
192
193
194 /* Append two lists of atimers LIST_1 and LIST_2 and return the
195 result list. */
196
197 static struct atimer *
198 append_atimer_lists (struct atimer *list_1, struct atimer *list_2)
199 {
200 if (list_1 == NULL)
201 return list_2;
202 else if (list_2 == NULL)
203 return list_1;
204 else
205 {
206 struct atimer *p;
207
208 for (p = list_1; p->next; p = p->next)
209 ;
210 p->next = list_2;
211 return list_1;
212 }
213 }
214
215
216 /* Stop all timers except timer T. T null means stop all timers. */
217
218 void
219 stop_other_atimers (struct atimer *t)
220 {
221 block_atimers ();
222
223 if (t)
224 {
225 struct atimer *p, *prev;
226
227 /* See if T is active. */
228 for (p = atimers, prev = NULL; p && p != t; prev = p, p = p->next)
229 ;
230
231 if (p == t)
232 {
233 if (prev)
234 prev->next = t->next;
235 else
236 atimers = t->next;
237 t->next = NULL;
238 }
239 else
240 /* T is not active. Let's handle this like T == 0. */
241 t = NULL;
242 }
243
244 stopped_atimers = append_atimer_lists (atimers, stopped_atimers);
245 atimers = t;
246 unblock_atimers ();
247 }
248
249
250 /* Run all timers again, if some have been stopped with a call to
251 stop_other_atimers. */
252
253 static void
254 run_all_atimers (void)
255 {
256 if (stopped_atimers)
257 {
258 struct atimer *t = atimers;
259 struct atimer *next;
260
261 block_atimers ();
262 atimers = stopped_atimers;
263 stopped_atimers = NULL;
264
265 while (t)
266 {
267 next = t->next;
268 schedule_atimer (t);
269 t = next;
270 }
271
272 unblock_atimers ();
273 }
274 }
275
276
277 /* A version of run_all_atimers suitable for a record_unwind_protect. */
278
279 Lisp_Object
280 unwind_stop_other_atimers (Lisp_Object dummy)
281 {
282 run_all_atimers ();
283 return Qnil;
284 }
285
286
287 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
288
289 static void
290 set_alarm (void)
291 {
292 if (atimers)
293 {
294 #ifdef HAVE_SETITIMER
295 struct itimerval it;
296 #endif
297 EMACS_TIME now, interval;
298
299 #ifdef HAVE_ITIMERSPEC
300 if (alarm_timer_ok)
301 {
302 struct itimerspec ispec;
303 ispec.it_value = atimers->expiration;
304 ispec.it_interval.tv_sec = ispec.it_interval.tv_nsec = 0;
305 if (timer_settime (alarm_timer, 0, &ispec, 0) == 0)
306 return;
307 }
308 #endif
309
310 /* Determine interval till the next timer is ripe.
311 Don't set the interval to 0; this disables the timer. */
312 now = current_emacs_time ();
313 interval = (EMACS_TIME_LE (atimers->expiration, now)
314 ? make_emacs_time (0, 1000 * 1000)
315 : sub_emacs_time (atimers->expiration, now));
316
317 #ifdef HAVE_SETITIMER
318
319 memset (&it, 0, sizeof it);
320 it.it_value = make_timeval (interval);
321 setitimer (ITIMER_REAL, &it, 0);
322 #else /* not HAVE_SETITIMER */
323 alarm (max (EMACS_SECS (interval), 1));
324 #endif /* not HAVE_SETITIMER */
325 }
326 }
327
328
329 /* Insert timer T into the list of active atimers `atimers', keeping
330 the list sorted by expiration time. T must not be in this list
331 already. */
332
333 static void
334 schedule_atimer (struct atimer *t)
335 {
336 struct atimer *a = atimers, *prev = NULL;
337
338 /* Look for the first atimer that is ripe after T. */
339 while (a && EMACS_TIME_GT (t->expiration, a->expiration))
340 prev = a, a = a->next;
341
342 /* Insert T in front of the atimer found, if any. */
343 if (prev)
344 prev->next = t;
345 else
346 atimers = t;
347
348 t->next = a;
349 }
350
351 static void
352 run_timers (void)
353 {
354 EMACS_TIME now = current_emacs_time ();
355
356 while (atimers && EMACS_TIME_LE (atimers->expiration, now))
357 {
358 struct atimer *t = atimers;
359 atimers = atimers->next;
360 t->fn (t);
361
362 if (t->type == ATIMER_CONTINUOUS)
363 {
364 t->expiration = add_emacs_time (now, t->interval);
365 schedule_atimer (t);
366 }
367 else
368 {
369 t->next = free_atimers;
370 free_atimers = t;
371 }
372 }
373
374 set_alarm ();
375 }
376
377
378 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
379 SIGALRM. */
380
381 static void
382 handle_alarm_signal (int sig)
383 {
384 pending_signals = 1;
385 }
386
387
388 /* Do pending timers. */
389
390 void
391 do_pending_atimers (void)
392 {
393 if (atimers)
394 {
395 block_atimers ();
396 run_timers ();
397 unblock_atimers ();
398 }
399 }
400
401
402 /* Turn alarms on/off. This seems to be temporarily necessary on
403 some systems like HPUX (see process.c). */
404
405 void
406 turn_on_atimers (bool on)
407 {
408 if (on)
409 set_alarm ();
410 else
411 alarm (0);
412 }
413
414
415 void
416 init_atimer (void)
417 {
418 struct sigaction action;
419 #ifdef HAVE_ITIMERSPEC
420 struct sigevent sigev;
421 sigev.sigev_notify = SIGEV_SIGNAL;
422 sigev.sigev_signo = SIGALRM;
423 sigev.sigev_value.sival_ptr = &alarm_timer;
424 alarm_timer_ok = timer_create (CLOCK_REALTIME, &sigev, &alarm_timer) == 0;
425 #endif
426 free_atimers = stopped_atimers = atimers = NULL;
427 /* pending_signals is initialized in init_keyboard.*/
428 emacs_sigaction_init (&action, handle_alarm_signal);
429 sigaction (SIGALRM, &action, 0);
430 }