Support building solana-program on 32-bit architectures that do not (#21577)
have 64-bit atomics by using a Mutex<u64> on 32-bit architectures. Currently the usage of atomics are only in functions that support tests and benchmarks.
This commit is contained in:
38
sdk/program/src/atomic_u64.rs
Normal file
38
sdk/program/src/atomic_u64.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
pub(crate) use implementation::AtomicU64;
|
||||
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
mod implementation {
|
||||
use std::sync::atomic;
|
||||
|
||||
pub(crate) struct AtomicU64(atomic::AtomicU64);
|
||||
|
||||
impl AtomicU64 {
|
||||
pub(crate) const fn new(initial: u64) -> Self {
|
||||
Self(atomic::AtomicU64::new(initial))
|
||||
}
|
||||
|
||||
pub(crate) fn fetch_add(&self, v: u64) -> u64 {
|
||||
self.0.fetch_add(v, atomic::Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_pointer_width = "64"))]
|
||||
mod implementation {
|
||||
use parking_lot::{const_mutex, Mutex};
|
||||
|
||||
pub(crate) struct AtomicU64(Mutex<u64>);
|
||||
|
||||
impl AtomicU64 {
|
||||
pub(crate) const fn new(initial: u64) -> Self {
|
||||
Self(const_mutex(initial))
|
||||
}
|
||||
|
||||
pub(crate) fn fetch_add(&self, v: u64) -> u64 {
|
||||
let mut lock = self.0.lock();
|
||||
let i = *lock;
|
||||
*lock = i + v;
|
||||
i
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user