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:
Jarred Nicholls 2021-12-02 20:18:21 -05:00 committed by GitHub
parent 1ae9cdcb43
commit 18a16ad956
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 53 additions and 9 deletions

1
Cargo.lock generated
View File

@ -5346,6 +5346,7 @@ dependencies = [
"log 0.4.14",
"num-derive",
"num-traits",
"parking_lot 0.11.2",
"rand 0.7.3",
"rustc_version 0.4.0",
"rustversion",

View File

@ -3188,6 +3188,7 @@ dependencies = [
"log",
"num-derive",
"num-traits",
"parking_lot",
"rand 0.7.3",
"rustc_version 0.4.0",
"rustversion",

View File

@ -19,7 +19,7 @@ bs58 = "0.4.0"
bytemuck = { version = "1.7.2", features = ["derive"] }
bv = { version = "0.11.1", features = ["serde"] }
hex = "0.4.2"
itertools = "0.10.1"
itertools = "0.10.1"
lazy_static = "1.4.0"
log = "0.4.14"
num-derive = "0.3"
@ -44,6 +44,9 @@ rand = "0.7.0"
solana-logger = { path = "../../logger", version = "=1.9.0" }
itertools = "0.10.1"
[target.'cfg(not(target_pointer_width = "64"))'.dependencies]
parking_lot = "0.11"
[dev-dependencies]
static_assertions = "1.1.0"
assert_matches = "1.3.0"

View 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
}
}
}

View File

@ -103,11 +103,11 @@ impl Hash {
/// unique Hash for tests and benchmarks.
pub fn new_unique() -> Self {
use std::sync::atomic::{AtomicU64, Ordering};
use crate::atomic_u64::AtomicU64;
static I: AtomicU64 = AtomicU64::new(1);
let mut b = [0u8; HASH_BYTES];
let i = I.fetch_add(1, Ordering::Relaxed);
let i = I.fetch_add(1);
b[0..8].copy_from_slice(&i.to_le_bytes());
Self::new(&b)
}

View File

@ -106,11 +106,11 @@ impl Hash {
/// unique Hash for tests and benchmarks.
pub fn new_unique() -> Self {
use std::sync::atomic::{AtomicU64, Ordering};
use crate::atomic_u64::AtomicU64;
static I: AtomicU64 = AtomicU64::new(1);
let mut b = [0u8; HASH_BYTES];
let i = I.fetch_add(1, Ordering::Relaxed);
let i = I.fetch_add(1);
b[0..8].copy_from_slice(&i.to_le_bytes());
Self::new(&b)
}

View File

@ -104,11 +104,11 @@ impl Hash {
/// unique Hash for tests and benchmarks.
pub fn new_unique() -> Self {
use std::sync::atomic::{AtomicU64, Ordering};
use crate::atomic_u64::AtomicU64;
static I: AtomicU64 = AtomicU64::new(1);
let mut b = [0u8; HASH_BYTES];
let i = I.fetch_add(1, Ordering::Relaxed);
let i = I.fetch_add(1);
b[0..8].copy_from_slice(&i.to_le_bytes());
Self::new(&b)
}

View File

@ -6,6 +6,7 @@
extern crate self as solana_program;
pub mod account_info;
pub(crate) mod atomic_u64;
pub mod blake3;
pub mod borsh;
pub mod bpf_loader;

View File

@ -148,11 +148,11 @@ impl Pubkey {
/// unique Pubkey for tests and benchmarks.
pub fn new_unique() -> Self {
use std::sync::atomic::{AtomicU64, Ordering};
use crate::atomic_u64::AtomicU64;
static I: AtomicU64 = AtomicU64::new(1);
let mut b = [0u8; 32];
let i = I.fetch_add(1, Ordering::Relaxed);
let i = I.fetch_add(1);
b[0..8].copy_from_slice(&i.to_le_bytes());
Self::new(&b)
}