Commit | Line | Data |
---|---|---|
e12489f9 | 1 | /* Asynchronous timers. |
ba318903 | 2 | Copyright (C) 2000-2014 Free Software Foundation, Inc. |
e12489f9 GM |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
9ec0b715 | 6 | GNU Emacs is free software: you can redistribute it and/or modify |
e12489f9 | 7 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
8 | the Free Software Foundation, either version 3 of the License, or |
9 | (at your option) any later version. | |
e12489f9 GM |
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 | |
9ec0b715 | 17 | along 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 | ||
31 | static struct atimer *free_atimers; | |
32 | ||
9c2e3e8d GM |
33 | /* List of currently not running timers due to a call to |
34 | lock_atimer. */ | |
35 | ||
36 | static 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 | ||
41 | static 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 |
46 | static timer_t alarm_timer; |
47 | static bool alarm_timer_ok; | |
48 | #endif | |
49 | ||
5f351ff1 | 50 | /* Block/unblock SIGALRM. */ |
e12489f9 | 51 | |
2fe28299 | 52 | static void |
1e952f0a | 53 | block_atimers (sigset_t *oldset) |
2fe28299 PE |
54 | { |
55 | sigset_t blocked; | |
56 | sigemptyset (&blocked); | |
57 | sigaddset (&blocked, SIGALRM); | |
1e952f0a | 58 | pthread_sigmask (SIG_BLOCK, &blocked, oldset); |
2fe28299 PE |
59 | } |
60 | static void | |
1e952f0a | 61 | unblock_atimers (sigset_t const *oldset) |
2fe28299 | 62 | { |
1e952f0a | 63 | pthread_sigmask (SIG_SETMASK, oldset, 0); |
2fe28299 | 64 | } |
e12489f9 GM |
65 | |
66 | /* Function prototypes. */ | |
67 | ||
f57e2426 J |
68 | static void set_alarm (void); |
69 | static void schedule_atimer (struct atimer *); | |
70 | static struct atimer *append_atimer_lists (struct atimer *, | |
71 | struct atimer *); | |
e12489f9 GM |
72 | |
73 | /* Start a new atimer of type TYPE. TIME specifies when the timer is | |
74 | ripe. FN is the function to call when the timer fires. | |
75 | CLIENT_DATA is stored in the client_data member of the atimer | |
76 | structure returned and so made available to FN when it is called. | |
77 | ||
78 | If TYPE is ATIMER_ABSOLUTE, TIME is the absolute time at which the | |
79 | timer fires. | |
80 | ||
81 | If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the | |
82 | future. | |
83 | ||
84 | In both cases, the timer is automatically freed after it has fired. | |
85 | ||
86 | If TYPE is ATIMER_CONTINUOUS, the timer fires every TIME s/us. | |
87 | ||
88 | Value is a pointer to the atimer started. It can be used in calls | |
89 | to cancel_atimer; don't free it yourself. */ | |
90 | ||
91 | struct atimer * | |
43aac990 PE |
92 | start_atimer (enum atimer_type type, struct timespec timestamp, |
93 | atimer_callback fn, void *client_data) | |
e12489f9 GM |
94 | { |
95 | struct atimer *t; | |
1e952f0a | 96 | sigset_t oldset; |
e12489f9 GM |
97 | |
98 | /* Round TIME up to the next full second if we don't have | |
99 | itimers. */ | |
100 | #ifndef HAVE_SETITIMER | |
43aac990 PE |
101 | if (timestamp.tv_nsec != 0 && timestamp.tv_sec < TYPE_MAXIMUM (time_t)) |
102 | timestamp = make_timespec (timestamp.tv_sec + 1, 0); | |
e12489f9 GM |
103 | #endif /* not HAVE_SETITIMER */ |
104 | ||
105 | /* Get an atimer structure from the free-list, or allocate | |
106 | a new one. */ | |
107 | if (free_atimers) | |
108 | { | |
109 | t = free_atimers; | |
110 | free_atimers = t->next; | |
111 | } | |
112 | else | |
23f86fce | 113 | t = xmalloc (sizeof *t); |
e12489f9 GM |
114 | |
115 | /* Fill the atimer structure. */ | |
72af86bd | 116 | memset (t, 0, sizeof *t); |
e12489f9 GM |
117 | t->type = type; |
118 | t->fn = fn; | |
119 | t->client_data = client_data; | |
120 | ||
1e952f0a | 121 | block_atimers (&oldset); |
e12489f9 GM |
122 | |
123 | /* Compute the timer's expiration time. */ | |
124 | switch (type) | |
125 | { | |
126 | case ATIMER_ABSOLUTE: | |
3e7d6594 | 127 | t->expiration = timestamp; |
e12489f9 | 128 | break; |
177c0ea7 | 129 | |
e12489f9 | 130 | case ATIMER_RELATIVE: |
43aac990 | 131 | t->expiration = timespec_add (current_timespec (), timestamp); |
e12489f9 | 132 | break; |
177c0ea7 | 133 | |
e12489f9 | 134 | case ATIMER_CONTINUOUS: |
43aac990 | 135 | t->expiration = timespec_add (current_timespec (), timestamp); |
3e7d6594 | 136 | t->interval = timestamp; |
e12489f9 GM |
137 | break; |
138 | } | |
139 | ||
140 | /* Insert the timer in the list of active atimers. */ | |
141 | schedule_atimer (t); | |
1e952f0a | 142 | unblock_atimers (&oldset); |
e12489f9 GM |
143 | |
144 | /* Arrange for a SIGALRM at the time the next atimer is ripe. */ | |
145 | set_alarm (); | |
177c0ea7 | 146 | |
e12489f9 GM |
147 | return t; |
148 | } | |
149 | ||
150 | ||
151 | /* Cancel and free atimer TIMER. */ | |
152 | ||
153 | void | |
971de7fb | 154 | cancel_atimer (struct atimer *timer) |
e12489f9 | 155 | { |
50b1039f | 156 | int i; |
1e952f0a | 157 | sigset_t oldset; |
177c0ea7 | 158 | |
1e952f0a | 159 | block_atimers (&oldset); |
e12489f9 | 160 | |
50b1039f | 161 | for (i = 0; i < 2; ++i) |
94870977 | 162 | { |
50b1039f GM |
163 | struct atimer *t, *prev; |
164 | struct atimer **list = i ? &stopped_atimers : &atimers; | |
177c0ea7 | 165 | |
94870977 | 166 | /* See if TIMER is active or stopped. */ |
4ab1d4be | 167 | for (t = *list, prev = NULL; t && t != timer; prev = t, t = t->next) |
94870977 GM |
168 | ; |
169 | ||
88dbda51 JB |
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. */ | |
94870977 GM |
173 | if (t) |
174 | { | |
175 | if (prev) | |
176 | prev->next = t->next; | |
177 | else | |
178 | *list = t->next; | |
177c0ea7 | 179 | |
94870977 GM |
180 | t->next = free_atimers; |
181 | free_atimers = t; | |
4ab1d4be | 182 | break; |
94870977 | 183 | } |
e12489f9 GM |
184 | } |
185 | ||
1e952f0a | 186 | unblock_atimers (&oldset); |
e12489f9 GM |
187 | } |
188 | ||
189 | ||
3e7d6594 | 190 | /* Append two lists of atimers LIST_1 and LIST_2 and return the |
50b1039f GM |
191 | result list. */ |
192 | ||
193 | static struct atimer * | |
3e7d6594 | 194 | append_atimer_lists (struct atimer *list_1, struct atimer *list_2) |
50b1039f | 195 | { |
3e7d6594 PE |
196 | if (list_1 == NULL) |
197 | return list_2; | |
198 | else if (list_2 == NULL) | |
199 | return list_1; | |
50b1039f GM |
200 | else |
201 | { | |
202 | struct atimer *p; | |
177c0ea7 | 203 | |
3e7d6594 | 204 | for (p = list_1; p->next; p = p->next) |
50b1039f | 205 | ; |
3e7d6594 PE |
206 | p->next = list_2; |
207 | return list_1; | |
50b1039f GM |
208 | } |
209 | } | |
210 | ||
211 | ||
212 | /* Stop all timers except timer T. T null means stop all timers. */ | |
9c2e3e8d GM |
213 | |
214 | void | |
971de7fb | 215 | stop_other_atimers (struct atimer *t) |
9c2e3e8d | 216 | { |
1e952f0a PE |
217 | sigset_t oldset; |
218 | block_atimers (&oldset); | |
177c0ea7 | 219 | |
9c2e3e8d GM |
220 | if (t) |
221 | { | |
2503c8b1 | 222 | struct atimer *p, *prev; |
177c0ea7 | 223 | |
2503c8b1 | 224 | /* See if T is active. */ |
96013ba9 | 225 | for (p = atimers, prev = NULL; p && p != t; prev = p, p = p->next) |
2503c8b1 GM |
226 | ; |
227 | ||
228 | if (p == t) | |
229 | { | |
230 | if (prev) | |
231 | prev->next = t->next; | |
232 | else | |
233 | atimers = t->next; | |
234 | t->next = NULL; | |
235 | } | |
236 | else | |
237 | /* T is not active. Let's handle this like T == 0. */ | |
238 | t = NULL; | |
9c2e3e8d | 239 | } |
177c0ea7 | 240 | |
50b1039f | 241 | stopped_atimers = append_atimer_lists (atimers, stopped_atimers); |
9c2e3e8d | 242 | atimers = t; |
1e952f0a | 243 | unblock_atimers (&oldset); |
9c2e3e8d GM |
244 | } |
245 | ||
246 | ||
247 | /* Run all timers again, if some have been stopped with a call to | |
248 | stop_other_atimers. */ | |
249 | ||
27e498e6 | 250 | void |
971de7fb | 251 | run_all_atimers (void) |
9c2e3e8d GM |
252 | { |
253 | if (stopped_atimers) | |
254 | { | |
255 | struct atimer *t = atimers; | |
50b1039f | 256 | struct atimer *next; |
1e952f0a | 257 | sigset_t oldset; |
177c0ea7 | 258 | |
1e952f0a | 259 | block_atimers (&oldset); |
9c2e3e8d GM |
260 | atimers = stopped_atimers; |
261 | stopped_atimers = NULL; | |
177c0ea7 | 262 | |
50b1039f GM |
263 | while (t) |
264 | { | |
265 | next = t->next; | |
266 | schedule_atimer (t); | |
267 | t = next; | |
268 | } | |
177c0ea7 | 269 | |
1e952f0a | 270 | unblock_atimers (&oldset); |
9c2e3e8d GM |
271 | } |
272 | } | |
273 | ||
274 | ||
e12489f9 GM |
275 | /* Arrange for a SIGALRM to arrive when the next timer is ripe. */ |
276 | ||
277 | static void | |
971de7fb | 278 | set_alarm (void) |
e12489f9 | 279 | { |
e12489f9 GM |
280 | if (atimers) |
281 | { | |
e12489f9 GM |
282 | #ifdef HAVE_SETITIMER |
283 | struct itimerval it; | |
284 | #endif | |
43aac990 | 285 | struct timespec now, interval; |
e12489f9 | 286 | |
2b794d69 | 287 | #ifdef HAVE_ITIMERSPEC |
e26fd2e4 PE |
288 | if (alarm_timer_ok) |
289 | { | |
290 | struct itimerspec ispec; | |
291 | ispec.it_value = atimers->expiration; | |
292 | ispec.it_interval.tv_sec = ispec.it_interval.tv_nsec = 0; | |
293 | if (timer_settime (alarm_timer, 0, &ispec, 0) == 0) | |
294 | return; | |
295 | } | |
296 | #endif | |
e12489f9 | 297 | |
e26fd2e4 PE |
298 | /* Determine interval till the next timer is ripe. |
299 | Don't set the interval to 0; this disables the timer. */ | |
43aac990 PE |
300 | now = current_timespec (); |
301 | interval = (timespec_cmp (atimers->expiration, now) <= 0 | |
302 | ? make_timespec (0, 1000 * 1000) | |
303 | : timespec_sub (atimers->expiration, now)); | |
d35af63c PE |
304 | |
305 | #ifdef HAVE_SETITIMER | |
177c0ea7 | 306 | |
72af86bd | 307 | memset (&it, 0, sizeof it); |
e9a9ae03 | 308 | it.it_value = make_timeval (interval); |
e12489f9 GM |
309 | setitimer (ITIMER_REAL, &it, 0); |
310 | #else /* not HAVE_SETITIMER */ | |
43aac990 | 311 | alarm (max (interval.tv_sec, 1)); |
e12489f9 GM |
312 | #endif /* not HAVE_SETITIMER */ |
313 | } | |
314 | } | |
315 | ||
316 | ||
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 | |
319 | already. */ | |
320 | ||
321 | static void | |
971de7fb | 322 | schedule_atimer (struct atimer *t) |
e12489f9 GM |
323 | { |
324 | struct atimer *a = atimers, *prev = NULL; | |
325 | ||
326 | /* Look for the first atimer that is ripe after T. */ | |
43aac990 | 327 | while (a && timespec_cmp (a->expiration, t->expiration) < 0) |
e12489f9 GM |
328 | prev = a, a = a->next; |
329 | ||
330 | /* Insert T in front of the atimer found, if any. */ | |
331 | if (prev) | |
332 | prev->next = t; | |
333 | else | |
334 | atimers = t; | |
177c0ea7 | 335 | |
e12489f9 GM |
336 | t->next = a; |
337 | } | |
338 | ||
170c80be | 339 | static void |
971de7fb | 340 | run_timers (void) |
e12489f9 | 341 | { |
43aac990 | 342 | struct timespec now = current_timespec (); |
177c0ea7 | 343 | |
43aac990 | 344 | while (atimers && timespec_cmp (atimers->expiration, now) <= 0) |
e12489f9 | 345 | { |
4d7e6e51 | 346 | struct atimer *t = atimers; |
e12489f9 GM |
347 | atimers = atimers->next; |
348 | t->fn (t); | |
177c0ea7 | 349 | |
e12489f9 GM |
350 | if (t->type == ATIMER_CONTINUOUS) |
351 | { | |
43aac990 | 352 | t->expiration = timespec_add (now, t->interval); |
e12489f9 GM |
353 | schedule_atimer (t); |
354 | } | |
355 | else | |
356 | { | |
357 | t->next = free_atimers; | |
358 | free_atimers = t; | |
359 | } | |
e12489f9 | 360 | } |
177c0ea7 | 361 | |
4d7e6e51 | 362 | set_alarm (); |
e12489f9 GM |
363 | } |
364 | ||
365 | ||
170c80be JD |
366 | /* Signal handler for SIGALRM. SIGNO is the signal number, i.e. |
367 | SIGALRM. */ | |
368 | ||
20ef56db PE |
369 | static void |
370 | handle_alarm_signal (int sig) | |
170c80be | 371 | { |
325530de | 372 | pending_signals = 1; |
170c80be JD |
373 | } |
374 | ||
375 | ||
4d7e6e51 | 376 | /* Do pending timers. */ |
e12489f9 GM |
377 | |
378 | void | |
971de7fb | 379 | do_pending_atimers (void) |
e12489f9 | 380 | { |
4d7e6e51 | 381 | if (atimers) |
e12489f9 | 382 | { |
1e952f0a PE |
383 | sigset_t oldset; |
384 | block_atimers (&oldset); | |
170c80be | 385 | run_timers (); |
1e952f0a | 386 | unblock_atimers (&oldset); |
e12489f9 GM |
387 | } |
388 | } | |
389 | ||
390 | ||
391 | /* Turn alarms on/off. This seems to be temporarily necessary on | |
392 | some systems like HPUX (see process.c). */ | |
393 | ||
394 | void | |
b1bb8011 | 395 | turn_on_atimers (bool on) |
e12489f9 GM |
396 | { |
397 | if (on) | |
4d7e6e51 | 398 | set_alarm (); |
e12489f9 GM |
399 | else |
400 | alarm (0); | |
401 | } | |
402 | ||
403 | ||
404 | void | |
971de7fb | 405 | init_atimer (void) |
e12489f9 | 406 | { |
2fe28299 | 407 | struct sigaction action; |
2b794d69 | 408 | #ifdef HAVE_ITIMERSPEC |
e26fd2e4 PE |
409 | struct sigevent sigev; |
410 | sigev.sigev_notify = SIGEV_SIGNAL; | |
411 | sigev.sigev_signo = SIGALRM; | |
412 | sigev.sigev_value.sival_ptr = &alarm_timer; | |
413 | alarm_timer_ok = timer_create (CLOCK_REALTIME, &sigev, &alarm_timer) == 0; | |
414 | #endif | |
bcdf96ba | 415 | free_atimers = stopped_atimers = atimers = NULL; |
325530de | 416 | /* pending_signals is initialized in init_keyboard.*/ |
4d7e6e51 | 417 | emacs_sigaction_init (&action, handle_alarm_signal); |
2fe28299 | 418 | sigaction (SIGALRM, &action, 0); |
e12489f9 | 419 | } |