Timestamp fixes for undo.
[bpt/emacs.git] / src / atimer.c
CommitLineData
e12489f9 1/* Asynchronous timers.
ab422c4d 2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
e12489f9
GM
3
4This file is part of GNU Emacs.
5
9ec0b715 6GNU Emacs is free software: you can redistribute it and/or modify
e12489f9 7it under the terms of the GNU General Public License as published by
9ec0b715
GM
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
e12489f9
GM
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
9ec0b715 17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
e12489f9
GM
18
19#include <config.h>
5890e9f7 20#include <stdio.h>
0328b6de 21
898b4c5c
DN
22#include "lisp.h"
23#include "syssignal.h"
24#include "systime.h"
25#include "blockinput.h"
26#include "atimer.h"
e12489f9 27#include <unistd.h>
e12489f9 28
e12489f9
GM
29/* Free-list of atimer structures. */
30
31static struct atimer *free_atimers;
32
9c2e3e8d
GM
33/* List of currently not running timers due to a call to
34 lock_atimer. */
35
36static struct atimer *stopped_atimers;
37
e12489f9
GM
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
41static struct atimer *atimers;
42
e26fd2e4
PE
43/* The alarm timer and whether it was properly initialized, if
44 POSIX timers are available. */
2b794d69 45#ifdef HAVE_ITIMERSPEC
e26fd2e4
PE
46static timer_t alarm_timer;
47static bool alarm_timer_ok;
48#endif
49
5f351ff1 50/* Block/unblock SIGALRM. */
e12489f9 51
2fe28299
PE
52static void
53sigmask_atimers (int how)
54{
55 sigset_t blocked;
56 sigemptyset (&blocked);
57 sigaddset (&blocked, SIGALRM);
58 pthread_sigmask (how, &blocked, 0);
59}
60static void
61block_atimers (void)
62{
63 sigmask_atimers (SIG_BLOCK);
64}
65static void
66unblock_atimers (void)
67{
68 sigmask_atimers (SIG_UNBLOCK);
69}
e12489f9
GM
70
71/* Function prototypes. */
72
f57e2426
J
73static void set_alarm (void);
74static void schedule_atimer (struct atimer *);
75static struct atimer *append_atimer_lists (struct atimer *,
76 struct atimer *);
e12489f9
GM
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
96struct atimer *
3e7d6594 97start_atimer (enum atimer_type type, EMACS_TIME timestamp, atimer_callback fn,
e5447b22 98 void *client_data)
e12489f9
GM
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
d35af63c
PE
105 if (EMACS_NSECS (timestamp) != 0
106 && EMACS_SECS (timestamp) < TYPE_MAXIMUM (time_t))
e9a9ae03 107 timestamp = make_emacs_time (EMACS_SECS (timestamp) + 1, 0);
e12489f9
GM
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
23f86fce 118 t = xmalloc (sizeof *t);
e12489f9
GM
119
120 /* Fill the atimer structure. */
72af86bd 121 memset (t, 0, sizeof *t);
e12489f9
GM
122 t->type = type;
123 t->fn = fn;
124 t->client_data = client_data;
125
2fe28299 126 block_atimers ();
e12489f9
GM
127
128 /* Compute the timer's expiration time. */
129 switch (type)
130 {
131 case ATIMER_ABSOLUTE:
3e7d6594 132 t->expiration = timestamp;
e12489f9 133 break;
177c0ea7 134
e12489f9 135 case ATIMER_RELATIVE:
e9a9ae03 136 t->expiration = add_emacs_time (current_emacs_time (), timestamp);
e12489f9 137 break;
177c0ea7 138
e12489f9 139 case ATIMER_CONTINUOUS:
e9a9ae03 140 t->expiration = add_emacs_time (current_emacs_time (), timestamp);
3e7d6594 141 t->interval = timestamp;
e12489f9
GM
142 break;
143 }
144
145 /* Insert the timer in the list of active atimers. */
146 schedule_atimer (t);
2fe28299 147 unblock_atimers ();
e12489f9
GM
148
149 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
150 set_alarm ();
177c0ea7 151
e12489f9
GM
152 return t;
153}
154
155
156/* Cancel and free atimer TIMER. */
157
158void
971de7fb 159cancel_atimer (struct atimer *timer)
e12489f9 160{
50b1039f 161 int i;
177c0ea7 162
2fe28299 163 block_atimers ();
e12489f9 164
50b1039f 165 for (i = 0; i < 2; ++i)
94870977 166 {
50b1039f
GM
167 struct atimer *t, *prev;
168 struct atimer **list = i ? &stopped_atimers : &atimers;
177c0ea7 169
94870977 170 /* See if TIMER is active or stopped. */
4ab1d4be 171 for (t = *list, prev = NULL; t && t != timer; prev = t, t = t->next)
94870977
GM
172 ;
173
88dbda51
JB
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. */
94870977
GM
177 if (t)
178 {
179 if (prev)
180 prev->next = t->next;
181 else
182 *list = t->next;
177c0ea7 183
94870977
GM
184 t->next = free_atimers;
185 free_atimers = t;
4ab1d4be 186 break;
94870977 187 }
e12489f9
GM
188 }
189
2fe28299 190 unblock_atimers ();
e12489f9
GM
191}
192
193
3e7d6594 194/* Append two lists of atimers LIST_1 and LIST_2 and return the
50b1039f
GM
195 result list. */
196
197static struct atimer *
3e7d6594 198append_atimer_lists (struct atimer *list_1, struct atimer *list_2)
50b1039f 199{
3e7d6594
PE
200 if (list_1 == NULL)
201 return list_2;
202 else if (list_2 == NULL)
203 return list_1;
50b1039f
GM
204 else
205 {
206 struct atimer *p;
177c0ea7 207
3e7d6594 208 for (p = list_1; p->next; p = p->next)
50b1039f 209 ;
3e7d6594
PE
210 p->next = list_2;
211 return list_1;
50b1039f
GM
212 }
213}
214
215
216/* Stop all timers except timer T. T null means stop all timers. */
9c2e3e8d
GM
217
218void
971de7fb 219stop_other_atimers (struct atimer *t)
9c2e3e8d 220{
2fe28299 221 block_atimers ();
177c0ea7 222
9c2e3e8d
GM
223 if (t)
224 {
2503c8b1 225 struct atimer *p, *prev;
177c0ea7 226
2503c8b1 227 /* See if T is active. */
96013ba9 228 for (p = atimers, prev = NULL; p && p != t; prev = p, p = p->next)
2503c8b1
GM
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;
9c2e3e8d 242 }
177c0ea7 243
50b1039f 244 stopped_atimers = append_atimer_lists (atimers, stopped_atimers);
9c2e3e8d 245 atimers = t;
2fe28299 246 unblock_atimers ();
9c2e3e8d
GM
247}
248
249
250/* Run all timers again, if some have been stopped with a call to
251 stop_other_atimers. */
252
e192d7d3 253static void
971de7fb 254run_all_atimers (void)
9c2e3e8d
GM
255{
256 if (stopped_atimers)
257 {
258 struct atimer *t = atimers;
50b1039f 259 struct atimer *next;
177c0ea7 260
2fe28299 261 block_atimers ();
9c2e3e8d
GM
262 atimers = stopped_atimers;
263 stopped_atimers = NULL;
177c0ea7 264
50b1039f
GM
265 while (t)
266 {
267 next = t->next;
268 schedule_atimer (t);
269 t = next;
270 }
177c0ea7 271
2fe28299 272 unblock_atimers ();
9c2e3e8d
GM
273 }
274}
275
276
e192d7d3 277/* A version of run_all_atimers suitable for a record_unwind_protect. */
9c2e3e8d
GM
278
279Lisp_Object
971de7fb 280unwind_stop_other_atimers (Lisp_Object dummy)
9c2e3e8d
GM
281{
282 run_all_atimers ();
283 return Qnil;
284}
285
286
e12489f9
GM
287/* Arrange for a SIGALRM to arrive when the next timer is ripe. */
288
289static void
971de7fb 290set_alarm (void)
e12489f9 291{
e12489f9
GM
292 if (atimers)
293 {
e12489f9
GM
294#ifdef HAVE_SETITIMER
295 struct itimerval it;
296#endif
e26fd2e4 297 EMACS_TIME now, interval;
e12489f9 298
2b794d69 299#ifdef HAVE_ITIMERSPEC
e26fd2e4
PE
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
e12489f9 309
e26fd2e4
PE
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));
d35af63c
PE
316
317#ifdef HAVE_SETITIMER
177c0ea7 318
72af86bd 319 memset (&it, 0, sizeof it);
e9a9ae03 320 it.it_value = make_timeval (interval);
e12489f9
GM
321 setitimer (ITIMER_REAL, &it, 0);
322#else /* not HAVE_SETITIMER */
e9a9ae03 323 alarm (max (EMACS_SECS (interval), 1));
e12489f9
GM
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
333static void
971de7fb 334schedule_atimer (struct atimer *t)
e12489f9
GM
335{
336 struct atimer *a = atimers, *prev = NULL;
337
338 /* Look for the first atimer that is ripe after T. */
954b166e 339 while (a && EMACS_TIME_LT (a->expiration, t->expiration))
e12489f9
GM
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;
177c0ea7 347
e12489f9
GM
348 t->next = a;
349}
350
170c80be 351static void
971de7fb 352run_timers (void)
e12489f9 353{
4d7e6e51 354 EMACS_TIME now = current_emacs_time ();
177c0ea7 355
4d7e6e51 356 while (atimers && EMACS_TIME_LE (atimers->expiration, now))
e12489f9 357 {
4d7e6e51 358 struct atimer *t = atimers;
e12489f9
GM
359 atimers = atimers->next;
360 t->fn (t);
177c0ea7 361
e12489f9
GM
362 if (t->type == ATIMER_CONTINUOUS)
363 {
e9a9ae03 364 t->expiration = add_emacs_time (now, t->interval);
e12489f9
GM
365 schedule_atimer (t);
366 }
367 else
368 {
369 t->next = free_atimers;
370 free_atimers = t;
371 }
e12489f9 372 }
177c0ea7 373
4d7e6e51 374 set_alarm ();
e12489f9
GM
375}
376
377
170c80be
JD
378/* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
379 SIGALRM. */
380
20ef56db
PE
381static void
382handle_alarm_signal (int sig)
170c80be 383{
325530de 384 pending_signals = 1;
170c80be
JD
385}
386
387
4d7e6e51 388/* Do pending timers. */
e12489f9
GM
389
390void
971de7fb 391do_pending_atimers (void)
e12489f9 392{
4d7e6e51 393 if (atimers)
e12489f9 394 {
2fe28299 395 block_atimers ();
170c80be 396 run_timers ();
2fe28299 397 unblock_atimers ();
e12489f9
GM
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
405void
b1bb8011 406turn_on_atimers (bool on)
e12489f9
GM
407{
408 if (on)
4d7e6e51 409 set_alarm ();
e12489f9
GM
410 else
411 alarm (0);
412}
413
414
415void
971de7fb 416init_atimer (void)
e12489f9 417{
2fe28299 418 struct sigaction action;
2b794d69 419#ifdef HAVE_ITIMERSPEC
e26fd2e4
PE
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
bcdf96ba 426 free_atimers = stopped_atimers = atimers = NULL;
325530de 427 /* pending_signals is initialized in init_keyboard.*/
4d7e6e51 428 emacs_sigaction_init (&action, handle_alarm_signal);
2fe28299 429 sigaction (SIGALRM, &action, 0);
e12489f9 430}