Al-Qudsi: Implementing truly safe semaphores in rust

Post Syndicated from original https://lwn.net/Articles/910417/

Mahmoud Al-Qudsi provides
extensive details
on what it takes to implement a safe semaphore type
in the Rust language.

The problem is that with n > 1, there’s no concept of a
“privileged” owning thread and all threads that have “obtained” the
semaphore do so equally. Therefore, a rust semaphore can only ever
provide read-only (&T) access to an underlying resource,
limiting the usefulness of such a semaphore almost to the point of
having no utility. As such, the only safe “owning” semaphore with
read-write access that can exist in the rust world would be
Semaphore<()>, or one that actually owns no data and can
only be used for its side effect of limiting concurrency while the
semaphore is “owned,” so to speak.