diff options
Diffstat (limited to 'src/atimer.c')
| -rw-r--r-- | src/atimer.c | 80 |
1 files changed, 45 insertions, 35 deletions
diff --git a/src/atimer.c b/src/atimer.c index eb3136ae55d..5dbd807872a 100644 --- a/src/atimer.c +++ b/src/atimer.c | |||
| @@ -17,9 +17,8 @@ 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/>. */ | 17 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
| 18 | 18 | ||
| 19 | #include <config.h> | 19 | #include <config.h> |
| 20 | #include <signal.h> | ||
| 21 | #include <stdio.h> | 20 | #include <stdio.h> |
| 22 | #include <setjmp.h> | 21 | |
| 23 | #include "lisp.h" | 22 | #include "lisp.h" |
| 24 | #include "syssignal.h" | 23 | #include "syssignal.h" |
| 25 | #include "systime.h" | 24 | #include "systime.h" |
| @@ -41,7 +40,7 @@ static struct atimer *stopped_atimers; | |||
| 41 | 40 | ||
| 42 | static struct atimer *atimers; | 41 | static struct atimer *atimers; |
| 43 | 42 | ||
| 44 | /* Non-zero means alarm_signal_handler has found ripe timers but | 43 | /* Non-zero means alarm signal handler has found ripe timers but |
| 45 | interrupt_input_blocked was non-zero. In this case, timer | 44 | interrupt_input_blocked was non-zero. In this case, timer |
| 46 | functions are not called until the next UNBLOCK_INPUT because timer | 45 | functions are not called until the next UNBLOCK_INPUT because timer |
| 47 | functions are expected to call X, and X cannot be assumed to be | 46 | functions are expected to call X, and X cannot be assumed to be |
| @@ -51,8 +50,24 @@ int pending_atimers; | |||
| 51 | 50 | ||
| 52 | /* Block/unblock SIGALRM. */ | 51 | /* Block/unblock SIGALRM. */ |
| 53 | 52 | ||
| 54 | #define BLOCK_ATIMERS sigblock (sigmask (SIGALRM)) | 53 | static void |
| 55 | #define UNBLOCK_ATIMERS sigunblock (sigmask (SIGALRM)) | 54 | sigmask_atimers (int how) |
| 55 | { | ||
| 56 | sigset_t blocked; | ||
| 57 | sigemptyset (&blocked); | ||
| 58 | sigaddset (&blocked, SIGALRM); | ||
| 59 | pthread_sigmask (how, &blocked, 0); | ||
| 60 | } | ||
| 61 | static void | ||
| 62 | block_atimers (void) | ||
| 63 | { | ||
| 64 | sigmask_atimers (SIG_BLOCK); | ||
| 65 | } | ||
| 66 | static void | ||
| 67 | unblock_atimers (void) | ||
| 68 | { | ||
| 69 | sigmask_atimers (SIG_UNBLOCK); | ||
| 70 | } | ||
| 56 | 71 | ||
| 57 | /* Function prototypes. */ | 72 | /* Function prototypes. */ |
| 58 | 73 | ||
| @@ -60,8 +75,6 @@ static void set_alarm (void); | |||
| 60 | static void schedule_atimer (struct atimer *); | 75 | static void schedule_atimer (struct atimer *); |
| 61 | static struct atimer *append_atimer_lists (struct atimer *, | 76 | static struct atimer *append_atimer_lists (struct atimer *, |
| 62 | struct atimer *); | 77 | struct atimer *); |
| 63 | static void alarm_signal_handler (int signo); | ||
| 64 | |||
| 65 | 78 | ||
| 66 | /* Start a new atimer of type TYPE. TIME specifies when the timer is | 79 | /* Start a new atimer of type TYPE. TIME specifies when the timer is |
| 67 | ripe. FN is the function to call when the timer fires. | 80 | ripe. FN is the function to call when the timer fires. |
| @@ -111,7 +124,7 @@ start_atimer (enum atimer_type type, EMACS_TIME timestamp, atimer_callback fn, | |||
| 111 | t->fn = fn; | 124 | t->fn = fn; |
| 112 | t->client_data = client_data; | 125 | t->client_data = client_data; |
| 113 | 126 | ||
| 114 | BLOCK_ATIMERS; | 127 | block_atimers (); |
| 115 | 128 | ||
| 116 | /* Compute the timer's expiration time. */ | 129 | /* Compute the timer's expiration time. */ |
| 117 | switch (type) | 130 | switch (type) |
| @@ -132,7 +145,7 @@ start_atimer (enum atimer_type type, EMACS_TIME timestamp, atimer_callback fn, | |||
| 132 | 145 | ||
| 133 | /* Insert the timer in the list of active atimers. */ | 146 | /* Insert the timer in the list of active atimers. */ |
| 134 | schedule_atimer (t); | 147 | schedule_atimer (t); |
| 135 | UNBLOCK_ATIMERS; | 148 | unblock_atimers (); |
| 136 | 149 | ||
| 137 | /* Arrange for a SIGALRM at the time the next atimer is ripe. */ | 150 | /* Arrange for a SIGALRM at the time the next atimer is ripe. */ |
| 138 | set_alarm (); | 151 | set_alarm (); |
| @@ -148,7 +161,7 @@ cancel_atimer (struct atimer *timer) | |||
| 148 | { | 161 | { |
| 149 | int i; | 162 | int i; |
| 150 | 163 | ||
| 151 | BLOCK_ATIMERS; | 164 | block_atimers (); |
| 152 | 165 | ||
| 153 | for (i = 0; i < 2; ++i) | 166 | for (i = 0; i < 2; ++i) |
| 154 | { | 167 | { |
| @@ -175,7 +188,7 @@ cancel_atimer (struct atimer *timer) | |||
| 175 | } | 188 | } |
| 176 | } | 189 | } |
| 177 | 190 | ||
| 178 | UNBLOCK_ATIMERS; | 191 | unblock_atimers (); |
| 179 | } | 192 | } |
| 180 | 193 | ||
| 181 | 194 | ||
| @@ -206,7 +219,7 @@ append_atimer_lists (struct atimer *list_1, struct atimer *list_2) | |||
| 206 | void | 219 | void |
| 207 | stop_other_atimers (struct atimer *t) | 220 | stop_other_atimers (struct atimer *t) |
| 208 | { | 221 | { |
| 209 | BLOCK_ATIMERS; | 222 | block_atimers (); |
| 210 | 223 | ||
| 211 | if (t) | 224 | if (t) |
| 212 | { | 225 | { |
| @@ -231,7 +244,7 @@ stop_other_atimers (struct atimer *t) | |||
| 231 | 244 | ||
| 232 | stopped_atimers = append_atimer_lists (atimers, stopped_atimers); | 245 | stopped_atimers = append_atimer_lists (atimers, stopped_atimers); |
| 233 | atimers = t; | 246 | atimers = t; |
| 234 | UNBLOCK_ATIMERS; | 247 | unblock_atimers (); |
| 235 | } | 248 | } |
| 236 | 249 | ||
| 237 | 250 | ||
| @@ -246,7 +259,7 @@ run_all_atimers (void) | |||
| 246 | struct atimer *t = atimers; | 259 | struct atimer *t = atimers; |
| 247 | struct atimer *next; | 260 | struct atimer *next; |
| 248 | 261 | ||
| 249 | BLOCK_ATIMERS; | 262 | block_atimers (); |
| 250 | atimers = stopped_atimers; | 263 | atimers = stopped_atimers; |
| 251 | stopped_atimers = NULL; | 264 | stopped_atimers = NULL; |
| 252 | 265 | ||
| @@ -257,7 +270,7 @@ run_all_atimers (void) | |||
| 257 | t = next; | 270 | t = next; |
| 258 | } | 271 | } |
| 259 | 272 | ||
| 260 | UNBLOCK_ATIMERS; | 273 | unblock_atimers (); |
| 261 | } | 274 | } |
| 262 | } | 275 | } |
| 263 | 276 | ||
| @@ -356,7 +369,6 @@ run_timers (void) | |||
| 356 | if (! atimers) | 369 | if (! atimers) |
| 357 | pending_atimers = 0; | 370 | pending_atimers = 0; |
| 358 | 371 | ||
| 359 | #ifdef SYNC_INPUT | ||
| 360 | if (pending_atimers) | 372 | if (pending_atimers) |
| 361 | pending_signals = 1; | 373 | pending_signals = 1; |
| 362 | else | 374 | else |
| @@ -364,42 +376,36 @@ run_timers (void) | |||
| 364 | pending_signals = interrupt_input_pending; | 376 | pending_signals = interrupt_input_pending; |
| 365 | set_alarm (); | 377 | set_alarm (); |
| 366 | } | 378 | } |
| 367 | #else | ||
| 368 | if (! pending_atimers) | ||
| 369 | set_alarm (); | ||
| 370 | #endif | ||
| 371 | } | 379 | } |
| 372 | 380 | ||
| 373 | 381 | ||
| 374 | /* Signal handler for SIGALRM. SIGNO is the signal number, i.e. | 382 | /* Signal handler for SIGALRM. SIGNO is the signal number, i.e. |
| 375 | SIGALRM. */ | 383 | SIGALRM. */ |
| 376 | 384 | ||
| 377 | void | 385 | static void |
| 378 | alarm_signal_handler (int signo) | 386 | handle_alarm_signal (int sig) |
| 379 | { | 387 | { |
| 380 | #ifndef SYNC_INPUT | ||
| 381 | SIGNAL_THREAD_CHECK (signo); | ||
| 382 | #endif | ||
| 383 | |||
| 384 | pending_atimers = 1; | 388 | pending_atimers = 1; |
| 385 | #ifdef SYNC_INPUT | ||
| 386 | pending_signals = 1; | 389 | pending_signals = 1; |
| 387 | #else | 390 | } |
| 388 | run_timers (); | 391 | |
| 389 | #endif | 392 | static void |
| 393 | deliver_alarm_signal (int sig) | ||
| 394 | { | ||
| 395 | handle_on_main_thread (sig, handle_alarm_signal); | ||
| 390 | } | 396 | } |
| 391 | 397 | ||
| 392 | 398 | ||
| 393 | /* Call alarm_signal_handler for pending timers. */ | 399 | /* Call alarm signal handler for pending timers. */ |
| 394 | 400 | ||
| 395 | void | 401 | void |
| 396 | do_pending_atimers (void) | 402 | do_pending_atimers (void) |
| 397 | { | 403 | { |
| 398 | if (pending_atimers) | 404 | if (pending_atimers) |
| 399 | { | 405 | { |
| 400 | BLOCK_ATIMERS; | 406 | block_atimers (); |
| 401 | run_timers (); | 407 | run_timers (); |
| 402 | UNBLOCK_ATIMERS; | 408 | unblock_atimers (); |
| 403 | } | 409 | } |
| 404 | } | 410 | } |
| 405 | 411 | ||
| @@ -412,7 +418,9 @@ turn_on_atimers (bool on) | |||
| 412 | { | 418 | { |
| 413 | if (on) | 419 | if (on) |
| 414 | { | 420 | { |
| 415 | signal (SIGALRM, alarm_signal_handler); | 421 | struct sigaction action; |
| 422 | emacs_sigaction_init (&action, deliver_alarm_signal); | ||
| 423 | sigaction (SIGALRM, &action, 0); | ||
| 416 | set_alarm (); | 424 | set_alarm (); |
| 417 | } | 425 | } |
| 418 | else | 426 | else |
| @@ -423,8 +431,10 @@ turn_on_atimers (bool on) | |||
| 423 | void | 431 | void |
| 424 | init_atimer (void) | 432 | init_atimer (void) |
| 425 | { | 433 | { |
| 434 | struct sigaction action; | ||
| 426 | free_atimers = stopped_atimers = atimers = NULL; | 435 | free_atimers = stopped_atimers = atimers = NULL; |
| 427 | pending_atimers = 0; | 436 | pending_atimers = 0; |
| 428 | /* pending_signals is initialized in init_keyboard.*/ | 437 | /* pending_signals is initialized in init_keyboard.*/ |
| 429 | signal (SIGALRM, alarm_signal_handler); | 438 | emacs_sigaction_init (&action, deliver_alarm_signal); |
| 439 | sigaction (SIGALRM, &action, 0); | ||
| 430 | } | 440 | } |