clinuxsynchronizationsystem-callsfutex

Wake up all threads sleeping on futex


I want to wake up all threads sleeping on a futex. I am confused by an inconsistency in the man futex page.

Specifically, the page says that val has type uint32_t. Later on, in the FUTEX_WAKE section, the page says that to wake up all threads, pass INT_MAX to val.

INT_MAX is for signed integers, not unsigned integers. I would expect to pass UINT32_MAX instead. Though I could also anticipate the argument that the val variable is reused for different things and so INT_MAX is still the correct option since val is cast to an int in the kernel.

What is the correct way to do this?


Solution

  • I reviewed the kernel code and it is correct to pass INT_MAX to val. The kernel casts val to a signed integer. Specifically, do_futex() in kernel/syscalls/futex.c passes val to futex_wake() in kernel/futex/waitwake.c. val is passed via the int nr_wake parameter in futex_wake(), so val is implicitly casted to an int.

    Therefore, the count passed for FUTEX_WAKE is treated as a signed integer, so INT_MAX is the correct value to use to wake up all threads.