A timeout of 42949663 ms (about 11.9 hours) or more ended early, because millis * hz + 999 overflowed 32 bits. "sleep 12h", for instance, returned after 4 minutes 10 seconds, and thread_depress_priority could end up with 0 ticks and no timer at all. Do the computation in 64 bits.
* kern/sched_prim.h: Include <stdint.h>. (convert_ipc_timeout_to_ticks): Use a 64-bit product. --- Here it is with the computation in 64 bits. kern/sched_prim.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kern/sched_prim.h b/kern/sched_prim.h index c250b220..da2411df 100644 --- a/kern/sched_prim.h +++ b/kern/sched_prim.h @@ -34,6 +34,7 @@ #ifndef _KERN_SCHED_PRIM_H_ #define _KERN_SCHED_PRIM_H_ +#include <stdint.h> #include <mach/boolean.h> #include <mach/message.h> /* for mach_msg_timeout_t */ #include <kern/lock.h> @@ -166,10 +167,11 @@ extern void stack_free( * to a timeout in ticks (for use by set_timeout). * This conversion rounds UP so that small timeouts * at least wait for one tick instead of not waiting at all. + * It is computed in 64 bits so that long timeouts don't overflow. */ #define convert_ipc_timeout_to_ticks(millis) \ - (((millis) * hz + 999) / 1000) + ((natural_t) (((uint64_t) (millis) * hz + 999) / 1000)) void set_pri(thread_t th, int pri, boolean_t resched); void do_thread_scan(void); -- 2.56.0
