| 1 | /* Asynchronous timers. |
| 2 | Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, |
| 3 | 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GNU Emacs. |
| 6 | |
| 7 | GNU Emacs is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | GNU Emacs is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <config.h> |
| 21 | #include <signal.h> |
| 22 | #include <stdio.h> |
| 23 | #include <setjmp.h> |
| 24 | #include <lisp.h> |
| 25 | #include <syssignal.h> |
| 26 | #include <systime.h> |
| 27 | #include <blockinput.h> |
| 28 | #include <atimer.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 | /* Free-list of atimer structures. */ |
| 39 | |
| 40 | static struct atimer *free_atimers; |
| 41 | |
| 42 | /* List of currently not running timers due to a call to |
| 43 | lock_atimer. */ |
| 44 | |
| 45 | static struct atimer *stopped_atimers; |
| 46 | |
| 47 | /* List of active atimers, sorted by expiration time. The timer that |
| 48 | will become ripe next is always at the front of this list. */ |
| 49 | |
| 50 | static struct atimer *atimers; |
| 51 | |
| 52 | /* Non-zero means alarm_signal_handler has found ripe timers but |
| 53 | interrupt_input_blocked was non-zero. In this case, timer |
| 54 | functions are not called until the next UNBLOCK_INPUT because timer |
| 55 | functions are expected to call X, and X cannot be assumed to be |
| 56 | reentrant. */ |
| 57 | |
| 58 | int pending_atimers; |
| 59 | |
| 60 | /* Block/unblock SIGALRM. */ |
| 61 | |
| 62 | #define BLOCK_ATIMERS sigblock (sigmask (SIGALRM)) |
| 63 | #define UNBLOCK_ATIMERS sigunblock (sigmask (SIGALRM)) |
| 64 | |
| 65 | /* Function prototypes. */ |
| 66 | |
| 67 | static void set_alarm (void); |
| 68 | static void schedule_atimer (struct atimer *); |
| 69 | static struct atimer *append_atimer_lists (struct atimer *, |
| 70 | struct atimer *); |
| 71 | SIGTYPE alarm_signal_handler (int signo); |
| 72 | |
| 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 * |
| 93 | start_atimer (enum atimer_type type, struct timeval time, atimer_callback fn, void *client_data) |
| 94 | { |
| 95 | struct atimer *t; |
| 96 | |
| 97 | /* Round TIME up to the next full second if we don't have |
| 98 | itimers. */ |
| 99 | #ifndef HAVE_SETITIMER |
| 100 | if (EMACS_USECS (time) != 0) |
| 101 | { |
| 102 | EMACS_SET_USECS (time, 0); |
| 103 | EMACS_SET_SECS (time, EMACS_SECS (time) + 1); |
| 104 | } |
| 105 | #endif /* not HAVE_SETITIMER */ |
| 106 | |
| 107 | /* Get an atimer structure from the free-list, or allocate |
| 108 | a new one. */ |
| 109 | if (free_atimers) |
| 110 | { |
| 111 | t = free_atimers; |
| 112 | free_atimers = t->next; |
| 113 | } |
| 114 | else |
| 115 | t = (struct atimer *) xmalloc (sizeof *t); |
| 116 | |
| 117 | /* Fill the atimer structure. */ |
| 118 | bzero (t, sizeof *t); |
| 119 | t->type = type; |
| 120 | t->fn = fn; |
| 121 | t->client_data = client_data; |
| 122 | |
| 123 | BLOCK_ATIMERS; |
| 124 | |
| 125 | /* Compute the timer's expiration time. */ |
| 126 | switch (type) |
| 127 | { |
| 128 | case ATIMER_ABSOLUTE: |
| 129 | t->expiration = time; |
| 130 | break; |
| 131 | |
| 132 | case ATIMER_RELATIVE: |
| 133 | EMACS_GET_TIME (t->expiration); |
| 134 | EMACS_ADD_TIME (t->expiration, t->expiration, time); |
| 135 | break; |
| 136 | |
| 137 | case ATIMER_CONTINUOUS: |
| 138 | EMACS_GET_TIME (t->expiration); |
| 139 | EMACS_ADD_TIME (t->expiration, t->expiration, time); |
| 140 | t->interval = time; |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | /* Insert the timer in the list of active atimers. */ |
| 145 | schedule_atimer (t); |
| 146 | UNBLOCK_ATIMERS; |
| 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 |
| 158 | cancel_atimer (struct atimer *timer) |
| 159 | { |
| 160 | int i; |
| 161 | |
| 162 | BLOCK_ATIMERS; |
| 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 the its list, and put in on the |
| 174 | free-list. We don't bother to arrange for setting a |
| 175 | different alarm time, 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 | |
| 189 | UNBLOCK_ATIMERS; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /* Append two lists of atimers LIST1 and LIST2 and return the |
| 194 | result list. */ |
| 195 | |
| 196 | static struct atimer * |
| 197 | append_atimer_lists (struct atimer *list1, struct atimer *list2) |
| 198 | { |
| 199 | if (list1 == NULL) |
| 200 | return list2; |
| 201 | else if (list2 == NULL) |
| 202 | return list1; |
| 203 | else |
| 204 | { |
| 205 | struct atimer *p; |
| 206 | |
| 207 | for (p = list1; p->next; p = p->next) |
| 208 | ; |
| 209 | p->next = list2; |
| 210 | return list1; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /* Stop all timers except timer T. T null means stop all timers. */ |
| 216 | |
| 217 | void |
| 218 | stop_other_atimers (struct atimer *t) |
| 219 | { |
| 220 | BLOCK_ATIMERS; |
| 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; |
| 245 | UNBLOCK_ATIMERS; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | /* Run all timers again, if some have been stopped with a call to |
| 250 | stop_other_atimers. */ |
| 251 | |
| 252 | void |
| 253 | run_all_atimers (void) |
| 254 | { |
| 255 | if (stopped_atimers) |
| 256 | { |
| 257 | struct atimer *t = atimers; |
| 258 | struct atimer *next; |
| 259 | |
| 260 | BLOCK_ATIMERS; |
| 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 | |
| 271 | UNBLOCK_ATIMERS; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | |
| 276 | /* A version of run_all_timers suitable for a record_unwind_protect. */ |
| 277 | |
| 278 | Lisp_Object |
| 279 | unwind_stop_other_atimers (Lisp_Object dummy) |
| 280 | { |
| 281 | run_all_atimers (); |
| 282 | return Qnil; |
| 283 | } |
| 284 | |
| 285 | |
| 286 | /* Arrange for a SIGALRM to arrive when the next timer is ripe. */ |
| 287 | |
| 288 | static void |
| 289 | set_alarm (void) |
| 290 | { |
| 291 | if (atimers) |
| 292 | { |
| 293 | EMACS_TIME now, time; |
| 294 | #ifdef HAVE_SETITIMER |
| 295 | struct itimerval it; |
| 296 | #endif |
| 297 | |
| 298 | /* Determine s/us till the next timer is ripe. */ |
| 299 | EMACS_GET_TIME (now); |
| 300 | EMACS_SUB_TIME (time, atimers->expiration, now); |
| 301 | |
| 302 | #ifdef HAVE_SETITIMER |
| 303 | /* Don't set the interval to 0; this disables the timer. */ |
| 304 | if (EMACS_TIME_LE (atimers->expiration, now)) |
| 305 | { |
| 306 | EMACS_SET_SECS (time, 0); |
| 307 | EMACS_SET_USECS (time, 1000); |
| 308 | } |
| 309 | |
| 310 | bzero (&it, sizeof it); |
| 311 | it.it_value = time; |
| 312 | setitimer (ITIMER_REAL, &it, 0); |
| 313 | #else /* not HAVE_SETITIMER */ |
| 314 | alarm (max (EMACS_SECS (time), 1)); |
| 315 | #endif /* not HAVE_SETITIMER */ |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | |
| 320 | /* Insert timer T into the list of active atimers `atimers', keeping |
| 321 | the list sorted by expiration time. T must not be in this list |
| 322 | already. */ |
| 323 | |
| 324 | static void |
| 325 | schedule_atimer (struct atimer *t) |
| 326 | { |
| 327 | struct atimer *a = atimers, *prev = NULL; |
| 328 | |
| 329 | /* Look for the first atimer that is ripe after T. */ |
| 330 | while (a && EMACS_TIME_GT (t->expiration, a->expiration)) |
| 331 | prev = a, a = a->next; |
| 332 | |
| 333 | /* Insert T in front of the atimer found, if any. */ |
| 334 | if (prev) |
| 335 | prev->next = t; |
| 336 | else |
| 337 | atimers = t; |
| 338 | |
| 339 | t->next = a; |
| 340 | } |
| 341 | |
| 342 | static void |
| 343 | run_timers (void) |
| 344 | { |
| 345 | EMACS_TIME now; |
| 346 | |
| 347 | EMACS_GET_TIME (now); |
| 348 | |
| 349 | while (atimers |
| 350 | && (pending_atimers = interrupt_input_blocked) == 0 |
| 351 | && EMACS_TIME_LE (atimers->expiration, now)) |
| 352 | { |
| 353 | struct atimer *t; |
| 354 | |
| 355 | t = atimers; |
| 356 | atimers = atimers->next; |
| 357 | t->fn (t); |
| 358 | |
| 359 | if (t->type == ATIMER_CONTINUOUS) |
| 360 | { |
| 361 | EMACS_ADD_TIME (t->expiration, now, t->interval); |
| 362 | schedule_atimer (t); |
| 363 | } |
| 364 | else |
| 365 | { |
| 366 | t->next = free_atimers; |
| 367 | free_atimers = t; |
| 368 | } |
| 369 | |
| 370 | EMACS_GET_TIME (now); |
| 371 | } |
| 372 | |
| 373 | if (! atimers) |
| 374 | pending_atimers = 0; |
| 375 | |
| 376 | #ifdef SYNC_INPUT |
| 377 | if (pending_atimers) |
| 378 | pending_signals = 1; |
| 379 | else |
| 380 | { |
| 381 | pending_signals = interrupt_input_pending; |
| 382 | set_alarm (); |
| 383 | } |
| 384 | #else |
| 385 | if (! pending_atimers) |
| 386 | set_alarm (); |
| 387 | #endif |
| 388 | } |
| 389 | |
| 390 | |
| 391 | /* Signal handler for SIGALRM. SIGNO is the signal number, i.e. |
| 392 | SIGALRM. */ |
| 393 | |
| 394 | SIGTYPE |
| 395 | alarm_signal_handler (int signo) |
| 396 | { |
| 397 | #ifndef SYNC_INPUT |
| 398 | SIGNAL_THREAD_CHECK (signo); |
| 399 | #endif |
| 400 | |
| 401 | pending_atimers = 1; |
| 402 | #ifdef SYNC_INPUT |
| 403 | pending_signals = 1; |
| 404 | #else |
| 405 | run_timers (); |
| 406 | #endif |
| 407 | } |
| 408 | |
| 409 | |
| 410 | /* Call alarm_signal_handler for pending timers. */ |
| 411 | |
| 412 | void |
| 413 | do_pending_atimers (void) |
| 414 | { |
| 415 | if (pending_atimers) |
| 416 | { |
| 417 | BLOCK_ATIMERS; |
| 418 | run_timers (); |
| 419 | UNBLOCK_ATIMERS; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | |
| 424 | /* Turn alarms on/off. This seems to be temporarily necessary on |
| 425 | some systems like HPUX (see process.c). */ |
| 426 | |
| 427 | void |
| 428 | turn_on_atimers (int on) |
| 429 | { |
| 430 | if (on) |
| 431 | { |
| 432 | signal (SIGALRM, alarm_signal_handler); |
| 433 | set_alarm (); |
| 434 | } |
| 435 | else |
| 436 | alarm (0); |
| 437 | } |
| 438 | |
| 439 | |
| 440 | void |
| 441 | init_atimer (void) |
| 442 | { |
| 443 | free_atimers = stopped_atimers = atimers = NULL; |
| 444 | pending_atimers = 0; |
| 445 | /* pending_signals is initialized in init_keyboard.*/ |
| 446 | signal (SIGALRM, alarm_signal_handler); |
| 447 | } |
| 448 | |
| 449 | /* arch-tag: e6308261-eec6-404b-89fb-6e5909518d70 |
| 450 | (do not change this comment) */ |