|
|
Log in / Subscribe / Register

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 22, 2022 8:47 UTC (Tue) by metan (subscriber, #74107)
In reply to: Beingessner: Rust's Unsafe Pointer Types Need An Overhaul by tialaramex
Parent article: Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Actually volatile must be used for any global variable modified from a signal handler. The most common pattern is to have volatile sig_atomic_t global flag set by the signal handler. I bet this is the most common use for volatile in userspace.


to post comments

Beingessner: Rust's Unsafe Pointer Types Need An Overhaul

Posted Mar 22, 2022 13:25 UTC (Tue) by tialaramex (subscriber, #21167) [Link]

Fair. AIUI POSIX promises only that this will work for volatile sig_atomic_t and there's no promise this will work for variables with another type even though in practice int will work on real hardware.

This all pre-dates a formal memory model, but it is promised in POSIX and so you are indeed welcome to rely on it on a POSIX system. Like making errno work the way the standard says it should, on modern systems this involves a considerable amount of extra lifting for your compiler and C library, but that work is done and so yes you might as well rely on it.

There's a lot of low-level code out there actually banging on MMIO far from any POSIX system and MMIO is, in fact by my understanding where volatile starts out (first C compilers are too naive to eliminate duplicate stores/ loads, as the optimiser improves it elides enough apparently useless loads and stores that now the device driver doesn't work, volatile qualifier tells the compiler not to optimise the loads and stores and now the device drivers work properly again), so if I was a betting man I might take the other side of your bet.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds