diff options
| author | Ian Kelling | 2015-07-05 15:55:19 -0700 |
|---|---|---|
| committer | Paul Eggert | 2015-07-05 19:21:49 -0700 |
| commit | 6e2fcc213d4569992009480cf298536407cea4d2 (patch) | |
| tree | e96d25a902eb180a4fd4029a3a16d836631f1c0e /src/process.c | |
| parent | 91cbc7b729304302efe7e1d34ac9bb5dc6550f12 (diff) | |
| download | emacs-6e2fcc213d4569992009480cf298536407cea4d2.tar.gz emacs-6e2fcc213d4569992009480cf298536407cea4d2.zip | |
Refactor timeouts in wait_reading_process_output
* src/process.c (wait_reading_process_output):
Simplify timeouts with an enum. Remove a redundant condition.
(Bug#20978)
Diffstat (limited to 'src/process.c')
| -rw-r--r-- | src/process.c | 109 |
1 files changed, 42 insertions, 67 deletions
diff --git a/src/process.c b/src/process.c index 6b3648e6952..325c069bd5c 100644 --- a/src/process.c +++ b/src/process.c | |||
| @@ -4586,6 +4586,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, | |||
| 4586 | int xerrno; | 4586 | int xerrno; |
| 4587 | Lisp_Object proc; | 4587 | Lisp_Object proc; |
| 4588 | struct timespec timeout, end_time; | 4588 | struct timespec timeout, end_time; |
| 4589 | enum { MINIMUM = -1, TIMEOUT, INFINITY } wait; | ||
| 4589 | int got_some_output = -1; | 4590 | int got_some_output = -1; |
| 4590 | ptrdiff_t count = SPECPDL_INDEX (); | 4591 | ptrdiff_t count = SPECPDL_INDEX (); |
| 4591 | 4592 | ||
| @@ -4601,21 +4602,19 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, | |||
| 4601 | waiting_for_user_input_p); | 4602 | waiting_for_user_input_p); |
| 4602 | waiting_for_user_input_p = read_kbd; | 4603 | waiting_for_user_input_p = read_kbd; |
| 4603 | 4604 | ||
| 4604 | if (time_limit < 0) | 4605 | if (TYPE_MAXIMUM (time_t) < time_limit) |
| 4605 | { | ||
| 4606 | time_limit = 0; | ||
| 4607 | nsecs = -1; | ||
| 4608 | } | ||
| 4609 | else if (TYPE_MAXIMUM (time_t) < time_limit) | ||
| 4610 | time_limit = TYPE_MAXIMUM (time_t); | 4606 | time_limit = TYPE_MAXIMUM (time_t); |
| 4611 | 4607 | ||
| 4612 | /* Since we may need to wait several times, | 4608 | if (time_limit < 0 || nsecs < 0) |
| 4613 | compute the absolute time to return at. */ | 4609 | wait = MINIMUM; |
| 4614 | if (time_limit || nsecs > 0) | 4610 | else if (time_limit > 0 || nsecs > 0) |
| 4615 | { | 4611 | { |
| 4616 | timeout = make_timespec (time_limit, nsecs); | 4612 | wait = TIMEOUT; |
| 4617 | end_time = timespec_add (current_timespec (), timeout); | 4613 | end_time = timespec_add (current_timespec (), |
| 4614 | make_timespec (time_limit, nsecs)); | ||
| 4618 | } | 4615 | } |
| 4616 | else | ||
| 4617 | wait = INFINITY; | ||
| 4619 | 4618 | ||
| 4620 | while (1) | 4619 | while (1) |
| 4621 | { | 4620 | { |
| @@ -4635,19 +4634,11 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, | |||
| 4635 | 4634 | ||
| 4636 | /* After reading input, vacuum up any leftovers without waiting. */ | 4635 | /* After reading input, vacuum up any leftovers without waiting. */ |
| 4637 | if (0 <= got_some_output) | 4636 | if (0 <= got_some_output) |
| 4638 | nsecs = -1; | 4637 | wait = MINIMUM; |
| 4639 | 4638 | ||
| 4640 | /* Compute time from now till when time limit is up. */ | 4639 | /* Compute time from now till when time limit is up. */ |
| 4641 | /* Exit if already run out. */ | 4640 | /* Exit if already run out. */ |
| 4642 | if (nsecs < 0) | 4641 | if (wait == TIMEOUT) |
| 4643 | { | ||
| 4644 | /* A negative timeout means | ||
| 4645 | gobble output available now | ||
| 4646 | but don't wait at all. */ | ||
| 4647 | |||
| 4648 | timeout = make_timespec (0, 0); | ||
| 4649 | } | ||
| 4650 | else if (time_limit || nsecs > 0) | ||
| 4651 | { | 4642 | { |
| 4652 | struct timespec now = current_timespec (); | 4643 | struct timespec now = current_timespec (); |
| 4653 | if (timespec_cmp (end_time, now) <= 0) | 4644 | if (timespec_cmp (end_time, now) <= 0) |
| @@ -4655,9 +4646,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, | |||
| 4655 | timeout = timespec_sub (end_time, now); | 4646 | timeout = timespec_sub (end_time, now); |
| 4656 | } | 4647 | } |
| 4657 | else | 4648 | else |
| 4658 | { | 4649 | timeout = make_timespec (wait < TIMEOUT ? 0 : 100000, 0); |
| 4659 | timeout = make_timespec (100000, 0); | ||
| 4660 | } | ||
| 4661 | 4650 | ||
| 4662 | /* Normally we run timers here. | 4651 | /* Normally we run timers here. |
| 4663 | But not if wait_for_cell; in those cases, | 4652 | But not if wait_for_cell; in those cases, |
| @@ -4698,24 +4687,20 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, | |||
| 4698 | && requeued_events_pending_p ()) | 4687 | && requeued_events_pending_p ()) |
| 4699 | break; | 4688 | break; |
| 4700 | 4689 | ||
| 4701 | /* A negative timeout means do not wait at all. */ | 4690 | if (timespec_valid_p (timer_delay)) |
| 4702 | if (nsecs >= 0) | 4691 | { |
| 4703 | { | 4692 | if (timespec_cmp (timer_delay, timeout) < 0) |
| 4704 | if (timespec_valid_p (timer_delay)) | 4693 | { |
| 4705 | { | 4694 | timeout = timer_delay; |
| 4706 | if (timespec_cmp (timer_delay, timeout) < 0) | 4695 | timeout_reduced_for_timers = true; |
| 4707 | { | 4696 | } |
| 4708 | timeout = timer_delay; | 4697 | } |
| 4709 | timeout_reduced_for_timers = true; | 4698 | else |
| 4710 | } | 4699 | { |
| 4711 | } | 4700 | /* This is so a breakpoint can be put here. */ |
| 4712 | else | 4701 | wait_reading_process_output_1 (); |
| 4713 | { | 4702 | } |
| 4714 | /* This is so a breakpoint can be put here. */ | 4703 | } |
| 4715 | wait_reading_process_output_1 (); | ||
| 4716 | } | ||
| 4717 | } | ||
| 4718 | } | ||
| 4719 | 4704 | ||
| 4720 | /* Cause C-g and alarm signals to take immediate action, | 4705 | /* Cause C-g and alarm signals to take immediate action, |
| 4721 | and cause input available signals to zero out timeout. | 4706 | and cause input available signals to zero out timeout. |
| @@ -4964,7 +4949,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, | |||
| 4964 | /* If we woke up due to SIGWINCH, actually change size now. */ | 4949 | /* If we woke up due to SIGWINCH, actually change size now. */ |
| 4965 | do_pending_window_change (0); | 4950 | do_pending_window_change (0); |
| 4966 | 4951 | ||
| 4967 | if ((time_limit || nsecs) && nfds == 0 && ! timeout_reduced_for_timers) | 4952 | if (wait < INFINITY && nfds == 0 && ! timeout_reduced_for_timers) |
| 4968 | /* We waited the full specified time, so return now. */ | 4953 | /* We waited the full specified time, so return now. */ |
| 4969 | break; | 4954 | break; |
| 4970 | if (nfds < 0) | 4955 | if (nfds < 0) |
| @@ -6954,21 +6939,21 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, | |||
| 6954 | { | 6939 | { |
| 6955 | register int nfds; | 6940 | register int nfds; |
| 6956 | struct timespec end_time, timeout; | 6941 | struct timespec end_time, timeout; |
| 6942 | enum { MINIMUM = -1, TIMEOUT, INFINITY } wait; | ||
| 6957 | 6943 | ||
| 6958 | if (time_limit < 0) | 6944 | if (TYPE_MAXIMUM (time_t) < time_limit) |
| 6959 | { | ||
| 6960 | time_limit = 0; | ||
| 6961 | nsecs = -1; | ||
| 6962 | } | ||
| 6963 | else if (TYPE_MAXIMUM (time_t) < time_limit) | ||
| 6964 | time_limit = TYPE_MAXIMUM (time_t); | 6945 | time_limit = TYPE_MAXIMUM (time_t); |
| 6965 | 6946 | ||
| 6966 | /* What does time_limit really mean? */ | 6947 | if (time_limit < 0 || nsecs < 0) |
| 6967 | if (time_limit || nsecs > 0) | 6948 | wait = MINIMUM; |
| 6949 | else if (time_limit > 0 || nsecs > 0) | ||
| 6968 | { | 6950 | { |
| 6969 | timeout = make_timespec (time_limit, nsecs); | 6951 | wait = TIMEOUT; |
| 6970 | end_time = timespec_add (current_timespec (), timeout); | 6952 | end_time = timespec_add (current_timespec (), |
| 6953 | make_timespec (time_limit, nsecs)); | ||
| 6971 | } | 6954 | } |
| 6955 | else | ||
| 6956 | wait = INFINITY; | ||
| 6972 | 6957 | ||
| 6973 | /* Turn off periodic alarms (in case they are in use) | 6958 | /* Turn off periodic alarms (in case they are in use) |
| 6974 | and then turn off any other atimers, | 6959 | and then turn off any other atimers, |
| @@ -6994,15 +6979,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, | |||
| 6994 | 6979 | ||
| 6995 | /* Compute time from now till when time limit is up. */ | 6980 | /* Compute time from now till when time limit is up. */ |
| 6996 | /* Exit if already run out. */ | 6981 | /* Exit if already run out. */ |
| 6997 | if (nsecs < 0) | 6982 | if (wait == TIMEOUT) |
| 6998 | { | ||
| 6999 | /* A negative timeout means | ||
| 7000 | gobble output available now | ||
| 7001 | but don't wait at all. */ | ||
| 7002 | |||
| 7003 | timeout = make_timespec (0, 0); | ||
| 7004 | } | ||
| 7005 | else if (time_limit || nsecs > 0) | ||
| 7006 | { | 6983 | { |
| 7007 | struct timespec now = current_timespec (); | 6984 | struct timespec now = current_timespec (); |
| 7008 | if (timespec_cmp (end_time, now) <= 0) | 6985 | if (timespec_cmp (end_time, now) <= 0) |
| @@ -7010,9 +6987,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, | |||
| 7010 | timeout = timespec_sub (end_time, now); | 6987 | timeout = timespec_sub (end_time, now); |
| 7011 | } | 6988 | } |
| 7012 | else | 6989 | else |
| 7013 | { | 6990 | timeout = make_timespec (wait < TIMEOUT ? 0 : 100000, 0); |
| 7014 | timeout = make_timespec (100000, 0); | ||
| 7015 | } | ||
| 7016 | 6991 | ||
| 7017 | /* If our caller will not immediately handle keyboard events, | 6992 | /* If our caller will not immediately handle keyboard events, |
| 7018 | run timer events directly. | 6993 | run timer events directly. |
| @@ -7040,7 +7015,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, | |||
| 7040 | && requeued_events_pending_p ()) | 7015 | && requeued_events_pending_p ()) |
| 7041 | break; | 7016 | break; |
| 7042 | 7017 | ||
| 7043 | if (timespec_valid_p (timer_delay) && nsecs >= 0) | 7018 | if (timespec_valid_p (timer_delay)) |
| 7044 | { | 7019 | { |
| 7045 | if (timespec_cmp (timer_delay, timeout) < 0) | 7020 | if (timespec_cmp (timer_delay, timeout) < 0) |
| 7046 | { | 7021 | { |
| @@ -7084,7 +7059,7 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, | |||
| 7084 | /* If we woke up due to SIGWINCH, actually change size now. */ | 7059 | /* If we woke up due to SIGWINCH, actually change size now. */ |
| 7085 | do_pending_window_change (0); | 7060 | do_pending_window_change (0); |
| 7086 | 7061 | ||
| 7087 | if ((time_limit || nsecs) && nfds == 0 && ! timeout_reduced_for_timers) | 7062 | if (wait < INFINITY && nfds == 0 && ! timeout_reduced_for_timers) |
| 7088 | /* We waited the full specified time, so return now. */ | 7063 | /* We waited the full specified time, so return now. */ |
| 7089 | break; | 7064 | break; |
| 7090 | 7065 | ||