You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be convenient if I could check if AtomicCells are lock-free at compile time. by marking AtomicCell<T>::is_lock_free() as a const fn.
For context, I'm working on an audio program in C++, due to C++ libraries including Qt. In audio programs, taking locks on the audio thread is regarded as unacceptable because it can block and cause the audio to stutter. As a result, I'd like to statically assert that a given atomic type written by the audio thread is lock-free. (I'm mostly looking at Rust for inspiration and comparison, to see whether parts of my code are nicer in C++ or Rust.)
Marking AtomicCell<T>::is_lock_free() as const doesn't seem to cause issues, but depends on atomic_is_lock_free. Trying to mark atomic_is_lock_free as const fails on rust-lang/rust#49146 "Allow if and match in constants", and rust-lang/rust#52000 "Allow loop in constant evaluation".
From a cursory examination, atomic! doesn't truly need a loop, but depends on if. Note that I don't fully understand atomic! nor all call sites.
Are there any plans to mark this as a const fn, to allow users to statically assert it's lock-free? I don't know if atomic! needs to be split into two macros or not, with duplication and possible future desynchronization.
The text was updated successfully, but these errors were encountered:
Probably const_if_match and const_loop will be stable at the same time (rust-lang/rust#72437 (comment)), but either way, atomic! doesn't really need a loop, so I think it makes sense to conditionally mark is_lock_free as a const fn when const_if_match is stable.
600: Make AtomicCell::is_lock_free always const fn r=Vtec234 a=taiki-e
`if` expression can only be used in const fn since Rust 1.46, so const `is_lock_free` was require Rust 1.46 (context: #531, #546).
However, when we replace uses of `if` expression with `|` and `&` operators, it seems `is_lock_free` can be used as const fn even in Rust 1.36.
r? @jeehoonkang or @Vtec234
Co-authored-by: Taiki Endo <[email protected]>
It would be convenient if I could check if AtomicCells are lock-free at compile time. by marking
AtomicCell<T>::is_lock_free()
as a const fn.For context, I'm working on an audio program in C++, due to C++ libraries including Qt. In audio programs, taking locks on the audio thread is regarded as unacceptable because it can block and cause the audio to stutter. As a result, I'd like to statically assert that a given atomic type written by the audio thread is lock-free. (I'm mostly looking at Rust for inspiration and comparison, to see whether parts of my code are nicer in C++ or Rust.)
crossbeam/crossbeam-utils/src/atomic/atomic_cell.rs
Lines 108 to 110 in 81ab18e
calls
atomic_is_lock_free<T>
:crossbeam/crossbeam-utils/src/atomic/atomic_cell.rs
Lines 809 to 812 in 81ab18e
expands to the
atomic!
macro:crossbeam/crossbeam-utils/src/atomic/atomic_cell.rs
Lines 777 to 807 in 81ab18e
Marking
AtomicCell<T>::is_lock_free()
asconst
doesn't seem to cause issues, but depends onatomic_is_lock_free
. Trying to markatomic_is_lock_free
asconst
fails on rust-lang/rust#49146 "Allowif
andmatch
in constants", and rust-lang/rust#52000 "Allowloop
in constant evaluation".From a cursory examination,
atomic!
doesn't truly need aloop
, but depends onif
. Note that I don't fully understandatomic!
nor all call sites.Are there any plans to mark this as a const fn, to allow users to statically assert it's lock-free? I don't know if
atomic!
needs to be split into two macros or not, with duplication and possible future desynchronization.The text was updated successfully, but these errors were encountered: