Skip to content
Newer
Older
100644 426 lines (337 sloc) 9.14 KB
9e7fc2f @railwaycat init emacs 23.2.92
authored
1 /* Asynchronous timers.
6c29c6a @railwaycat emacs-24.4.90 with emacs-24.4.90-mac-5.4
authored
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
9e7fc2f @railwaycat init emacs 23.2.92
authored
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>
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
21
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
22 #include "lisp.h"
23 #include "syssignal.h"
24 #include "systime.h"
25 #include "blockinput.h"
26 #include "atimer.h"
9e7fc2f @railwaycat init emacs 23.2.92
authored
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
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
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
9e7fc2f @railwaycat init emacs 23.2.92
authored
49
50 /* Block/unblock SIGALRM. */
51
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
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 }
9e7fc2f @railwaycat init emacs 23.2.92
authored
70
71 /* Function prototypes. */
72
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
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 *);
9e7fc2f @railwaycat init emacs 23.2.92
authored
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 *
2e5ff49 @railwaycat emacs-24.4 with emacs-24.4-mac-5.0
authored
97 start_atimer (enum atimer_type type, struct timespec timestamp,
98 atimer_callback fn, void *client_data)
9e7fc2f @railwaycat init emacs 23.2.92
authored
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
2e5ff49 @railwaycat emacs-24.4 with emacs-24.4-mac-5.0
authored
105 if (timestamp.tv_nsec != 0 && timestamp.tv_sec < TYPE_MAXIMUM (time_t))
106 timestamp = make_timespec (timestamp.tv_sec + 1, 0);
9e7fc2f @railwaycat init emacs 23.2.92
authored
107 #endif /* not HAVE_SETITIMER */
108
109 /* Get an atimer structure from the free-list, or allocate
110 a new one. */
111 if (free_atimers)
112 {
113 t = free_atimers;
114 free_atimers = t->next;
115 }
116 else
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
117 t = xmalloc (sizeof *t);
9e7fc2f @railwaycat init emacs 23.2.92
authored
118
119 /* Fill the atimer structure. */
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
120 memset (t, 0, sizeof *t);
9e7fc2f @railwaycat init emacs 23.2.92
authored
121 t->type = type;
122 t->fn = fn;
123 t->client_data = client_data;
124
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
125 block_atimers ();
9e7fc2f @railwaycat init emacs 23.2.92
authored
126
127 /* Compute the timer's expiration time. */
128 switch (type)
129 {
130 case ATIMER_ABSOLUTE:
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
131 t->expiration = timestamp;
9e7fc2f @railwaycat init emacs 23.2.92
authored
132 break;
133
134 case ATIMER_RELATIVE:
2e5ff49 @railwaycat emacs-24.4 with emacs-24.4-mac-5.0
authored
135 t->expiration = timespec_add (current_timespec (), timestamp);
9e7fc2f @railwaycat init emacs 23.2.92
authored
136 break;
137
138 case ATIMER_CONTINUOUS:
2e5ff49 @railwaycat emacs-24.4 with emacs-24.4-mac-5.0
authored
139 t->expiration = timespec_add (current_timespec (), timestamp);
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
140 t->interval = timestamp;
9e7fc2f @railwaycat init emacs 23.2.92
authored
141 break;
142 }
143
144 /* Insert the timer in the list of active atimers. */
145 schedule_atimer (t);
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
146 unblock_atimers ();
9e7fc2f @railwaycat init emacs 23.2.92
authored
147
148 /* Arrange for a SIGALRM at the time the next atimer is ripe. */
149 set_alarm ();
150
151 return t;
152 }
153
154
155 /* Cancel and free atimer TIMER. */
156
157 void
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
158 cancel_atimer (struct atimer *timer)
9e7fc2f @railwaycat init emacs 23.2.92
authored
159 {
160 int i;
161
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
162 block_atimers ();
9e7fc2f @railwaycat init emacs 23.2.92
authored
163
164 for (i = 0; i < 2; ++i)
165 {
166 struct atimer *t, *prev;
167 struct atimer **list = i ? &stopped_atimers : &atimers;
168
169 /* See if TIMER is active or stopped. */
170 for (t = *list, prev = NULL; t && t != timer; prev = t, t = t->next)
171 ;
172
173 /* If it is, take it off its list, and put in on the free-list.
174 We don't bother to arrange for setting a different alarm time,
175 since a too early one doesn't hurt. */
176 if (t)
177 {
178 if (prev)
179 prev->next = t->next;
180 else
181 *list = t->next;
182
183 t->next = free_atimers;
184 free_atimers = t;
185 break;
186 }
187 }
188
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
189 unblock_atimers ();
9e7fc2f @railwaycat init emacs 23.2.92
authored
190 }
191
192
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
193 /* Append two lists of atimers LIST_1 and LIST_2 and return the
9e7fc2f @railwaycat init emacs 23.2.92
authored
194 result list. */
195
196 static struct atimer *
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
197 append_atimer_lists (struct atimer *list_1, struct atimer *list_2)
9e7fc2f @railwaycat init emacs 23.2.92
authored
198 {
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
199 if (list_1 == NULL)
200 return list_2;
201 else if (list_2 == NULL)
202 return list_1;
9e7fc2f @railwaycat init emacs 23.2.92
authored
203 else
204 {
205 struct atimer *p;
206
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
207 for (p = list_1; p->next; p = p->next)
9e7fc2f @railwaycat init emacs 23.2.92
authored
208 ;
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
209 p->next = list_2;
210 return list_1;
9e7fc2f @railwaycat init emacs 23.2.92
authored
211 }
212 }
213
214
215 /* Stop all timers except timer T. T null means stop all timers. */
216
217 void
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
218 stop_other_atimers (struct atimer *t)
9e7fc2f @railwaycat init emacs 23.2.92
authored
219 {
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
220 block_atimers ();
9e7fc2f @railwaycat init emacs 23.2.92
authored
221
222 if (t)
223 {
224 struct atimer *p, *prev;
225
226 /* See if T is active. */
227 for (p = atimers, prev = NULL; p && p != t; prev = p, p = p->next)
228 ;
229
230 if (p == t)
231 {
232 if (prev)
233 prev->next = t->next;
234 else
235 atimers = t->next;
236 t->next = NULL;
237 }
238 else
239 /* T is not active. Let's handle this like T == 0. */
240 t = NULL;
241 }
242
243 stopped_atimers = append_atimer_lists (atimers, stopped_atimers);
244 atimers = t;
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
245 unblock_atimers ();
9e7fc2f @railwaycat init emacs 23.2.92
authored
246 }
247
248
249 /* Run all timers again, if some have been stopped with a call to
250 stop_other_atimers. */
251
2e5ff49 @railwaycat emacs-24.4 with emacs-24.4-mac-5.0
authored
252 void
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
253 run_all_atimers (void)
9e7fc2f @railwaycat init emacs 23.2.92
authored
254 {
255 if (stopped_atimers)
256 {
257 struct atimer *t = atimers;
258 struct atimer *next;
259
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
260 block_atimers ();
9e7fc2f @railwaycat init emacs 23.2.92
authored
261 atimers = stopped_atimers;
262 stopped_atimers = NULL;
263
264 while (t)
265 {
266 next = t->next;
267 schedule_atimer (t);
268 t = next;
269 }
270
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
271 unblock_atimers ();
9e7fc2f @railwaycat init emacs 23.2.92
authored
272 }
273 }
274
275
276 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */
277
278 static void
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
279 set_alarm (void)
9e7fc2f @railwaycat init emacs 23.2.92
authored
280 {
281 if (atimers)
282 {
283 #ifdef HAVE_SETITIMER
284 struct itimerval it;
285 #endif
2e5ff49 @railwaycat emacs-24.4 with emacs-24.4-mac-5.0
authored
286 struct timespec now, interval;
9e7fc2f @railwaycat init emacs 23.2.92
authored
287
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
288 #ifdef HAVE_ITIMERSPEC
289 if (alarm_timer_ok)
9e7fc2f @railwaycat init emacs 23.2.92
authored
290 {
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
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;
9e7fc2f @railwaycat init emacs 23.2.92
authored
296 }
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
297 #endif
298
299 /* Determine interval till the next timer is ripe.
300 Don't set the interval to 0; this disables the timer. */
2e5ff49 @railwaycat emacs-24.4 with emacs-24.4-mac-5.0
authored
301 now = current_timespec ();
302 interval = (timespec_cmp (atimers->expiration, now) <= 0
303 ? make_timespec (0, 1000 * 1000)
304 : timespec_sub (atimers->expiration, now));
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
305
306 #ifdef HAVE_SETITIMER
9e7fc2f @railwaycat init emacs 23.2.92
authored
307
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
308 memset (&it, 0, sizeof it);
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
309 it.it_value = make_timeval (interval);
9e7fc2f @railwaycat init emacs 23.2.92
authored
310 setitimer (ITIMER_REAL, &it, 0);
311 #else /* not HAVE_SETITIMER */
2e5ff49 @railwaycat emacs-24.4 with emacs-24.4-mac-5.0
authored
312 alarm (max (interval.tv_sec, 1));
9e7fc2f @railwaycat init emacs 23.2.92
authored
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
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
323 schedule_atimer (struct atimer *t)
9e7fc2f @railwaycat init emacs 23.2.92
authored
324 {
325 struct atimer *a = atimers, *prev = NULL;
326
327 /* Look for the first atimer that is ripe after T. */
2e5ff49 @railwaycat emacs-24.4 with emacs-24.4-mac-5.0
authored
328 while (a && timespec_cmp (a->expiration, t->expiration) < 0)
9e7fc2f @railwaycat init emacs 23.2.92
authored
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;
336
337 t->next = a;
338 }
339
340 static void
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
341 run_timers (void)
9e7fc2f @railwaycat init emacs 23.2.92
authored
342 {
2e5ff49 @railwaycat emacs-24.4 with emacs-24.4-mac-5.0
authored
343 struct timespec now = current_timespec ();
9e7fc2f @railwaycat init emacs 23.2.92
authored
344
2e5ff49 @railwaycat emacs-24.4 with emacs-24.4-mac-5.0
authored
345 while (atimers && timespec_cmp (atimers->expiration, now) <= 0)
9e7fc2f @railwaycat init emacs 23.2.92
authored
346 {
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
347 struct atimer *t = atimers;
9e7fc2f @railwaycat init emacs 23.2.92
authored
348 atimers = atimers->next;
d75115f @railwaycat merge mac port patch
authored
349 #ifndef DARWIN_OS
9e7fc2f @railwaycat init emacs 23.2.92
authored
350 t->fn (t);
d75115f @railwaycat merge mac port patch
authored
351 #endif
9e7fc2f @railwaycat init emacs 23.2.92
authored
352
353 if (t->type == ATIMER_CONTINUOUS)
354 {
2e5ff49 @railwaycat emacs-24.4 with emacs-24.4-mac-5.0
authored
355 t->expiration = timespec_add (now, t->interval);
9e7fc2f @railwaycat init emacs 23.2.92
authored
356 schedule_atimer (t);
357 }
358 else
359 {
360 t->next = free_atimers;
361 free_atimers = t;
362 }
d75115f @railwaycat merge mac port patch
authored
363 #ifdef DARWIN_OS
364 /* Fix for Ctrl-G. Perhaps this should apply to all platforms. */
365 t->fn (t);
366 #endif
9e7fc2f @railwaycat init emacs 23.2.92
authored
367 }
368
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
369 set_alarm ();
9e7fc2f @railwaycat init emacs 23.2.92
authored
370 }
371
372
373 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e.
374 SIGALRM. */
375
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
376 static void
377 handle_alarm_signal (int sig)
9e7fc2f @railwaycat init emacs 23.2.92
authored
378 {
379 pending_signals = 1;
380 }
381
382
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
383 /* Do pending timers. */
9e7fc2f @railwaycat init emacs 23.2.92
authored
384
385 void
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
386 do_pending_atimers (void)
9e7fc2f @railwaycat init emacs 23.2.92
authored
387 {
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
388 if (atimers)
9e7fc2f @railwaycat init emacs 23.2.92
authored
389 {
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
390 block_atimers ();
9e7fc2f @railwaycat init emacs 23.2.92
authored
391 run_timers ();
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
392 unblock_atimers ();
9e7fc2f @railwaycat init emacs 23.2.92
authored
393 }
394 }
395
396
397 /* Turn alarms on/off. This seems to be temporarily necessary on
398 some systems like HPUX (see process.c). */
399
400 void
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
401 turn_on_atimers (bool on)
9e7fc2f @railwaycat init emacs 23.2.92
authored
402 {
403 if (on)
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
404 set_alarm ();
9e7fc2f @railwaycat init emacs 23.2.92
authored
405 else
406 alarm (0);
407 }
408
409
410 void
479bbfd @railwaycat update to emacs 24.0.94 with mac 2.90
authored
411 init_atimer (void)
9e7fc2f @railwaycat init emacs 23.2.92
authored
412 {
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
413 struct sigaction action;
414 #ifdef HAVE_ITIMERSPEC
415 struct sigevent sigev;
416 sigev.sigev_notify = SIGEV_SIGNAL;
417 sigev.sigev_signo = SIGALRM;
418 sigev.sigev_value.sival_ptr = &alarm_timer;
419 alarm_timer_ok = timer_create (CLOCK_REALTIME, &sigev, &alarm_timer) == 0;
420 #endif
9e7fc2f @railwaycat init emacs 23.2.92
authored
421 free_atimers = stopped_atimers = atimers = NULL;
422 /* pending_signals is initialized in init_keyboard.*/
70089e3 @railwaycat Emacs 24.2.90 with emacs-24.2.90-mac-3.90
authored
423 emacs_sigaction_init (&action, handle_alarm_signal);
424 sigaction (SIGALRM, &action, 0);
9e7fc2f @railwaycat init emacs 23.2.92
authored
425 }
Something went wrong with that request. Please try again.