Mercurial > emacs
annotate src/atimer.c @ 27670:cf2edc15eaa9
(stopped_atimers): New variable.
(stop_other_atimers, run_all_atimers, unwind_stop_other_atimers):
New functions.
| author | Gerd Moellmann <gerd@gnu.org> |
|---|---|
| date | Sat, 12 Feb 2000 13:13:13 +0000 |
| parents | 7580a16f676c |
| children | 5c49b0be3b7b |
| rev | line source |
|---|---|
| 27433 | 1 /* Asynchronous timers. |
| 2 Copyright (C) 2000 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 2, or (at your option) | |
| 9 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; see the file COPYING. If not, write to | |
| 18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
| 19 Boston, MA 02111-1307, USA. */ | |
| 20 | |
| 21 #include <config.h> | |
| 22 #include <lisp.h> | |
| 23 #include <signal.h> | |
| 24 #include <syssignal.h> | |
| 25 #include <systime.h> | |
| 26 #include <blockinput.h> | |
| 27 #include <atimer.h> | |
| 28 #include <stdio.h> | |
| 29 | |
| 30 #ifdef HAVE_UNISTD_H | |
| 31 #include <unistd.h> | |
| 32 #endif | |
| 33 | |
| 34 #ifdef HAVE_SYS_TIME_H | |
| 35 #include <sys/time.h> | |
| 36 #endif | |
| 37 | |
| 38 /* The ubiquitous min/max macros. */ | |
| 39 | |
| 40 #define max(X, Y) ((X) > (Y) ? (X) : (Y)) | |
| 41 #define min(X, Y) ((X) < (Y) ? (X) : (Y)) | |
| 42 | |
| 43 /* Free-list of atimer structures. */ | |
| 44 | |
| 45 static struct atimer *free_atimers; | |
| 46 | |
|
27670
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
47 /* List of currently not running timers due to a call to |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
48 lock_atimer. */ |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
49 |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
50 static struct atimer *stopped_atimers; |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
51 |
| 27433 | 52 /* List of active atimers, sorted by expiration time. The timer that |
| 53 will become ripe next is always at the front of this list. */ | |
| 54 | |
| 55 static struct atimer *atimers; | |
| 56 | |
| 57 /* Non-zero means alarm_signal_handler has found ripe timers but | |
| 58 interrupt_input_blocked was non-zero. In this case, timer | |
| 59 functions are not called until the next UNBLOCK_INPUT because timer | |
| 60 functions are expected to call X, and X cannot be assumed to be | |
| 61 reentrant. */ | |
| 62 | |
| 63 int pending_atimers; | |
| 64 | |
| 65 /* Block/unblock SIGALRM.. */ | |
| 66 | |
| 67 #define BLOCK_ATIMERS sigblock (sigmask (SIGALRM)) | |
| 68 #define UNBLOCK_ATIMERS sigunblock (sigmask (SIGALRM)) | |
| 69 | |
| 70 /* Function prototypes. */ | |
| 71 | |
| 72 static void set_alarm P_ ((void)); | |
| 73 static void schedule_atimer P_ ((struct atimer *)); | |
| 74 | |
| 75 | |
| 76 /* Start a new atimer of type TYPE. TIME specifies when the timer is | |
| 77 ripe. FN is the function to call when the timer fires. | |
| 78 CLIENT_DATA is stored in the client_data member of the atimer | |
| 79 structure returned and so made available to FN when it is called. | |
| 80 | |
| 81 If TYPE is ATIMER_ABSOLUTE, TIME is the absolute time at which the | |
| 82 timer fires. | |
| 83 | |
| 84 If TYPE is ATIMER_RELATIVE, the timer is ripe TIME s/us in the | |
| 85 future. | |
| 86 | |
| 87 In both cases, the timer is automatically freed after it has fired. | |
| 88 | |
| 89 If TYPE is ATIMER_CONTINUOUS, the timer fires every TIME s/us. | |
| 90 | |
| 91 Value is a pointer to the atimer started. It can be used in calls | |
| 92 to cancel_atimer; don't free it yourself. */ | |
| 93 | |
| 94 struct atimer * | |
| 95 start_atimer (type, time, fn, client_data) | |
| 96 enum atimer_type type; | |
| 97 EMACS_TIME time; | |
| 98 atimer_callback fn; | |
| 99 void *client_data; | |
| 100 { | |
| 101 struct atimer *t; | |
| 102 | |
|
27670
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
103 /* May not be called when some timers are stopped. */ |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
104 if (stopped_atimers) |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
105 abort (); |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
106 |
| 27433 | 107 /* Round TIME up to the next full second if we don't have |
| 108 itimers. */ | |
| 109 #ifndef HAVE_SETITIMER | |
| 110 if (EMACS_USECS (time) != 0) | |
| 111 { | |
|
27452
7580a16f676c
(start_atimer) [!HAVE_SETITIMER]: Use EMACS_SET_SECS
Eli Zaretskii <eliz@gnu.org>
parents:
27433
diff
changeset
|
112 EMACS_SET_USECS (time, 0); |
|
7580a16f676c
(start_atimer) [!HAVE_SETITIMER]: Use EMACS_SET_SECS
Eli Zaretskii <eliz@gnu.org>
parents:
27433
diff
changeset
|
113 EMACS_SET_SECS (time, EMACS_SECS (time) + 1); |
| 27433 | 114 } |
| 115 #endif /* not HAVE_SETITIMER */ | |
| 116 | |
| 117 /* Get an atimer structure from the free-list, or allocate | |
| 118 a new one. */ | |
| 119 if (free_atimers) | |
| 120 { | |
| 121 t = free_atimers; | |
| 122 free_atimers = t->next; | |
| 123 } | |
| 124 else | |
| 125 t = (struct atimer *) xmalloc (sizeof *t); | |
| 126 | |
| 127 /* Fill the atimer structure. */ | |
| 128 bzero (t, sizeof *t); | |
| 129 t->type = type; | |
| 130 t->fn = fn; | |
| 131 t->client_data = client_data; | |
| 132 | |
| 133 BLOCK_ATIMERS; | |
| 134 | |
| 135 /* Compute the timer's expiration time. */ | |
| 136 switch (type) | |
| 137 { | |
| 138 case ATIMER_ABSOLUTE: | |
| 139 t->expiration = time; | |
| 140 break; | |
| 141 | |
| 142 case ATIMER_RELATIVE: | |
| 143 EMACS_GET_TIME (t->expiration); | |
| 144 EMACS_ADD_TIME (t->expiration, t->expiration, time); | |
| 145 break; | |
| 146 | |
| 147 case ATIMER_CONTINUOUS: | |
| 148 EMACS_GET_TIME (t->expiration); | |
| 149 EMACS_ADD_TIME (t->expiration, t->expiration, time); | |
| 150 t->interval = time; | |
| 151 break; | |
| 152 } | |
| 153 | |
| 154 /* Insert the timer in the list of active atimers. */ | |
| 155 schedule_atimer (t); | |
| 156 UNBLOCK_ATIMERS; | |
| 157 | |
| 158 /* Arrange for a SIGALRM at the time the next atimer is ripe. */ | |
| 159 set_alarm (); | |
| 160 | |
| 161 return t; | |
| 162 } | |
| 163 | |
| 164 | |
| 165 /* Cancel and free atimer TIMER. */ | |
| 166 | |
| 167 void | |
| 168 cancel_atimer (timer) | |
| 169 struct atimer *timer; | |
| 170 { | |
| 171 struct atimer *t, *prev; | |
| 172 | |
|
27670
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
173 /* May not be called when some timers are stopped. */ |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
174 if (stopped_atimers) |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
175 abort (); |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
176 |
| 27433 | 177 BLOCK_ATIMERS; |
| 178 | |
| 179 /* See if TIMER is active. */ | |
| 180 for (t = atimers, prev = 0; t && t != timer; t = t->next) | |
| 181 ; | |
| 182 | |
| 183 /* If it is, take it off the list of active timers, put in on the | |
| 184 free-list. We don't bother to arrange for setting a different | |
| 185 alarm time, since a too early one doesn't hurt. */ | |
| 186 if (t) | |
| 187 { | |
| 188 if (prev) | |
| 189 prev->next = t->next; | |
| 190 else | |
| 191 atimers = t->next; | |
| 192 | |
| 193 t->next = free_atimers; | |
| 194 free_atimers = t; | |
| 195 } | |
| 196 | |
| 197 UNBLOCK_ATIMERS; | |
| 198 } | |
| 199 | |
| 200 | |
|
27670
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
201 /* Stop all timers except timer T. T null means stop all timers. |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
202 This function may only be called when all timers are running. Two |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
203 calls of this function in a row will lead to an abort. You may not |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
204 call cancel_atimer or start_atimer while timers are stopped. */ |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
205 |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
206 void |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
207 stop_other_atimers (t) |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
208 struct atimer *t; |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
209 { |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
210 BLOCK_ATIMERS; |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
211 |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
212 if (stopped_atimers) |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
213 abort (); |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
214 |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
215 if (t) |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
216 { |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
217 cancel_atimer (t); |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
218 if (free_atimers != t) |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
219 abort (); |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
220 free_atimers = free_atimers->next; |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
221 t->next = NULL; |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
222 } |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
223 |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
224 stopped_atimers = atimers; |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
225 atimers = t; |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
226 UNBLOCK_ATIMERS; |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
227 } |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
228 |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
229 |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
230 /* Run all timers again, if some have been stopped with a call to |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
231 stop_other_atimers. */ |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
232 |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
233 void |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
234 run_all_atimers () |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
235 { |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
236 if (stopped_atimers) |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
237 { |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
238 struct atimer *t = atimers; |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
239 BLOCK_ATIMERS; |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
240 atimers = stopped_atimers; |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
241 stopped_atimers = NULL; |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
242 if (t) |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
243 schedule_atimer (t); |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
244 UNBLOCK_ATIMERS; |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
245 } |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
246 } |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
247 |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
248 |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
249 /* A version of run_all_timers suitable for a record_unwind_protect. */ |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
250 |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
251 Lisp_Object |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
252 unwind_stop_other_atimers (dummy) |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
253 Lisp_Object dummy; |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
254 { |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
255 run_all_atimers (); |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
256 return Qnil; |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
257 } |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
258 |
|
cf2edc15eaa9
(stopped_atimers): New variable.
Gerd Moellmann <gerd@gnu.org>
parents:
27452
diff
changeset
|
259 |
| 27433 | 260 /* Arrange for a SIGALRM to arrive when the next timer is ripe. */ |
| 261 | |
| 262 static void | |
| 263 set_alarm () | |
| 264 { | |
| 265 #if defined (USG) && !defined (POSIX_SIGNALS) | |
| 266 /* USG systems forget handlers when they are used; | |
| 267 must reestablish each time. */ | |
| 268 signal (SIGALRM, alarm_signal_handler); | |
| 269 #endif /* USG */ | |
| 270 | |
| 271 if (atimers) | |
| 272 { | |
| 273 EMACS_TIME now, time; | |
| 274 #ifdef HAVE_SETITIMER | |
| 275 struct itimerval it; | |
| 276 #endif | |
| 277 | |
| 278 /* Determine s/us till the next timer is ripe. */ | |
| 279 EMACS_GET_TIME (now); | |
| 280 EMACS_SUB_TIME (time, atimers->expiration, now); | |
| 281 | |
| 282 #ifdef HAVE_SETITIMER | |
| 283 /* Don't set the interval to 0; this disables the timer. */ | |
| 284 if (EMACS_TIME_LE (atimers->expiration, now)) | |
| 285 { | |
| 286 EMACS_SET_SECS (time, 0); | |
| 287 EMACS_SET_USECS (time, 1000); | |
| 288 } | |
| 289 | |
| 290 bzero (&it, sizeof it); | |
| 291 it.it_value = time; | |
| 292 setitimer (ITIMER_REAL, &it, 0); | |
| 293 #else /* not HAVE_SETITIMER */ | |
| 294 alarm (max (EMACS_SECS (time), 1)); | |
| 295 #endif /* not HAVE_SETITIMER */ | |
| 296 } | |
| 297 } | |
| 298 | |
| 299 | |
| 300 /* Insert timer T into the list of active atimers `atimers', keeping | |
| 301 the list sorted by expiration time. T must not be in this list | |
| 302 already. */ | |
| 303 | |
| 304 static void | |
| 305 schedule_atimer (t) | |
| 306 struct atimer *t; | |
| 307 { | |
| 308 struct atimer *a = atimers, *prev = NULL; | |
| 309 | |
| 310 /* Look for the first atimer that is ripe after T. */ | |
| 311 while (a && EMACS_TIME_GT (t->expiration, a->expiration)) | |
| 312 prev = a, a = a->next; | |
| 313 | |
| 314 /* Insert T in front of the atimer found, if any. */ | |
| 315 if (prev) | |
| 316 prev->next = t; | |
| 317 else | |
| 318 atimers = t; | |
| 319 | |
| 320 t->next = a; | |
| 321 } | |
| 322 | |
| 323 | |
| 324 /* Signal handler for SIGALRM. SIGNO is the signal number, i.e. | |
| 325 SIGALRM. */ | |
| 326 | |
| 327 SIGTYPE | |
| 328 alarm_signal_handler (signo) | |
| 329 int signo; | |
| 330 { | |
| 331 EMACS_TIME now; | |
| 332 | |
| 333 EMACS_GET_TIME (now); | |
| 334 pending_atimers = 0; | |
| 335 | |
| 336 while (atimers | |
| 337 && (pending_atimers = interrupt_input_blocked) == 0 | |
| 338 && EMACS_TIME_LE (atimers->expiration, now)) | |
| 339 { | |
| 340 struct atimer *t; | |
| 341 | |
| 342 t = atimers; | |
| 343 atimers = atimers->next; | |
| 344 t->fn (t); | |
| 345 | |
| 346 if (t->type == ATIMER_CONTINUOUS) | |
| 347 { | |
| 348 EMACS_ADD_TIME (t->expiration, now, t->interval); | |
| 349 schedule_atimer (t); | |
| 350 } | |
| 351 else | |
| 352 { | |
| 353 t->next = free_atimers; | |
| 354 free_atimers = t; | |
| 355 } | |
| 356 | |
| 357 EMACS_GET_TIME (now); | |
| 358 } | |
| 359 | |
| 360 #if defined (USG) && !defined (POSIX_SIGNALS) | |
| 361 /* USG systems forget handlers when they are used; | |
| 362 must reestablish each time. */ | |
| 363 signal (SIGALRM, alarm_signal_handler); | |
| 364 #endif /* USG */ | |
| 365 | |
| 366 set_alarm (); | |
| 367 } | |
| 368 | |
| 369 | |
| 370 /* Call alarm_signal_handler for pending timers. */ | |
| 371 | |
| 372 void | |
| 373 do_pending_atimers () | |
| 374 { | |
| 375 if (pending_atimers) | |
| 376 { | |
| 377 BLOCK_ATIMERS; | |
| 378 alarm_signal_handler (SIGALRM); | |
| 379 UNBLOCK_ATIMERS; | |
| 380 } | |
| 381 } | |
| 382 | |
| 383 | |
| 384 /* Turn alarms on/off. This seems to be temporarily necessary on | |
| 385 some systems like HPUX (see process.c). */ | |
| 386 | |
| 387 void | |
| 388 turn_on_atimers (on) | |
| 389 int on; | |
| 390 { | |
| 391 if (on) | |
| 392 { | |
| 393 signal (SIGALRM, alarm_signal_handler); | |
| 394 set_alarm (); | |
| 395 } | |
| 396 else | |
| 397 alarm (0); | |
| 398 } | |
| 399 | |
| 400 | |
| 401 void | |
| 402 init_atimer () | |
| 403 { | |
| 404 free_atimers = atimers = NULL; | |
| 405 pending_atimers = 0; | |
| 406 signal (SIGALRM, alarm_signal_handler); | |
| 407 } |
