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