Hello,

ajrojas, le mar. 01 déc. 2026 14:04:57 -0300, a ecrit:
> * kern/sched_prim.h (convert_ipc_timeout_to_ticks): Divide by 1000
> before multiplying by hz.

But then the delay is less precise... Better compute with 64b, it's
really not expensive compared to correctness.

Samuel

> ---
> To see it without waiting 12 hours, this returns after 0.34 s without
> the patch:
> 
>   #include <mach.h>
>   #include <stdio.h>
>   #include <time.h>
> 
>   int
>   main (void)
>   {
>     mach_port_t port;
>     struct { mach_msg_header_t h; char buf[64]; } msg;
>     struct timespec a, b;
> 
>     mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE, &port);
>     clock_gettime (CLOCK_MONOTONIC, &a);
>     mach_msg (&msg.h, MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0, sizeof msg, port,
>               42950000, MACH_PORT_NULL);  /* 11 h 55 min 50 s */
>     clock_gettime (CLOCK_MONOTONIC, &b);
>     printf ("%.3f s\n", b.tv_sec - a.tv_sec + (b.tv_nsec - a.tv_nsec) / 1e9);
>     return 0;
>   }
> 
> With the patch, in a Guix x86_64 VM, that program, "sleep 42950" and a
> 12 h timeout were all still waiting after 7 minutes, and short timeouts
> behave as before.  On the host, the new expression gives the old result
> for every input that didn't overflow.  It builds for i386 but I haven't
> booted it there, and I haven't tested the thread_depress_priority case.
> 
> The (mach_msg_timeout_t) -1 that glibc passes for deadlines more than
> 49.7 days away used to return right away.  Now it really waits.
> 
> I divided first instead of casting to 64 bits, so that timed mach_msg
> calls on i386 don't pay for a 64-bit division.
> 
>  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..3d149209 100644
> --- a/kern/sched_prim.h
> +++ b/kern/sched_prim.h
> @@ -166,10 +166,12 @@ 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.
> + *   Whole seconds are converted first, so that long timeouts
> + *   don't overflow.
>   */
>  
>  #define convert_ipc_timeout_to_ticks(millis) \
> -             (((millis) * hz + 999) / 1000)
> +             ((millis) / 1000 * hz + ((millis) % 1000 * hz + 999) / 1000)
>  
>  void set_pri(thread_t th, int pri, boolean_t resched);
>  void do_thread_scan(void);
> -- 
> 2.56.0
> 

-- 
Samuel
`When you say "I wrote a program that crashed Windows", people just stare at
you blankly and say "Hey, I got those with the system, *for free*".'
(By Linus Torvalds)

Reply via email to